blob: 4b2866cb67fc50040999275ebf918e4bb048e6c5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070053#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070057#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopiana67932f2011-04-20 14:20:59 -070059#include <private/surfaceflinger/SharedBufferStack.h>
60
Mathias Agopiana1ecca92009-05-21 19:21:59 -070061/* ideally AID_GRAPHICS would be in a semi-public header
62 * or there would be a way to map a user/group name to its id
63 */
64#ifndef AID_GRAPHICS
65#define AID_GRAPHICS 1003
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Mathias Agopiancbb288b2009-09-07 16:32:45 -070083 mResizeTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mFreezeDisplay(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mFreezeCount(0),
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -070091 mFreezeDisplayTime(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070096 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070097 mDebugInSwapBuffers(0),
98 mLastSwapBufferTime(0),
99 mDebugInTransaction(0),
100 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700101 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mConsoleSignals(0),
103 mSecureFrameBuffer(0)
104{
105 init();
106}
107
108void SurfaceFlinger::init()
109{
110 LOGI("SurfaceFlinger is starting");
111
112 // debugging stuff...
113 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 property_get("debug.sf.showupdates", value, "0");
116 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 property_get("debug.sf.showbackground", value, "0");
119 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121 property_get("debug.sf.ddms", value, "0");
122 mDebugDDMS = atoi(value);
123 if (mDebugDDMS) {
124 DdmConnection::start(getServiceName());
125 }
126
Mathias Agopian78fd5012010-04-20 14:51:04 -0700127 LOGI_IF(mDebugRegion, "showupdates enabled");
128 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700129 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
132SurfaceFlinger::~SurfaceFlinger()
133{
134 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700137sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700139 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian7e27f052010-05-28 14:22:23 -0700142sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143{
Mathias Agopian96f08192010-06-02 23:28:45 -0700144 sp<ISurfaceComposerClient> bclient;
145 sp<Client> client(new Client(this));
146 status_t err = client->initCheck();
147 if (err == NO_ERROR) {
148 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return bclient;
151}
152
Jamie Gennis9a78c902011-01-12 18:30:40 -0800153sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
154{
155 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
156 return gba;
157}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
160{
161 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
162 const GraphicPlane& plane(mGraphicPlanes[dpy]);
163 return plane;
164}
165
166GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
167{
168 return const_cast<GraphicPlane&>(
169 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
170}
171
172void SurfaceFlinger::bootFinished()
173{
174 const nsecs_t now = systemTime();
175 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700176 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700177 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700178
179 // wait patiently for the window manager death
180 const String16 name("window");
181 sp<IBinder> window(defaultServiceManager()->getService(name));
182 if (window != 0) {
183 window->linkToDeath(this);
184 }
185
186 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700187 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188}
189
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700190void SurfaceFlinger::binderDied(const wp<IBinder>& who)
191{
192 // the window manager died on us. prepare its eulogy.
193
194 // unfreeze the screen in case it was... frozen
195 mFreezeDisplayTime = 0;
196 mFreezeCount = 0;
197 mFreezeDisplay = false;
198
199 // reset screen orientation
200 setOrientation(0, eOrientationDefault, 0);
201
202 // restart the boot-animation
203 property_set("ctl.start", "bootanim");
204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206void SurfaceFlinger::onFirstRef()
207{
208 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
209
210 // Wait for the main thread to be done with its initialization
211 mReadyToRunBarrier.wait();
212}
213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214static inline uint16_t pack565(int r, int g, int b) {
215 return (r<<11)|(g<<5)|b;
216}
217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218status_t SurfaceFlinger::readyToRun()
219{
220 LOGI( "SurfaceFlinger's main thread ready to run. "
221 "Initializing graphics H/W...");
222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 // we only support one display currently
224 int dpy = 0;
225
226 {
227 // initialize the main display
228 GraphicPlane& plane(graphicPlane(dpy));
229 DisplayHardware* const hw = new DisplayHardware(this, dpy);
230 plane.setDisplayHardware(hw);
231 }
232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 // create the shared control-block
234 mServerHeap = new MemoryHeapBase(4096,
235 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
236 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
239 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700240
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700241 new(mServerCblk) surface_flinger_cblk_t;
242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 // initialize primary screen
244 // (other display should be initialized in the same manner, but
245 // asynchronously, as they could come and go. None of this is supported
246 // yet).
247 const GraphicPlane& plane(graphicPlane(dpy));
248 const DisplayHardware& hw = plane.displayHardware();
249 const uint32_t w = hw.getWidth();
250 const uint32_t h = hw.getHeight();
251 const uint32_t f = hw.getFormat();
252 hw.makeCurrent();
253
254 // initialize the shared control block
255 mServerCblk->connected |= 1<<dpy;
256 display_cblk_t* dcblk = mServerCblk->displays + dpy;
257 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800258 dcblk->w = plane.getWidth();
259 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 dcblk->format = f;
261 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
262 dcblk->xdpi = hw.getDpiX();
263 dcblk->ydpi = hw.getDpiY();
264 dcblk->fps = hw.getRefreshRate();
265 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266
267 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700269 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glEnableClientState(GL_VERTEX_ARRAY);
271 glEnable(GL_SCISSOR_TEST);
272 glShadeModel(GL_FLAT);
273 glDisable(GL_DITHER);
274 glDisable(GL_CULL_FACE);
275
276 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
277 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 glGenTextures(1, &mWormholeTexName);
280 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700286 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
287
288 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
289 glGenTextures(1, &mProtectedTexName);
290 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
293 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
294 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
295 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
296 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
298 glViewport(0, 0, w, h);
299 glMatrixMode(GL_PROJECTION);
300 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700301 // put the origin in the left-bottom corner
302 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 mReadyToRunBarrier.open();
305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700310 // start boot animation
311 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return NO_ERROR;
314}
315
316// ----------------------------------------------------------------------------
317#if 0
318#pragma mark -
319#pragma mark Events Handler
320#endif
321
322void SurfaceFlinger::waitForEvent()
323{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700324 while (true) {
325 nsecs_t timeout = -1;
Mathias Agopian04087722009-12-01 17:23:28 -0800326 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700327 if (UNLIKELY(isFrozen())) {
328 // wait 5 seconds
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700329 const nsecs_t now = systemTime();
330 if (mFreezeDisplayTime == 0) {
331 mFreezeDisplayTime = now;
332 }
333 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
334 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700335 }
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700336
Mathias Agopianbb641242010-05-18 17:06:55 -0700337 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian04087722009-12-01 17:23:28 -0800338
339 // see if we timed out
340 if (isFrozen()) {
341 const nsecs_t now = systemTime();
342 nsecs_t frozenTime = (now - mFreezeDisplayTime);
343 if (frozenTime >= freezeDisplayTimeout) {
344 // we timed out and are still frozen
345 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
346 mFreezeDisplay, mFreezeCount);
347 mFreezeDisplayTime = 0;
348 mFreezeCount = 0;
349 mFreezeDisplay = false;
350 }
351 }
352
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700353 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700354 switch (msg->what) {
355 case MessageQueue::INVALIDATE:
356 // invalidate message, just return to the main loop
357 return;
358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800360 }
361}
362
363void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700364 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
Jamie Gennis582270d2011-08-17 18:19:00 -0700367bool SurfaceFlinger::authenticateSurfaceTexture(
368 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800369 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700370 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800371
372 // Check the visible layer list for the ISurface
373 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
374 size_t count = currentLayers.size();
375 for (size_t i=0 ; i<count ; i++) {
376 const sp<LayerBase>& layer(currentLayers[i]);
377 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700378 if (lbc != NULL) {
379 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
380 if (lbcBinder == surfaceTextureBinder) {
381 return true;
382 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800383 }
384 }
385
386 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700387 // SurfaceTexture gets destroyed before all the clients are done using it,
388 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800389 // will instead fail later on when the client tries to use the surface,
390 // which should be reported as "surface XYZ returned an -ENODEV". The
391 // purgatorized layers are no less authentic than the visible ones, so this
392 // should not cause any harm.
393 size_t purgatorySize = mLayerPurgatory.size();
394 for (size_t i=0 ; i<purgatorySize ; i++) {
395 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
396 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700397 if (lbc != NULL) {
398 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
399 if (lbcBinder == surfaceTextureBinder) {
400 return true;
401 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800402 }
403 }
404
405 return false;
406}
407
Mathias Agopianbb641242010-05-18 17:06:55 -0700408status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
409 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410{
Mathias Agopianbb641242010-05-18 17:06:55 -0700411 return mEventQueue.postMessage(msg, reltime, flags);
412}
413
414status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
415 nsecs_t reltime, uint32_t flags)
416{
417 status_t res = mEventQueue.postMessage(msg, reltime, flags);
418 if (res == NO_ERROR) {
419 msg->wait();
420 }
421 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
423
424// ----------------------------------------------------------------------------
425#if 0
426#pragma mark -
427#pragma mark Main loop
428#endif
429
430bool SurfaceFlinger::threadLoop()
431{
432 waitForEvent();
433
434 // check for transactions
435 if (UNLIKELY(mConsoleSignals)) {
436 handleConsoleEvents();
437 }
438
Mathias Agopian698c0872011-06-28 19:09:31 -0700439 // if we're in a global transaction, don't do anything.
440 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
441 uint32_t transactionFlags = peekTransactionFlags(mask);
442 if (UNLIKELY(transactionFlags)) {
443 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444 }
445
446 // post surfaces (if needed)
447 handlePageFlip();
448
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700449 if (mDirtyRegion.isEmpty()) {
450 // nothing new to do.
451 return true;
452 }
453
Mathias Agopiana350ff92010-08-10 17:14:02 -0700454 if (UNLIKELY(mHwWorkListDirty)) {
455 // build the h/w work list
456 handleWorkList();
457 }
458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800459 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana44b0412011-10-16 18:46:35 -0700460 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700462
463 const int index = hw.getCurrentBufferIndex();
464 GraphicLog& logger(GraphicLog::getInstance());
465
466 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 handleRepaint();
468
Mathias Agopian74faca22009-09-17 16:18:16 -0700469 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700470 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700471 hw.compositionComplete();
472
Mathias Agopian35b48d12010-09-13 22:57:58 -0700473 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700474 postFramebuffer();
475
Mathias Agopian35b48d12010-09-13 22:57:58 -0700476 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 } else {
478 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800479 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 usleep(16667); // 60 fps period
481 }
482 return true;
483}
484
485void SurfaceFlinger::postFramebuffer()
486{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700487 // this should never happen. we do the flip anyways so we don't
488 // risk to cause a deadlock with hwc
489 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700490 const DisplayHardware& hw(graphicPlane(0).displayHardware());
491 const nsecs_t now = systemTime();
492 mDebugInSwapBuffers = now;
493 hw.flip(mSwapRegion);
494 mLastSwapBufferTime = systemTime() - now;
495 mDebugInSwapBuffers = 0;
496 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497}
498
499void SurfaceFlinger::handleConsoleEvents()
500{
501 // something to do with the console
502 const DisplayHardware& hw = graphicPlane(0).displayHardware();
503
504 int what = android_atomic_and(0, &mConsoleSignals);
505 if (what & eConsoleAcquired) {
506 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700507 // this is a temporary work-around, eventually this should be called
508 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700509 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 }
511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700513 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 }
516 }
517
518 mDirtyRegion.set(hw.bounds());
519}
520
521void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
522{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700523 Mutex::Autolock _l(mStateLock);
524 const nsecs_t now = systemTime();
525 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526
Mathias Agopianca4d3602011-05-19 15:38:14 -0700527 // Here we're guaranteed that some transaction flags are set
528 // so we can call handleTransactionLocked() unconditionally.
529 // We call getTransactionFlags(), which will also clear the flags,
530 // with mStateLock held to guarantee that mCurrentState won't change
531 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700532
Mathias Agopianca4d3602011-05-19 15:38:14 -0700533 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
534 transactionFlags = getTransactionFlags(mask);
535 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700536
Mathias Agopianca4d3602011-05-19 15:38:14 -0700537 mLastTransactionTime = systemTime() - now;
538 mDebugInTransaction = 0;
539 invalidateHwcGeometry();
540 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700541}
542
Mathias Agopianca4d3602011-05-19 15:38:14 -0700543void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700544{
545 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 const size_t count = currentLayers.size();
547
548 /*
549 * Traversal of the children
550 * (perform the transaction for each of them if needed)
551 */
552
553 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
554 if (layersNeedTransaction) {
555 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700556 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
558 if (!trFlags) continue;
559
560 const uint32_t flags = layer->doTransaction(0);
561 if (flags & Layer::eVisibleRegion)
562 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 }
564 }
565
566 /*
567 * Perform our own transaction if needed
568 */
569
570 if (transactionFlags & eTransactionNeeded) {
571 if (mCurrentState.orientation != mDrawingState.orientation) {
572 // the orientation has changed, recompute all visible regions
573 // and invalidate everything.
574
575 const int dpy = 0;
576 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700577 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 GraphicPlane& plane(graphicPlane(dpy));
579 plane.setOrientation(orientation);
580
581 // update the shared control block
582 const DisplayHardware& hw(plane.displayHardware());
583 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
584 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800585 dcblk->w = plane.getWidth();
586 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587
588 mVisibleRegionsDirty = true;
589 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 }
591
592 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
593 // freezing or unfreezing the display -> trigger animation if needed
594 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian064e1e62010-03-01 17:51:17 -0800595 if (mFreezeDisplay)
596 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 }
598
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700599 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
600 // layers have been added
601 mVisibleRegionsDirty = true;
602 }
603
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 // some layers might have been removed, so
605 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700606 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700607 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700609 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700610 const size_t count = previousLayers.size();
611 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700612 const sp<LayerBase>& layer(previousLayers[i]);
613 if (currentLayers.indexOf( layer ) < 0) {
614 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700615 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700616 }
617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 }
620
621 commitTransaction();
622}
623
624sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
625{
626 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
627}
628
629void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800630 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631{
632 const GraphicPlane& plane(graphicPlane(0));
633 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700634 const DisplayHardware& hw(plane.displayHardware());
635 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636
637 Region aboveOpaqueLayers;
638 Region aboveCoveredLayers;
639 Region dirty;
640
641 bool secureFrameBuffer = false;
642
643 size_t i = currentLayers.size();
644 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 layer->validateVisibility(planeTransform);
647
648 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700649 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopianab028732010-03-16 16:41:46 -0700651 /*
652 * opaqueRegion: area of a surface that is fully opaque.
653 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700655
656 /*
657 * visibleRegion: area of a surface that is visible on screen
658 * and not fully transparent. This is essentially the layer's
659 * footprint minus the opaque regions above it.
660 * Areas covered by a translucent surface are considered visible.
661 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700663
664 /*
665 * coveredRegion: area of a surface that is covered by all
666 * visible regions above it (which includes the translucent areas).
667 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700669
670
671 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700672 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700673 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700674 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700676 visibleRegion.andSelf(screenRegion);
677 if (!visibleRegion.isEmpty()) {
678 // Remove the transparent area from the visible region
679 if (translucent) {
680 visibleRegion.subtractSelf(layer->transparentRegionScreen);
681 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682
Mathias Agopianab028732010-03-16 16:41:46 -0700683 // compute the opaque region
684 const int32_t layerOrientation = layer->getOrientation();
685 if (s.alpha==255 && !translucent &&
686 ((layerOrientation & Transform::ROT_INVALID) == false)) {
687 // the opaque region is the layer's footprint
688 opaqueRegion = visibleRegion;
689 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 }
691 }
692
Mathias Agopianab028732010-03-16 16:41:46 -0700693 // Clip the covered region to the visible region
694 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
695
696 // Update aboveCoveredLayers for next (lower) layer
697 aboveCoveredLayers.orSelf(visibleRegion);
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 // subtract the opaque region covered by the layers above us
700 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701
702 // compute this layer's dirty region
703 if (layer->contentDirty) {
704 // we need to invalidate the whole region
705 dirty = visibleRegion;
706 // as well, as the old visible region
707 dirty.orSelf(layer->visibleRegionScreen);
708 layer->contentDirty = false;
709 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700710 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700711 * the exposed region consists of two components:
712 * 1) what's VISIBLE now and was COVERED before
713 * 2) what's EXPOSED now less what was EXPOSED before
714 *
715 * note that (1) is conservative, we start with the whole
716 * visible region but only keep what used to be covered by
717 * something -- which mean it may have been exposed.
718 *
719 * (2) handles areas that were not covered by anything but got
720 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700721 */
Mathias Agopianab028732010-03-16 16:41:46 -0700722 const Region newExposed = visibleRegion - coveredRegion;
723 const Region oldVisibleRegion = layer->visibleRegionScreen;
724 const Region oldCoveredRegion = layer->coveredRegionScreen;
725 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
726 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 }
728 dirty.subtractSelf(aboveOpaqueLayers);
729
730 // accumulate to the screen dirty region
731 dirtyRegion.orSelf(dirty);
732
Mathias Agopianab028732010-03-16 16:41:46 -0700733 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700735
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 // Store the visible region is screen space
737 layer->setVisibleRegion(visibleRegion);
738 layer->setCoveredRegion(coveredRegion);
739
Mathias Agopian97011222009-07-28 10:57:27 -0700740 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 if (layer->isSecure() && !visibleRegion.isEmpty()) {
742 secureFrameBuffer = true;
743 }
744 }
745
Mathias Agopian97011222009-07-28 10:57:27 -0700746 // invalidate the areas where a layer was removed
747 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
748 mDirtyRegionRemovedLayer.clear();
749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 mSecureFrameBuffer = secureFrameBuffer;
751 opaqueRegion = aboveOpaqueLayers;
752}
753
754
755void SurfaceFlinger::commitTransaction()
756{
757 mDrawingState = mCurrentState;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700758 mResizeTransationPending = false;
759 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760}
761
762void SurfaceFlinger::handlePageFlip()
763{
764 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800765 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 visibleRegions |= lockPageFlip(currentLayers);
767
768 const DisplayHardware& hw = graphicPlane(0).displayHardware();
769 const Region screenRegion(hw.bounds());
770 if (visibleRegions) {
771 Region opaqueRegion;
772 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700773
774 /*
775 * rebuild the visible layer list
776 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800777 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700778 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700779 mVisibleLayersSortedByZ.setCapacity(count);
780 for (size_t i=0 ; i<count ; i++) {
781 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
782 mVisibleLayersSortedByZ.add(currentLayers[i]);
783 }
784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 mWormholeRegion = screenRegion.subtract(opaqueRegion);
786 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800787 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 }
789
790 unlockPageFlip(currentLayers);
791 mDirtyRegion.andSelf(screenRegion);
792}
793
Mathias Agopianad456f92011-01-13 17:53:01 -0800794void SurfaceFlinger::invalidateHwcGeometry()
795{
796 mHwWorkListDirty = true;
797}
798
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
800{
801 bool recomputeVisibleRegions = false;
802 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700803 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700805 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 layer->lockPageFlip(recomputeVisibleRegions);
807 }
808 return recomputeVisibleRegions;
809}
810
811void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
812{
813 const GraphicPlane& plane(graphicPlane(0));
814 const Transform& planeTransform(plane.transform());
815 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700816 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700818 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 layer->unlockPageFlip(planeTransform, mDirtyRegion);
820 }
821}
822
Mathias Agopiana350ff92010-08-10 17:14:02 -0700823void SurfaceFlinger::handleWorkList()
824{
825 mHwWorkListDirty = false;
826 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
827 if (hwc.initCheck() == NO_ERROR) {
828 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
829 const size_t count = currentLayers.size();
830 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700831 hwc_layer_t* const cur(hwc.getLayers());
832 for (size_t i=0 ; cur && i<count ; i++) {
833 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700834 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700835 cur[i].compositionType = HWC_FRAMEBUFFER;
836 cur[i].flags |= HWC_SKIP_LAYER;
837 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700838 }
839 }
840}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700841
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842void SurfaceFlinger::handleRepaint()
843{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700844 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700845 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846
847 if (UNLIKELY(mDebugRegion)) {
848 debugFlashRegions();
849 }
850
Mathias Agopianb8a55602009-06-26 19:06:36 -0700851 // set the frame buffer
852 const DisplayHardware& hw(graphicPlane(0).displayHardware());
853 glMatrixMode(GL_MODELVIEW);
854 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855
856 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700857 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
858 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700859 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700860 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
861 // takes a rectangle, we must make sure to update that whole
862 // rectangle in that case
863 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700864 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700865 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700866 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700868 // in the BUFFER_PRESERVED case, obviously, we can update only
869 // what's needed and nothing more.
870 // NOTE: this is NOT a common case, as preserving the backbuffer
871 // is costly and usually involves copying the whole update back.
872 }
873 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700874 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700875 // We need to redraw the rectangle that will be updated
876 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700877 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700878 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700879 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700880 } else {
881 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700883 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 }
885 }
886
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700887 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800888 composeSurfaces(mDirtyRegion);
889
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700890 // update the swap region and clear the dirty region
891 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 mDirtyRegion.clear();
893}
894
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700895void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700897 const DisplayHardware& hw(graphicPlane(0).displayHardware());
898 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700899 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700901 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700902 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700903
Mathias Agopianf384cc32011-09-08 18:31:55 -0700904 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
905 size_t count = layers.size();
906
907 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700908 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
909 hwc.getNumLayers(), count);
910
911 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700912 if (hwc.initCheck() == NO_ERROR) {
913 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
914 }
Mathias Agopian45721772010-08-12 15:03:26 -0700915
916 /*
917 * update the per-frame h/w composer data for each layer
918 * and build the transparent region of the FB
919 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700920 for (size_t i=0 ; i<count ; i++) {
921 const sp<LayerBase>& layer(layers[i]);
922 layer->setPerFrameData(&cur[i]);
923 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700924 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700925 status_t err = hwc.prepare();
926 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700927
Mathias Agopianf384cc32011-09-08 18:31:55 -0700928 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700929 // what's happening here is tricky.
930 // we want to clear all the layers with the CLEAR_FB flags
931 // that are opaque.
932 // however, since some GPU are efficient at preserving
933 // the backbuffer, we want to take advantage of that so we do the
934 // clear only in the dirty region (other areas will be preserved
935 // on those GPUs).
936 // NOTE: on non backbuffer preserving GPU, the dirty region
937 // has already been expanded as needed, so the code is correct
938 // there too.
939 //
940 // However, the content of the framebuffer cannot be trusted when
941 // we switch to/from FB/OVERLAY, in which case we need to
942 // expand the dirty region to those areas too.
943 //
944 // Note also that there is a special case when switching from
945 // "no layers in FB" to "some layers in FB", where we need to redraw
946 // the entire FB, since some areas might contain uninitialized
947 // data.
948 //
949 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700950 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700951 // that is, we can't erase anything outside the dirty region.
952
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700953 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700954
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700955 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
956 transparent.set(hw.getBounds());
957 dirtyInOut = transparent;
958 } else {
959 for (size_t i=0 ; i<count ; i++) {
960 const sp<LayerBase>& layer(layers[i]);
961 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
962 transparent.orSelf(layer->visibleRegionScreen);
963 }
964 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
965 if (isOverlay != layer->isOverlay()) {
966 // we transitioned to/from overlay, so add this layer
967 // to the dirty region so the framebuffer can be either
968 // cleared or redrawn.
969 dirtyInOut.orSelf(layer->visibleRegionScreen);
970 }
971 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800972 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700973 // don't erase stuff outside the dirty region
974 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700975 }
976
977 /*
978 * clear the area of the FB that need to be transparent
979 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700980 if (!transparent.isEmpty()) {
981 glClearColor(0,0,0,0);
982 Region::const_iterator it = transparent.begin();
983 Region::const_iterator const end = transparent.end();
984 const int32_t height = hw.getHeight();
985 while (it != end) {
986 const Rect& r(*it++);
987 const GLint sy = height - (r.top + r.height());
988 glScissor(r.left, sy, r.width(), r.height());
989 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800990 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700991 }
992 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700993}
Mathias Agopian45721772010-08-12 15:03:26 -0700994
Mathias Agopianf384cc32011-09-08 18:31:55 -0700995void SurfaceFlinger::composeSurfaces(const Region& dirty)
996{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700997 const DisplayHardware& hw(graphicPlane(0).displayHardware());
998 HWComposer& hwc(hw.getHwComposer());
999
1000 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
1001 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001002 // should never happen unless the window manager has a bug
1003 // draw something...
1004 drawWormhole();
1005 }
1006
Mathias Agopian45721772010-08-12 15:03:26 -07001007 /*
1008 * and then, render the layers targeted at the framebuffer
1009 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001010 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -07001011 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1012 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -07001013 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001014 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -07001015 continue;
Mathias Agopian45721772010-08-12 15:03:26 -07001016 }
1017 const sp<LayerBase>& layer(layers[i]);
1018 const Region clip(dirty.intersect(layer->visibleRegionScreen));
1019 if (!clip.isEmpty()) {
1020 layer->draw(clip);
1021 }
1022 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023}
1024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025void SurfaceFlinger::debugFlashRegions()
1026{
Mathias Agopian0a917752010-06-14 21:20:00 -07001027 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1028 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -07001029 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001030 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001031 return;
1032 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001033
Mathias Agopian0a917752010-06-14 21:20:00 -07001034 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1035 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1036 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1037 mDirtyRegion.bounds() : hw.bounds());
1038 composeSurfaces(repaint);
1039 }
1040
Mathias Agopianc492e672011-10-18 14:49:27 -07001041 glDisable(GL_TEXTURE_EXTERNAL_OES);
1042 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001043 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 glDisable(GL_SCISSOR_TEST);
1045
Mathias Agopian0926f502009-05-04 14:17:04 -07001046 static int toggle = 0;
1047 toggle = 1 - toggle;
1048 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001049 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001050 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001051 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001052 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053
Mathias Agopian20f68782009-05-11 00:03:41 -07001054 Region::const_iterator it = mDirtyRegion.begin();
1055 Region::const_iterator const end = mDirtyRegion.end();
1056 while (it != end) {
1057 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001059 { r.left, height - r.top },
1060 { r.left, height - r.bottom },
1061 { r.right, height - r.bottom },
1062 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 };
1064 glVertexPointer(2, GL_FLOAT, 0, vertices);
1065 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1066 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001067
Mathias Agopian0656a682011-09-20 17:22:44 -07001068 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069
1070 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001071 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072
1073 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074}
1075
1076void SurfaceFlinger::drawWormhole() const
1077{
1078 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1079 if (region.isEmpty())
1080 return;
1081
1082 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1083 const int32_t width = hw.getWidth();
1084 const int32_t height = hw.getHeight();
1085
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001087 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001088 Region::const_iterator it = region.begin();
1089 Region::const_iterator const end = region.end();
1090 while (it != end) {
1091 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 const GLint sy = height - (r.top + r.height());
1093 glScissor(r.left, sy, r.width(), r.height());
1094 glClear(GL_COLOR_BUFFER_BIT);
1095 }
1096 } else {
1097 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1098 { width, height }, { 0, height } };
1099 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 glVertexPointer(2, GL_SHORT, 0, vertices);
1102 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1103 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001104
1105 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 glEnable(GL_TEXTURE_2D);
1107 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1108 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1109 glMatrixMode(GL_TEXTURE);
1110 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001111
1112 glDisable(GL_BLEND);
1113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001115 Region::const_iterator it = region.begin();
1116 Region::const_iterator const end = region.end();
1117 while (it != end) {
1118 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001119 const GLint sy = height - (r.top + r.height());
1120 glScissor(r.left, sy, r.width(), r.height());
1121 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1122 }
1123 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001124 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001125 glLoadIdentity();
1126 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 }
1128}
1129
1130void SurfaceFlinger::debugShowFPS() const
1131{
1132 static int mFrameCount;
1133 static int mLastFrameCount = 0;
1134 static nsecs_t mLastFpsTime = 0;
1135 static float mFps = 0;
1136 mFrameCount++;
1137 nsecs_t now = systemTime();
1138 nsecs_t diff = now - mLastFpsTime;
1139 if (diff > ms2ns(250)) {
1140 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1141 mLastFpsTime = now;
1142 mLastFrameCount = mFrameCount;
1143 }
1144 // XXX: mFPS has the value we want
1145 }
1146
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001147status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148{
1149 Mutex::Autolock _l(mStateLock);
1150 addLayer_l(layer);
1151 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1152 return NO_ERROR;
1153}
1154
Mathias Agopian96f08192010-06-02 23:28:45 -07001155status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1156{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001157 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001158 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1159}
1160
1161ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1162 const sp<LayerBaseClient>& lbc)
1163{
Mathias Agopian96f08192010-06-02 23:28:45 -07001164 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001165 size_t name = client->attachLayer(lbc);
1166
1167 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001168
1169 // add this layer to the current state list
1170 addLayer_l(lbc);
1171
Mathias Agopian4f113742011-05-03 16:21:41 -07001172 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001173}
1174
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001175status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176{
1177 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001178 status_t err = purgatorizeLayer_l(layer);
1179 if (err == NO_ERROR)
1180 setTransactionFlags(eTransactionNeeded);
1181 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182}
1183
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001184status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001186 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1187 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001188 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001189 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1191 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001192 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 return NO_ERROR;
1194 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001195 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196}
1197
Mathias Agopian9a112062009-04-17 19:36:26 -07001198status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1199{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001200 // First add the layer to the purgatory list, which makes sure it won't
1201 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001202 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001203 if (err >= 0) {
1204 mLayerPurgatory.add(layerBase);
1205 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001206
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001207 layerBase->onRemoved();
1208
Mathias Agopian3d579642009-06-04 18:46:21 -07001209 // it's possible that we don't find a layer, because it might
1210 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001211 // from the user because there is a race between Client::destroySurface(),
1212 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001213 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1214}
1215
Mathias Agopian96f08192010-06-02 23:28:45 -07001216status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217{
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 layer->forceVisibilityTransaction();
1219 setTransactionFlags(eTraversalNeeded);
1220 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221}
1222
Mathias Agopiandea20b12011-05-03 17:04:02 -07001223uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1224{
1225 return android_atomic_release_load(&mTransactionFlags);
1226}
1227
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1229{
1230 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1231}
1232
Mathias Agopianbb641242010-05-18 17:06:55 -07001233uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234{
1235 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1236 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001237 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 }
1239 return old;
1240}
1241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001243void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
1244 int orientation) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001245 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001246
Mathias Agopian698c0872011-06-28 19:09:31 -07001247 uint32_t flags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001248 if (mCurrentState.orientation != orientation) {
1249 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1250 mCurrentState.orientation = orientation;
1251 flags |= eTransactionNeeded;
1252 mResizeTransationPending = true;
1253 } else if (orientation != eOrientationUnchanged) {
1254 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1255 orientation);
1256 }
1257 }
1258
Mathias Agopian698c0872011-06-28 19:09:31 -07001259 const size_t count = state.size();
1260 for (size_t i=0 ; i<count ; i++) {
1261 const ComposerState& s(state[i]);
1262 sp<Client> client( static_cast<Client *>(s.client.get()) );
1263 flags |= setClientStateLocked(client, s.state);
1264 }
1265 if (flags) {
1266 setTransactionFlags(flags);
1267 }
1268
1269 signalEvent();
1270
1271 // if there is a transaction with a resize, wait for it to
1272 // take effect before returning.
1273 while (mResizeTransationPending) {
1274 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1275 if (CC_UNLIKELY(err != NO_ERROR)) {
1276 // just in case something goes wrong in SF, return to the
1277 // called after a few seconds.
1278 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1279 mResizeTransationPending = false;
1280 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001281 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 }
1283}
1284
1285status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1286{
1287 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1288 return BAD_VALUE;
1289
1290 Mutex::Autolock _l(mStateLock);
1291 mCurrentState.freezeDisplay = 1;
1292 setTransactionFlags(eTransactionNeeded);
1293
1294 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001295 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 return NO_ERROR;
1297}
1298
1299status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1300{
1301 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1302 return BAD_VALUE;
1303
1304 Mutex::Autolock _l(mStateLock);
1305 mCurrentState.freezeDisplay = 0;
1306 setTransactionFlags(eTransactionNeeded);
1307
1308 // flags is intended to communicate some sort of animation behavior
Mathias Agopian62b74442009-04-14 23:02:51 -07001309 // (for instance fading)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 return NO_ERROR;
1311}
1312
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001313int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001314 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315{
1316 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1317 return BAD_VALUE;
1318
1319 Mutex::Autolock _l(mStateLock);
1320 if (mCurrentState.orientation != orientation) {
1321 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001322 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 mCurrentState.orientation = orientation;
1324 setTransactionFlags(eTransactionNeeded);
1325 mTransactionCV.wait(mStateLock);
1326 } else {
1327 orientation = BAD_VALUE;
1328 }
1329 }
1330 return orientation;
1331}
1332
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001333sp<ISurface> SurfaceFlinger::createSurface(
1334 ISurfaceComposerClient::surface_data_t* params,
1335 const String8& name,
1336 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1338 uint32_t flags)
1339{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001340 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001341 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001342
1343 if (int32_t(w|h) < 0) {
1344 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1345 int(w), int(h));
1346 return surfaceHandle;
1347 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001350 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 switch (flags & eFXSurfaceMask) {
1352 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001353 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1354 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 break;
1356 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001357 // for now we treat Blur as Dim, until we can implement it
1358 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001360 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001362 case eFXSurfaceScreenshot:
1363 layer = createScreenshotSurface(client, d, w, h, flags);
1364 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 }
1366
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001367 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001368 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001369 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001373 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001374 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001375 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001376 if (normalLayer != 0) {
1377 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001378 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001379 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001380 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001381
Mathias Agopian96f08192010-06-02 23:28:45 -07001382 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 }
1384
1385 return surfaceHandle;
1386}
1387
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001388sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001389 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001390 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001391 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392{
1393 // initialize the surfaces
1394 switch (format) { // TODO: take h/w into account
1395 case PIXEL_FORMAT_TRANSPARENT:
1396 case PIXEL_FORMAT_TRANSLUCENT:
1397 format = PIXEL_FORMAT_RGBA_8888;
1398 break;
1399 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001400#ifdef NO_RGBX_8888
1401 format = PIXEL_FORMAT_RGB_565;
1402#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001403 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001404#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 break;
1406 }
1407
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001408#ifdef NO_RGBX_8888
1409 if (format == PIXEL_FORMAT_RGBX_8888)
1410 format = PIXEL_FORMAT_RGBA_8888;
1411#endif
1412
Mathias Agopian96f08192010-06-02 23:28:45 -07001413 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001414 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001415 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001417 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 }
1419 return layer;
1420}
1421
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001422sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001423 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001424 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425{
Mathias Agopian96f08192010-06-02 23:28:45 -07001426 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001427 return layer;
1428}
1429
1430sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1431 const sp<Client>& client, DisplayID display,
1432 uint32_t w, uint32_t h, uint32_t flags)
1433{
1434 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
1435 status_t err = layer->capture();
1436 if (err != NO_ERROR) {
1437 layer.clear();
1438 LOGW("createScreenshotSurface failed (%s)", strerror(-err));
1439 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 return layer;
1441}
1442
Mathias Agopian96f08192010-06-02 23:28:45 -07001443status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444{
Mathias Agopian9a112062009-04-17 19:36:26 -07001445 /*
1446 * called by the window manager, when a surface should be marked for
1447 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001448 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001449 * The surface is removed from the current and drawing lists, but placed
1450 * in the purgatory queue, so it's not destroyed right-away (we need
1451 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001452 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001453
Mathias Agopian48d819a2009-09-10 19:41:18 -07001454 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001455 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001456 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001457 if (layer != 0) {
1458 err = purgatorizeLayer_l(layer);
1459 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001460 setTransactionFlags(eTransactionNeeded);
1461 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001462 }
1463 return err;
1464}
1465
Mathias Agopianca4d3602011-05-19 15:38:14 -07001466status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001467{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001468 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001469 status_t err = NO_ERROR;
1470 sp<LayerBaseClient> l(layer.promote());
1471 if (l != NULL) {
1472 Mutex::Autolock _l(mStateLock);
1473 err = removeLayer_l(l);
1474 if (err == NAME_NOT_FOUND) {
1475 // The surface wasn't in the current list, which means it was
1476 // removed already, which means it is in the purgatory,
1477 // and need to be removed from there.
1478 ssize_t idx = mLayerPurgatory.remove(l);
1479 LOGE_IF(idx < 0,
1480 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001481 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001482 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1483 "error removing layer=%p (%s)", l.get(), strerror(-err));
1484 }
1485 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486}
1487
Mathias Agopian698c0872011-06-28 19:09:31 -07001488uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001489 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001490 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001493 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1494 if (layer != 0) {
1495 const uint32_t what = s.what;
1496 if (what & ePositionChanged) {
1497 if (layer->setPosition(s.x, s.y))
1498 flags |= eTraversalNeeded;
1499 }
1500 if (what & eLayerChanged) {
1501 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1502 if (layer->setLayer(s.z)) {
1503 mCurrentState.layersSortedByZ.removeAt(idx);
1504 mCurrentState.layersSortedByZ.add(layer);
1505 // we need traversal (state changed)
1506 // AND transaction (list changed)
1507 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 }
1509 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001510 if (what & eSizeChanged) {
1511 if (layer->setSize(s.w, s.h)) {
1512 flags |= eTraversalNeeded;
1513 mResizeTransationPending = true;
1514 }
1515 }
1516 if (what & eAlphaChanged) {
1517 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1518 flags |= eTraversalNeeded;
1519 }
1520 if (what & eMatrixChanged) {
1521 if (layer->setMatrix(s.matrix))
1522 flags |= eTraversalNeeded;
1523 }
1524 if (what & eTransparentRegionChanged) {
1525 if (layer->setTransparentRegionHint(s.transparentRegion))
1526 flags |= eTraversalNeeded;
1527 }
1528 if (what & eVisibilityChanged) {
1529 if (layer->setFlags(s.flags, s.mask))
1530 flags |= eTraversalNeeded;
1531 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001533 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001534}
1535
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536void SurfaceFlinger::screenReleased(int dpy)
1537{
1538 // this may be called by a signal handler, we can't do too much in here
1539 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1540 signalEvent();
1541}
1542
1543void SurfaceFlinger::screenAcquired(int dpy)
1544{
1545 // this may be called by a signal handler, we can't do too much in here
1546 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1547 signalEvent();
1548}
1549
1550status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1551{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001552 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553 char buffer[SIZE];
1554 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001555
1556 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 snprintf(buffer, SIZE, "Permission Denial: "
1558 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1559 IPCThreadState::self()->getCallingPid(),
1560 IPCThreadState::self()->getCallingUid());
1561 result.append(buffer);
1562 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001563
1564 // figure out if we're stuck somewhere
1565 const nsecs_t now = systemTime();
1566 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1567 const nsecs_t inTransaction(mDebugInTransaction);
1568 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1569 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1570
1571 // Try to get the main lock, but don't insist if we can't
1572 // (this would indicate SF is stuck, but we want to be able to
1573 // print something in dumpsys).
1574 int retry = 3;
1575 while (mStateLock.tryLock()<0 && --retry>=0) {
1576 usleep(1000000);
1577 }
1578 const bool locked(retry >= 0);
1579 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001580 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001581 "SurfaceFlinger appears to be unresponsive, "
1582 "dumping anyways (no locks held)\n");
1583 result.append(buffer);
1584 }
1585
Mathias Agopian48b888a2011-01-19 16:15:53 -08001586 /*
1587 * Dump the visible layer list
1588 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1590 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001591 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1592 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001594 const sp<LayerBase>& layer(currentLayers[i]);
1595 layer->dump(result, buffer, SIZE);
1596 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 s.transparentRegion.dump(result, "transparentRegion");
1598 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1599 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1600 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001601
Mathias Agopian48b888a2011-01-19 16:15:53 -08001602 /*
1603 * Dump the layers in the purgatory
1604 */
1605
1606 const size_t purgatorySize = mLayerPurgatory.size();
1607 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1608 result.append(buffer);
1609 for (size_t i=0 ; i<purgatorySize ; i++) {
1610 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1611 layer->shortDump(result, buffer, SIZE);
1612 }
1613
1614 /*
1615 * Dump SurfaceFlinger global state
1616 */
1617
Mathias Agopianad701862011-07-14 18:01:49 -07001618 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001619 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001620
1621 const GLExtensions& extensions(GLExtensions::getInstance());
1622 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1623 extensions.getVendor(),
1624 extensions.getRenderer(),
1625 extensions.getVersion());
1626 result.append(buffer);
1627 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1628 result.append(buffer);
1629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 mWormholeRegion.dump(result, "WormholeRegion");
1631 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1632 snprintf(buffer, SIZE,
1633 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1634 mFreezeDisplay?"yes":"no", mFreezeCount,
1635 mCurrentState.orientation, hw.canDraw());
1636 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001637 snprintf(buffer, SIZE,
1638 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001639 " last transaction time : %f us\n"
1640 " refresh-rate : %f fps\n"
1641 " x-dpi : %f\n"
1642 " y-dpi : %f\n",
1643 mLastSwapBufferTime/1000.0,
1644 mLastTransactionTime/1000.0,
1645 hw.getRefreshRate(),
1646 hw.getDpiX(),
1647 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001648 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001649
Mathias Agopian9795c422009-08-26 16:36:26 -07001650 if (inSwapBuffersDuration || !locked) {
1651 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1652 inSwapBuffersDuration/1000.0);
1653 result.append(buffer);
1654 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001655
Mathias Agopian9795c422009-08-26 16:36:26 -07001656 if (inTransactionDuration || !locked) {
1657 snprintf(buffer, SIZE, " transaction time: %f us\n",
1658 inTransactionDuration/1000.0);
1659 result.append(buffer);
1660 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001661
Mathias Agopian48b888a2011-01-19 16:15:53 -08001662 /*
1663 * Dump HWComposer state
1664 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001665 HWComposer& hwc(hw.getHwComposer());
1666 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1667 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001668 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001669 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001670 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001671
Mathias Agopian48b888a2011-01-19 16:15:53 -08001672 /*
1673 * Dump gralloc state
1674 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001675 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001676 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001677 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001678
1679 if (locked) {
1680 mStateLock.unlock();
1681 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 }
1683 write(fd, result.string(), result.size());
1684 return NO_ERROR;
1685}
1686
1687status_t SurfaceFlinger::onTransact(
1688 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1689{
1690 switch (code) {
1691 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001692 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 case SET_ORIENTATION:
1694 case FREEZE_DISPLAY:
1695 case UNFREEZE_DISPLAY:
1696 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001697 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001698 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699 {
1700 // codes that require permission check
1701 IPCThreadState* ipc = IPCThreadState::self();
1702 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001703 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001704 if ((uid != AID_GRAPHICS) &&
1705 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001706 LOGE("Permission Denial: "
1707 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1708 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001710 break;
1711 }
1712 case CAPTURE_SCREEN:
1713 {
1714 // codes that require permission check
1715 IPCThreadState* ipc = IPCThreadState::self();
1716 const int pid = ipc->getCallingPid();
1717 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001718 if ((uid != AID_GRAPHICS) &&
1719 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001720 LOGE("Permission Denial: "
1721 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1722 return PERMISSION_DENIED;
1723 }
1724 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 }
1726 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1729 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001730 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001731 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001732 IPCThreadState* ipc = IPCThreadState::self();
1733 const int pid = ipc->getCallingPid();
1734 const int uid = ipc->getCallingUid();
1735 LOGE("Permission Denial: "
1736 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 return PERMISSION_DENIED;
1738 }
1739 int n;
1740 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001741 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001742 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 return NO_ERROR;
1744 case 1002: // SHOW_UPDATES
1745 n = data.readInt32();
1746 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001747 invalidateHwcGeometry();
1748 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 return NO_ERROR;
1750 case 1003: // SHOW_BACKGROUND
1751 n = data.readInt32();
1752 mDebugBackground = n ? 1 : 0;
1753 return NO_ERROR;
1754 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001755 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001756 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001758 case 1005:{ // force transaction
1759 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1760 return NO_ERROR;
1761 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001762 case 1006:{ // enable/disable GraphicLog
1763 int enabled = data.readInt32();
1764 GraphicLog::getInstance().setEnabled(enabled);
1765 return NO_ERROR;
1766 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 case 1007: // set mFreezeCount
1768 mFreezeCount = data.readInt32();
Mathias Agopian04087722009-12-01 17:23:28 -08001769 mFreezeDisplayTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001770 return NO_ERROR;
Mathias Agopian53331da2011-08-22 21:44:41 -07001771 case 1008: // toggle use of hw composer
1772 n = data.readInt32();
1773 mDebugDisableHWC = n ? 1 : 0;
1774 invalidateHwcGeometry();
1775 repaintEverything();
1776 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001777 case 1009: // toggle use of transform hint
1778 n = data.readInt32();
1779 mDebugDisableTransformHint = n ? 1 : 0;
1780 invalidateHwcGeometry();
1781 repaintEverything();
1782 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001784 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 reply->writeInt32(0);
1786 reply->writeInt32(mDebugRegion);
1787 reply->writeInt32(mDebugBackground);
1788 return NO_ERROR;
1789 case 1013: {
1790 Mutex::Autolock _l(mStateLock);
1791 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1792 reply->writeInt32(hw.getPageFlipCount());
1793 }
1794 return NO_ERROR;
1795 }
1796 }
1797 return err;
1798}
1799
Mathias Agopian53331da2011-08-22 21:44:41 -07001800void SurfaceFlinger::repaintEverything() {
1801 Mutex::Autolock _l(mStateLock);
1802 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian3a3cad32011-10-18 17:36:28 -07001803 mDirtyRegion.set(hw.bounds());
Mathias Agopian53331da2011-08-22 21:44:41 -07001804 signalEvent();
1805}
1806
Mathias Agopian59119e62010-10-11 12:37:43 -07001807// ---------------------------------------------------------------------------
1808
Mathias Agopian118d0242011-10-13 16:02:48 -07001809status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1810 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1811{
1812 Mutex::Autolock _l(mStateLock);
1813 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1814}
1815
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001816status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1817 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001818{
Mathias Agopian59119e62010-10-11 12:37:43 -07001819 if (!GLExtensions::getInstance().haveFramebufferObject())
1820 return INVALID_OPERATION;
1821
1822 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001823 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001824 const uint32_t hw_w = hw.getWidth();
1825 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001826 GLfloat u = 1;
1827 GLfloat v = 1;
1828
1829 // make sure to clear all GL error flags
1830 while ( glGetError() != GL_NO_ERROR ) ;
1831
1832 // create a FBO
1833 GLuint name, tname;
1834 glGenTextures(1, &tname);
1835 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1837 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001838 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001839 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001840 GLint tw = (2 << (31 - clz(hw_w)));
1841 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1843 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001844 u = GLfloat(hw_w) / tw;
1845 v = GLfloat(hw_h) / th;
1846 }
1847 glGenFramebuffersOES(1, &name);
1848 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001849 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1850 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001851
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001852 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001853 glDisable(GL_TEXTURE_EXTERNAL_OES);
1854 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001855 glClearColor(0,0,0,1);
1856 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001857 glMatrixMode(GL_MODELVIEW);
1858 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001859 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1860 const size_t count = layers.size();
1861 for (size_t i=0 ; i<count ; ++i) {
1862 const sp<LayerBase>& layer(layers[i]);
1863 layer->drawForSreenShot();
1864 }
1865
Mathias Agopian118d0242011-10-13 16:02:48 -07001866 hw.compositionComplete();
1867
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868 // back to main framebuffer
1869 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1870 glDisable(GL_SCISSOR_TEST);
1871 glDeleteFramebuffersOES(1, &name);
1872
1873 *textureName = tname;
1874 *uOut = u;
1875 *vOut = v;
1876 return NO_ERROR;
1877}
1878
1879// ---------------------------------------------------------------------------
1880
1881status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1882{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001883 // get screen geometry
1884 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1885 const uint32_t hw_w = hw.getWidth();
1886 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001887 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001888
1889 GLfloat u, v;
1890 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001891 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001892 if (result != NO_ERROR) {
1893 return result;
1894 }
1895
1896 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001897 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001898 glBindTexture(GL_TEXTURE_2D, tname);
1899 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1900 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1901 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1902 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1903 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1904 glVertexPointer(2, GL_FLOAT, 0, vtx);
1905
Mathias Agopianffcf4652011-07-07 17:30:31 -07001906 /*
1907 * Texture coordinate mapping
1908 *
1909 * u
1910 * 1 +----------+---+
1911 * | | | | image is inverted
1912 * | V | | w.r.t. the texture
1913 * 1-v +----------+ | coordinates
1914 * | |
1915 * | |
1916 * | |
1917 * 0 +--------------+
1918 * 0 1
1919 *
1920 */
1921
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001922 class s_curve_interpolator {
1923 const float nbFrames, s, v;
1924 public:
1925 s_curve_interpolator(int nbFrames, float s)
1926 : nbFrames(1.0f / (nbFrames-1)), s(s),
1927 v(1.0f + expf(-s + 0.5f*s)) {
1928 }
1929 float operator()(int f) {
1930 const float x = f * nbFrames;
1931 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1932 }
1933 };
1934
1935 class v_stretch {
1936 const GLfloat hw_w, hw_h;
1937 public:
1938 v_stretch(uint32_t hw_w, uint32_t hw_h)
1939 : hw_w(hw_w), hw_h(hw_h) {
1940 }
1941 void operator()(GLfloat* vtx, float v) {
1942 const GLfloat w = hw_w + (hw_w * v);
1943 const GLfloat h = hw_h - (hw_h * v);
1944 const GLfloat x = (hw_w - w) * 0.5f;
1945 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001946 vtx[0] = x; vtx[1] = y;
1947 vtx[2] = x; vtx[3] = y + h;
1948 vtx[4] = x + w; vtx[5] = y + h;
1949 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001950 }
1951 };
1952
1953 class h_stretch {
1954 const GLfloat hw_w, hw_h;
1955 public:
1956 h_stretch(uint32_t hw_w, uint32_t hw_h)
1957 : hw_w(hw_w), hw_h(hw_h) {
1958 }
1959 void operator()(GLfloat* vtx, float v) {
1960 const GLfloat w = hw_w - (hw_w * v);
1961 const GLfloat h = 1.0f;
1962 const GLfloat x = (hw_w - w) * 0.5f;
1963 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001964 vtx[0] = x; vtx[1] = y;
1965 vtx[2] = x; vtx[3] = y + h;
1966 vtx[4] = x + w; vtx[5] = y + h;
1967 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001968 }
1969 };
1970
1971 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001972 char value[PROPERTY_VALUE_MAX];
1973 property_get("debug.sf.electron_frames", value, "24");
1974 int nbFrames = (atoi(value) + 1) >> 1;
1975 if (nbFrames <= 0) // just in case
1976 nbFrames = 24;
1977
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978 s_curve_interpolator itr(nbFrames, 7.5f);
1979 s_curve_interpolator itg(nbFrames, 8.0f);
1980 s_curve_interpolator itb(nbFrames, 8.5f);
1981
1982 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001983
1984 glMatrixMode(GL_TEXTURE);
1985 glLoadIdentity();
1986 glMatrixMode(GL_MODELVIEW);
1987 glLoadIdentity();
1988
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001989 glEnable(GL_BLEND);
1990 glBlendFunc(GL_ONE, GL_ONE);
1991 for (int i=0 ; i<nbFrames ; i++) {
1992 float x, y, w, h;
1993 const float vr = itr(i);
1994 const float vg = itg(i);
1995 const float vb = itb(i);
1996
1997 // clear screen
1998 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001999 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002000 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002001
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 // draw the red plane
2003 vverts(vtx, vr);
2004 glColorMask(1,0,0,1);
2005 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002006
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007 // draw the green plane
2008 vverts(vtx, vg);
2009 glColorMask(0,1,0,1);
2010 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002011
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002012 // draw the blue plane
2013 vverts(vtx, vb);
2014 glColorMask(0,0,1,1);
2015 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002016
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002017 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002018 glDisable(GL_TEXTURE_2D);
2019 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002020 glColor4f(vg, vg, vg, 1);
2021 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2022 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002023 }
2024
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002025 h_stretch hverts(hw_w, hw_h);
2026 glDisable(GL_BLEND);
2027 glDisable(GL_TEXTURE_2D);
2028 glColorMask(1,1,1,1);
2029 for (int i=0 ; i<nbFrames ; i++) {
2030 const float v = itg(i);
2031 hverts(vtx, v);
2032 glClear(GL_COLOR_BUFFER_BIT);
2033 glColor4f(1-v, 1-v, 1-v, 1);
2034 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2035 hw.flip(screenBounds);
2036 }
2037
2038 glColorMask(1,1,1,1);
2039 glEnable(GL_SCISSOR_TEST);
2040 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2041 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002042 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002043 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002044 return NO_ERROR;
2045}
2046
2047status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2048{
2049 status_t result = PERMISSION_DENIED;
2050
2051 if (!GLExtensions::getInstance().haveFramebufferObject())
2052 return INVALID_OPERATION;
2053
2054
2055 // get screen geometry
2056 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2057 const uint32_t hw_w = hw.getWidth();
2058 const uint32_t hw_h = hw.getHeight();
2059 const Region screenBounds(hw.bounds());
2060
2061 GLfloat u, v;
2062 GLuint tname;
2063 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2064 if (result != NO_ERROR) {
2065 return result;
2066 }
2067
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 GLfloat vtx[8];
2069 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002070 glBindTexture(GL_TEXTURE_2D, tname);
2071 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2072 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2073 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2074 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2075 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2076 glVertexPointer(2, GL_FLOAT, 0, vtx);
2077
2078 class s_curve_interpolator {
2079 const float nbFrames, s, v;
2080 public:
2081 s_curve_interpolator(int nbFrames, float s)
2082 : nbFrames(1.0f / (nbFrames-1)), s(s),
2083 v(1.0f + expf(-s + 0.5f*s)) {
2084 }
2085 float operator()(int f) {
2086 const float x = f * nbFrames;
2087 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2088 }
2089 };
2090
2091 class v_stretch {
2092 const GLfloat hw_w, hw_h;
2093 public:
2094 v_stretch(uint32_t hw_w, uint32_t hw_h)
2095 : hw_w(hw_w), hw_h(hw_h) {
2096 }
2097 void operator()(GLfloat* vtx, float v) {
2098 const GLfloat w = hw_w + (hw_w * v);
2099 const GLfloat h = hw_h - (hw_h * v);
2100 const GLfloat x = (hw_w - w) * 0.5f;
2101 const GLfloat y = (hw_h - h) * 0.5f;
2102 vtx[0] = x; vtx[1] = y;
2103 vtx[2] = x; vtx[3] = y + h;
2104 vtx[4] = x + w; vtx[5] = y + h;
2105 vtx[6] = x + w; vtx[7] = y;
2106 }
2107 };
2108
2109 class h_stretch {
2110 const GLfloat hw_w, hw_h;
2111 public:
2112 h_stretch(uint32_t hw_w, uint32_t hw_h)
2113 : hw_w(hw_w), hw_h(hw_h) {
2114 }
2115 void operator()(GLfloat* vtx, float v) {
2116 const GLfloat w = hw_w - (hw_w * v);
2117 const GLfloat h = 1.0f;
2118 const GLfloat x = (hw_w - w) * 0.5f;
2119 const GLfloat y = (hw_h - h) * 0.5f;
2120 vtx[0] = x; vtx[1] = y;
2121 vtx[2] = x; vtx[3] = y + h;
2122 vtx[4] = x + w; vtx[5] = y + h;
2123 vtx[6] = x + w; vtx[7] = y;
2124 }
2125 };
2126
Mathias Agopiana6546e52010-10-14 12:33:07 -07002127 // the full animation is 12 frames
2128 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002129 s_curve_interpolator itr(nbFrames, 7.5f);
2130 s_curve_interpolator itg(nbFrames, 8.0f);
2131 s_curve_interpolator itb(nbFrames, 8.5f);
2132
2133 h_stretch hverts(hw_w, hw_h);
2134 glDisable(GL_BLEND);
2135 glDisable(GL_TEXTURE_2D);
2136 glColorMask(1,1,1,1);
2137 for (int i=nbFrames-1 ; i>=0 ; i--) {
2138 const float v = itg(i);
2139 hverts(vtx, v);
2140 glClear(GL_COLOR_BUFFER_BIT);
2141 glColor4f(1-v, 1-v, 1-v, 1);
2142 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2143 hw.flip(screenBounds);
2144 }
2145
Mathias Agopiana6546e52010-10-14 12:33:07 -07002146 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002147 v_stretch vverts(hw_w, hw_h);
2148 glEnable(GL_BLEND);
2149 glBlendFunc(GL_ONE, GL_ONE);
2150 for (int i=nbFrames-1 ; i>=0 ; i--) {
2151 float x, y, w, h;
2152 const float vr = itr(i);
2153 const float vg = itg(i);
2154 const float vb = itb(i);
2155
2156 // clear screen
2157 glColorMask(1,1,1,1);
2158 glClear(GL_COLOR_BUFFER_BIT);
2159 glEnable(GL_TEXTURE_2D);
2160
2161 // draw the red plane
2162 vverts(vtx, vr);
2163 glColorMask(1,0,0,1);
2164 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2165
2166 // draw the green plane
2167 vverts(vtx, vg);
2168 glColorMask(0,1,0,1);
2169 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2170
2171 // draw the blue plane
2172 vverts(vtx, vb);
2173 glColorMask(0,0,1,1);
2174 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2175
2176 hw.flip(screenBounds);
2177 }
2178
2179 glColorMask(1,1,1,1);
2180 glEnable(GL_SCISSOR_TEST);
2181 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002182 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002183 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002184 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002185
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002186 return NO_ERROR;
2187}
2188
2189// ---------------------------------------------------------------------------
2190
Mathias Agopianabd671a2010-10-14 14:54:06 -07002191status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002192{
2193 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2194 if (!hw.canDraw()) {
2195 // we're already off
2196 return NO_ERROR;
2197 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002198
2199 // turn off hwc while we're doing the animation
2200 hw.getHwComposer().disable();
2201 // and make sure to turn it back on (if needed) next time we compose
2202 invalidateHwcGeometry();
2203
Mathias Agopianabd671a2010-10-14 14:54:06 -07002204 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2205 electronBeamOffAnimationImplLocked();
2206 }
2207
2208 // always clear the whole screen at the end of the animation
2209 glClearColor(0,0,0,1);
2210 glDisable(GL_SCISSOR_TEST);
2211 glClear(GL_COLOR_BUFFER_BIT);
2212 glEnable(GL_SCISSOR_TEST);
2213 hw.flip( Region(hw.bounds()) );
2214
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002215 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002216}
2217
2218status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2219{
Mathias Agopian59119e62010-10-11 12:37:43 -07002220 class MessageTurnElectronBeamOff : public MessageBase {
2221 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002222 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002223 status_t result;
2224 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002225 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2226 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002227 }
2228 status_t getResult() const {
2229 return result;
2230 }
2231 virtual bool handler() {
2232 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002233 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002234 return true;
2235 }
2236 };
2237
Mathias Agopianabd671a2010-10-14 14:54:06 -07002238 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002239 status_t res = postMessageSync(msg);
2240 if (res == NO_ERROR) {
2241 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242
2243 // work-around: when the power-manager calls us we activate the
2244 // animation. eventually, the "on" animation will be called
2245 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002247 }
2248 return res;
2249}
2250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002251// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002252
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254{
2255 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2256 if (hw.canDraw()) {
2257 // we're already on
2258 return NO_ERROR;
2259 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2261 electronBeamOnAnimationImplLocked();
2262 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002263
2264 // make sure to redraw the whole screen when the animation is done
2265 mDirtyRegion.set(hw.bounds());
2266 signalEvent();
2267
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002268 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002269}
2270
2271status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2272{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002273 class MessageTurnElectronBeamOn : public MessageBase {
2274 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002275 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002276 status_t result;
2277 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002278 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2279 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002280 }
2281 status_t getResult() const {
2282 return result;
2283 }
2284 virtual bool handler() {
2285 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002286 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002287 return true;
2288 }
2289 };
2290
Mathias Agopianabd671a2010-10-14 14:54:06 -07002291 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002292 return NO_ERROR;
2293}
2294
2295// ---------------------------------------------------------------------------
2296
Mathias Agopian74c40c02010-09-29 13:02:36 -07002297status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2298 sp<IMemoryHeap>* heap,
2299 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002300 uint32_t sw, uint32_t sh,
2301 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002302{
2303 status_t result = PERMISSION_DENIED;
2304
2305 // only one display supported for now
2306 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2307 return BAD_VALUE;
2308
2309 if (!GLExtensions::getInstance().haveFramebufferObject())
2310 return INVALID_OPERATION;
2311
2312 // get screen geometry
2313 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2314 const uint32_t hw_w = hw.getWidth();
2315 const uint32_t hw_h = hw.getHeight();
2316
2317 if ((sw > hw_w) || (sh > hw_h))
2318 return BAD_VALUE;
2319
2320 sw = (!sw) ? hw_w : sw;
2321 sh = (!sh) ? hw_h : sh;
2322 const size_t size = sw * sh * 4;
2323
Mathias Agopian1c71a472011-03-02 18:45:50 -08002324 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2325 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002326
Mathias Agopian74c40c02010-09-29 13:02:36 -07002327 // make sure to clear all GL error flags
2328 while ( glGetError() != GL_NO_ERROR ) ;
2329
2330 // create a FBO
2331 GLuint name, tname;
2332 glGenRenderbuffersOES(1, &tname);
2333 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2334 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2335 glGenFramebuffersOES(1, &name);
2336 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2337 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2338 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2339
2340 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002341
Mathias Agopian74c40c02010-09-29 13:02:36 -07002342 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2343
2344 // invert everything, b/c glReadPixel() below will invert the FB
2345 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002346 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002347 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002348 glMatrixMode(GL_PROJECTION);
2349 glPushMatrix();
2350 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002351 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002352 glMatrixMode(GL_MODELVIEW);
2353
2354 // redraw the screen entirely...
2355 glClearColor(0,0,0,1);
2356 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002357
Jamie Gennis9575f602011-10-07 14:51:16 -07002358 const LayerVector& layers(mDrawingState.layersSortedByZ);
2359 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 for (size_t i=0 ; i<count ; ++i) {
2361 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002362 const uint32_t flags = layer->drawingState().flags;
2363 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2364 const uint32_t z = layer->drawingState().z;
2365 if (z >= minLayerZ && z <= maxLayerZ) {
2366 layer->drawForSreenShot();
2367 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002368 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002369 }
2370
2371 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002372 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 glScissor(0, 0, sw, sh);
2374
2375 // check for errors and return screen capture
2376 if (glGetError() != GL_NO_ERROR) {
2377 // error while rendering
2378 result = INVALID_OPERATION;
2379 } else {
2380 // allocate shared memory large enough to hold the
2381 // screen capture
2382 sp<MemoryHeapBase> base(
2383 new MemoryHeapBase(size, 0, "screen-capture") );
2384 void* const ptr = base->getBase();
2385 if (ptr) {
2386 // capture the screen with glReadPixels()
2387 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2388 if (glGetError() == GL_NO_ERROR) {
2389 *heap = base;
2390 *w = sw;
2391 *h = sh;
2392 *f = PIXEL_FORMAT_RGBA_8888;
2393 result = NO_ERROR;
2394 }
2395 } else {
2396 result = NO_MEMORY;
2397 }
2398 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002399 glEnable(GL_SCISSOR_TEST);
2400 glViewport(0, 0, hw_w, hw_h);
2401 glMatrixMode(GL_PROJECTION);
2402 glPopMatrix();
2403 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002404 } else {
2405 result = BAD_VALUE;
2406 }
2407
2408 // release FBO resources
2409 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2410 glDeleteRenderbuffersOES(1, &tname);
2411 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002412
2413 hw.compositionComplete();
2414
Mathias Agopian1c71a472011-03-02 18:45:50 -08002415 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002416
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 return result;
2418}
2419
2420
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002421status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2422 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002424 uint32_t sw, uint32_t sh,
2425 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002426{
2427 // only one display supported for now
2428 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2429 return BAD_VALUE;
2430
2431 if (!GLExtensions::getInstance().haveFramebufferObject())
2432 return INVALID_OPERATION;
2433
2434 class MessageCaptureScreen : public MessageBase {
2435 SurfaceFlinger* flinger;
2436 DisplayID dpy;
2437 sp<IMemoryHeap>* heap;
2438 uint32_t* w;
2439 uint32_t* h;
2440 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002441 uint32_t sw;
2442 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002443 uint32_t minLayerZ;
2444 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002445 status_t result;
2446 public:
2447 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002448 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002449 uint32_t sw, uint32_t sh,
2450 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002451 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002452 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2453 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2454 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002455 {
2456 }
2457 status_t getResult() const {
2458 return result;
2459 }
2460 virtual bool handler() {
2461 Mutex::Autolock _l(flinger->mStateLock);
2462
2463 // if we have secure windows, never allow the screen capture
2464 if (flinger->mSecureFrameBuffer)
2465 return true;
2466
Mathias Agopian74c40c02010-09-29 13:02:36 -07002467 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002468 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002470 return true;
2471 }
2472 };
2473
2474 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002475 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002476 status_t res = postMessageSync(msg);
2477 if (res == NO_ERROR) {
2478 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2479 }
2480 return res;
2481}
2482
2483// ---------------------------------------------------------------------------
2484
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002485sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2486{
2487 sp<Layer> result;
2488 Mutex::Autolock _l(mStateLock);
2489 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2490 return result;
2491}
2492
2493// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494
Mathias Agopian96f08192010-06-02 23:28:45 -07002495Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002496 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498}
2499
Mathias Agopian96f08192010-06-02 23:28:45 -07002500Client::~Client()
2501{
Mathias Agopian96f08192010-06-02 23:28:45 -07002502 const size_t count = mLayers.size();
2503 for (size_t i=0 ; i<count ; i++) {
2504 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2505 if (layer != 0) {
2506 mFlinger->removeLayer(layer);
2507 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
2509}
2510
Mathias Agopian96f08192010-06-02 23:28:45 -07002511status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002512 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002513}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002514
Mathias Agopian4f113742011-05-03 16:21:41 -07002515size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002516{
Mathias Agopian4f113742011-05-03 16:21:41 -07002517 Mutex::Autolock _l(mLock);
2518 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002519 mLayers.add(name, layer);
2520 return name;
2521}
2522
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002523void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002524{
Mathias Agopian4f113742011-05-03 16:21:41 -07002525 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002526 // we do a linear search here, because this doesn't happen often
2527 const size_t count = mLayers.size();
2528 for (size_t i=0 ; i<count ; i++) {
2529 if (mLayers.valueAt(i) == layer) {
2530 mLayers.removeItemsAt(i, 1);
2531 break;
2532 }
2533 }
2534}
Mathias Agopian4f113742011-05-03 16:21:41 -07002535sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2536{
2537 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002538 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002539 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002540 if (layer != 0) {
2541 lbc = layer.promote();
2542 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002543 }
2544 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002545}
2546
Mathias Agopiana67932f2011-04-20 14:20:59 -07002547
2548status_t Client::onTransact(
2549 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2550{
2551 // these must be checked
2552 IPCThreadState* ipc = IPCThreadState::self();
2553 const int pid = ipc->getCallingPid();
2554 const int uid = ipc->getCallingUid();
2555 const int self_pid = getpid();
2556 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2557 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002558 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002559 {
2560 LOGE("Permission Denial: "
2561 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2562 return PERMISSION_DENIED;
2563 }
2564 }
2565 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002566}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002567
2568
Mathias Agopian96f08192010-06-02 23:28:45 -07002569sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002570 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002571 const String8& name,
2572 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002573 uint32_t flags)
2574{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002575 /*
2576 * createSurface must be called from the GL thread so that it can
2577 * have access to the GL context.
2578 */
2579
2580 class MessageCreateSurface : public MessageBase {
2581 sp<ISurface> result;
2582 SurfaceFlinger* flinger;
2583 ISurfaceComposerClient::surface_data_t* params;
2584 Client* client;
2585 const String8& name;
2586 DisplayID display;
2587 uint32_t w, h;
2588 PixelFormat format;
2589 uint32_t flags;
2590 public:
2591 MessageCreateSurface(SurfaceFlinger* flinger,
2592 ISurfaceComposerClient::surface_data_t* params,
2593 const String8& name, Client* client,
2594 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2595 uint32_t flags)
2596 : flinger(flinger), params(params), client(client), name(name),
2597 display(display), w(w), h(h), format(format), flags(flags)
2598 {
2599 }
2600 sp<ISurface> getResult() const { return result; }
2601 virtual bool handler() {
2602 result = flinger->createSurface(params, name, client,
2603 display, w, h, format, flags);
2604 return true;
2605 }
2606 };
2607
2608 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2609 params, name, this, display, w, h, format, flags);
2610 mFlinger->postMessageSync(msg);
2611 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002612}
Mathias Agopian96f08192010-06-02 23:28:45 -07002613status_t Client::destroySurface(SurfaceID sid) {
2614 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002616
2617// ---------------------------------------------------------------------------
2618
Jamie Gennis9a78c902011-01-12 18:30:40 -08002619GraphicBufferAlloc::GraphicBufferAlloc() {}
2620
2621GraphicBufferAlloc::~GraphicBufferAlloc() {}
2622
2623sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002624 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002625 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2626 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002627 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002628 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002629 if (err == NO_MEMORY) {
2630 GraphicBuffer::dumpAllocationsToSystemLog();
2631 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002632 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2633 "failed (%s), handle=%p",
2634 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002635 return 0;
2636 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002637 return graphicBuffer;
2638}
2639
Jamie Gennis9a78c902011-01-12 18:30:40 -08002640// ---------------------------------------------------------------------------
2641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002642GraphicPlane::GraphicPlane()
2643 : mHw(0)
2644{
2645}
2646
2647GraphicPlane::~GraphicPlane() {
2648 delete mHw;
2649}
2650
2651bool GraphicPlane::initialized() const {
2652 return mHw ? true : false;
2653}
2654
Mathias Agopian2b92d892010-02-08 15:49:35 -08002655int GraphicPlane::getWidth() const {
2656 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002657}
2658
Mathias Agopian2b92d892010-02-08 15:49:35 -08002659int GraphicPlane::getHeight() const {
2660 return mHeight;
2661}
2662
2663void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2664{
2665 mHw = hw;
2666
2667 // initialize the display orientation transform.
2668 // it's a constant that should come from the display driver.
2669 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2670 char property[PROPERTY_VALUE_MAX];
2671 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2672 //displayOrientation
2673 switch (atoi(property)) {
2674 case 90:
2675 displayOrientation = ISurfaceComposer::eOrientation90;
2676 break;
2677 case 270:
2678 displayOrientation = ISurfaceComposer::eOrientation270;
2679 break;
2680 }
2681 }
2682
2683 const float w = hw->getWidth();
2684 const float h = hw->getHeight();
2685 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2686 &mDisplayTransform);
2687 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2688 mDisplayWidth = h;
2689 mDisplayHeight = w;
2690 } else {
2691 mDisplayWidth = w;
2692 mDisplayHeight = h;
2693 }
2694
2695 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696}
2697
2698status_t GraphicPlane::orientationToTransfrom(
2699 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002700{
2701 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002702 switch (orientation) {
2703 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002704 flags = Transform::ROT_0;
2705 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002707 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002708 break;
2709 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002710 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002711 break;
2712 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002713 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002714 break;
2715 default:
2716 return BAD_VALUE;
2717 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002718 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002719 return NO_ERROR;
2720}
2721
2722status_t GraphicPlane::setOrientation(int orientation)
2723{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002724 // If the rotation can be handled in hardware, this is where
2725 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002726
2727 const DisplayHardware& hw(displayHardware());
2728 const float w = mDisplayWidth;
2729 const float h = mDisplayHeight;
2730 mWidth = int(w);
2731 mHeight = int(h);
2732
2733 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002734 GraphicPlane::orientationToTransfrom(orientation, w, h,
2735 &orientationTransform);
2736 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2737 mWidth = int(h);
2738 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002739 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002740
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002741 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002742 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002743 return NO_ERROR;
2744}
2745
2746const DisplayHardware& GraphicPlane::displayHardware() const {
2747 return *mHw;
2748}
2749
Mathias Agopian59119e62010-10-11 12:37:43 -07002750DisplayHardware& GraphicPlane::editDisplayHardware() {
2751 return *mHw;
2752}
2753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754const Transform& GraphicPlane::transform() const {
2755 return mGlobalTransform;
2756}
2757
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002758EGLDisplay GraphicPlane::getEGLDisplay() const {
2759 return mHw->getEGLDisplay();
2760}
2761
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002762// ---------------------------------------------------------------------------
2763
2764}; // namespace android