blob: d795809adbe9d84f666b0d2d31ad6f4806f2ccf5 [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
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/IDisplayEventConnection.h>
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <utils/String8.h>
40#include <utils/String16.h>
41#include <utils/StopWatch.h>
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070044#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
47#include <pixelflinger/pixelflinger.h>
48#include <GLES/gl.h>
49
50#include "clz.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051#include "DisplayEventConnection.h"
52#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070053#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070054#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Mathias Agopiana67932f2011-04-20 14:20:59 -070063#include <private/surfaceflinger/SharedBufferStack.h>
64
Mathias Agopiana1ecca92009-05-21 19:21:59 -070065/* ideally AID_GRAPHICS would be in a semi-public header
66 * or there would be a way to map a user/group name to its id
67 */
68#ifndef AID_GRAPHICS
69#define AID_GRAPHICS 1003
70#endif
71
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080072#define EGL_VERSION_HW_ANDROID 0x3143
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
76namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077// ---------------------------------------------------------------------------
78
Mathias Agopian99b49842011-06-27 16:05:52 -070079const String16 sHardwareTest("android.permission.HARDWARE_TEST");
80const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
81const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
82const String16 sDump("android.permission.DUMP");
83
84// ---------------------------------------------------------------------------
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086SurfaceFlinger::SurfaceFlinger()
87 : BnSurfaceComposer(), Thread(false),
88 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070089 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070093 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070094 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070097 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070098 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070099 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700100 mDebugInSwapBuffers(0),
101 mLastSwapBufferTime(0),
102 mDebugInTransaction(0),
103 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700104 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mConsoleSignals(0),
106 mSecureFrameBuffer(0)
107{
108 init();
109}
110
111void SurfaceFlinger::init()
112{
Steve Blocka19954a2012-01-04 20:05:49 +0000113 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
115 // debugging stuff...
116 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 property_get("debug.sf.showupdates", value, "0");
119 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 property_get("debug.sf.showbackground", value, "0");
122 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124 property_get("debug.sf.ddms", value, "0");
125 mDebugDDMS = atoi(value);
126 if (mDebugDDMS) {
127 DdmConnection::start(getServiceName());
128 }
129
Steve Blocka19954a2012-01-04 20:05:49 +0000130 ALOGI_IF(mDebugRegion, "showupdates enabled");
131 ALOGI_IF(mDebugBackground, "showbackground enabled");
132 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133}
134
135SurfaceFlinger::~SurfaceFlinger()
136{
137 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700140sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700142 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian7e27f052010-05-28 14:22:23 -0700145sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146{
Mathias Agopian96f08192010-06-02 23:28:45 -0700147 sp<ISurfaceComposerClient> bclient;
148 sp<Client> client(new Client(this));
149 status_t err = client->initCheck();
150 if (err == NO_ERROR) {
151 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 return bclient;
154}
155
Jamie Gennis9a78c902011-01-12 18:30:40 -0800156sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
157{
158 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
159 return gba;
160}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
163{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000164 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 const GraphicPlane& plane(mGraphicPlanes[dpy]);
166 return plane;
167}
168
169GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
170{
171 return const_cast<GraphicPlane&>(
172 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
173}
174
175void SurfaceFlinger::bootFinished()
176{
177 const nsecs_t now = systemTime();
178 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000179 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700180 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700181
182 // wait patiently for the window manager death
183 const String16 name("window");
184 sp<IBinder> window(defaultServiceManager()->getService(name));
185 if (window != 0) {
186 window->linkToDeath(this);
187 }
188
189 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700190 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700193void SurfaceFlinger::binderDied(const wp<IBinder>& who)
194{
195 // the window manager died on us. prepare its eulogy.
196
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700197 // reset screen orientation
198 setOrientation(0, eOrientationDefault, 0);
199
200 // restart the boot-animation
201 property_set("ctl.start", "bootanim");
202}
203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204void SurfaceFlinger::onFirstRef()
205{
206 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
207
208 // Wait for the main thread to be done with its initialization
209 mReadyToRunBarrier.wait();
210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212static inline uint16_t pack565(int r, int g, int b) {
213 return (r<<11)|(g<<5)|b;
214}
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216status_t SurfaceFlinger::readyToRun()
217{
Steve Blocka19954a2012-01-04 20:05:49 +0000218 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 "Initializing graphics H/W...");
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 // we only support one display currently
222 int dpy = 0;
223
224 {
225 // initialize the main display
226 GraphicPlane& plane(graphicPlane(dpy));
227 DisplayHardware* const hw = new DisplayHardware(this, dpy);
228 plane.setDisplayHardware(hw);
229 }
230
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700231 // create the shared control-block
232 mServerHeap = new MemoryHeapBase(4096,
233 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000234 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700235
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700236 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000237 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700238
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700239 new(mServerCblk) surface_flinger_cblk_t;
240
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 // initialize primary screen
242 // (other display should be initialized in the same manner, but
243 // asynchronously, as they could come and go. None of this is supported
244 // yet).
245 const GraphicPlane& plane(graphicPlane(dpy));
246 const DisplayHardware& hw = plane.displayHardware();
247 const uint32_t w = hw.getWidth();
248 const uint32_t h = hw.getHeight();
249 const uint32_t f = hw.getFormat();
250 hw.makeCurrent();
251
252 // initialize the shared control block
253 mServerCblk->connected |= 1<<dpy;
254 display_cblk_t* dcblk = mServerCblk->displays + dpy;
255 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800256 dcblk->w = plane.getWidth();
257 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 dcblk->format = f;
259 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
260 dcblk->xdpi = hw.getDpiX();
261 dcblk->ydpi = hw.getDpiY();
262 dcblk->fps = hw.getRefreshRate();
263 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264
265 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700267 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glEnableClientState(GL_VERTEX_ARRAY);
269 glEnable(GL_SCISSOR_TEST);
270 glShadeModel(GL_FLAT);
271 glDisable(GL_DITHER);
272 glDisable(GL_CULL_FACE);
273
274 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
275 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700276 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 glGenTextures(1, &mWormholeTexName);
278 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
283 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700284 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
285
286 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
287 glGenTextures(1, &mProtectedTexName);
288 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
291 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
292 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
293 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
294 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
296 glViewport(0, 0, w, h);
297 glMatrixMode(GL_PROJECTION);
298 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700299 // put the origin in the left-bottom corner
300 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 -0800301
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800302
303 // start the EventThread
304 mEventThread = new EventThread(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 /*
307 * We're now ready to accept clients...
308 */
309
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800310 mReadyToRunBarrier.open();
311
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700312 // start boot animation
313 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700314
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
318// ----------------------------------------------------------------------------
319#if 0
320#pragma mark -
321#pragma mark Events Handler
322#endif
323
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800324void SurfaceFlinger::waitForEvent() {
325 mEventQueue.waitMessage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
327
328void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700329 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330}
331
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800332status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
333 nsecs_t reltime, uint32_t flags) {
334 return mEventQueue.postMessage(msg, reltime);
335}
336
337status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
338 nsecs_t reltime, uint32_t flags) {
339 status_t res = mEventQueue.postMessage(msg, reltime);
340 if (res == NO_ERROR) {
341 msg->wait();
342 }
343 return res;
344}
345
346// ----------------------------------------------------------------------------
347
Jamie Gennis582270d2011-08-17 18:19:00 -0700348bool SurfaceFlinger::authenticateSurfaceTexture(
349 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800350 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700351 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800352
353 // Check the visible layer list for the ISurface
354 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
355 size_t count = currentLayers.size();
356 for (size_t i=0 ; i<count ; i++) {
357 const sp<LayerBase>& layer(currentLayers[i]);
358 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700359 if (lbc != NULL) {
360 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
361 if (lbcBinder == surfaceTextureBinder) {
362 return true;
363 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800364 }
365 }
366
367 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700368 // SurfaceTexture gets destroyed before all the clients are done using it,
369 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800370 // will instead fail later on when the client tries to use the surface,
371 // which should be reported as "surface XYZ returned an -ENODEV". The
372 // purgatorized layers are no less authentic than the visible ones, so this
373 // should not cause any harm.
374 size_t purgatorySize = mLayerPurgatory.size();
375 for (size_t i=0 ; i<purgatorySize ; i++) {
376 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(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 return false;
387}
388
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800389// ----------------------------------------------------------------------------
390
391sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800392 sp<DisplayEventConnection> result(new DisplayEventConnection(mEventThread));
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800393 return result;
Mathias Agopianbb641242010-05-18 17:06:55 -0700394}
395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396// ----------------------------------------------------------------------------
397#if 0
398#pragma mark -
399#pragma mark Main loop
400#endif
401
402bool SurfaceFlinger::threadLoop()
403{
404 waitForEvent();
405
406 // check for transactions
Glenn Kasten99ed2242011-12-15 09:51:17 -0800407 if (CC_UNLIKELY(mConsoleSignals)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 handleConsoleEvents();
409 }
410
Mathias Agopian698c0872011-06-28 19:09:31 -0700411 // if we're in a global transaction, don't do anything.
412 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
413 uint32_t transactionFlags = peekTransactionFlags(mask);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800414 if (CC_UNLIKELY(transactionFlags)) {
Mathias Agopian698c0872011-06-28 19:09:31 -0700415 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 }
417
418 // post surfaces (if needed)
419 handlePageFlip();
420
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700421 if (mDirtyRegion.isEmpty()) {
422 // nothing new to do.
423 return true;
424 }
425
Glenn Kasten99ed2242011-12-15 09:51:17 -0800426 if (CC_UNLIKELY(mHwWorkListDirty)) {
Mathias Agopiana350ff92010-08-10 17:14:02 -0700427 // build the h/w work list
428 handleWorkList();
429 }
430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Glenn Kasten99ed2242011-12-15 09:51:17 -0800432 if (CC_LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700434
435 const int index = hw.getCurrentBufferIndex();
436 GraphicLog& logger(GraphicLog::getInstance());
437
438 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 handleRepaint();
440
Mathias Agopian74faca22009-09-17 16:18:16 -0700441 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700442 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700443 hw.compositionComplete();
444
Mathias Agopian35b48d12010-09-13 22:57:58 -0700445 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700446 postFramebuffer();
447
Mathias Agopian35b48d12010-09-13 22:57:58 -0700448 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 } else {
450 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800451 hw.compositionComplete();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800452 hw.waitForVSync();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453 }
454 return true;
455}
456
457void SurfaceFlinger::postFramebuffer()
458{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700459 // this should never happen. we do the flip anyways so we don't
460 // risk to cause a deadlock with hwc
Steve Block32397c12012-01-05 23:22:43 +0000461 ALOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700462 const DisplayHardware& hw(graphicPlane(0).displayHardware());
463 const nsecs_t now = systemTime();
464 mDebugInSwapBuffers = now;
465 hw.flip(mSwapRegion);
466 mLastSwapBufferTime = systemTime() - now;
467 mDebugInSwapBuffers = 0;
468 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
471void SurfaceFlinger::handleConsoleEvents()
472{
473 // something to do with the console
474 const DisplayHardware& hw = graphicPlane(0).displayHardware();
475
476 int what = android_atomic_and(0, &mConsoleSignals);
477 if (what & eConsoleAcquired) {
478 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700479 // this is a temporary work-around, eventually this should be called
480 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700481 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 }
483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700485 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 }
488 }
489
490 mDirtyRegion.set(hw.bounds());
491}
492
493void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
494{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700495 Mutex::Autolock _l(mStateLock);
496 const nsecs_t now = systemTime();
497 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498
Mathias Agopianca4d3602011-05-19 15:38:14 -0700499 // Here we're guaranteed that some transaction flags are set
500 // so we can call handleTransactionLocked() unconditionally.
501 // We call getTransactionFlags(), which will also clear the flags,
502 // with mStateLock held to guarantee that mCurrentState won't change
503 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700504
Mathias Agopianca4d3602011-05-19 15:38:14 -0700505 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
506 transactionFlags = getTransactionFlags(mask);
507 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700508
Mathias Agopianca4d3602011-05-19 15:38:14 -0700509 mLastTransactionTime = systemTime() - now;
510 mDebugInTransaction = 0;
511 invalidateHwcGeometry();
512 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700513}
514
Mathias Agopianca4d3602011-05-19 15:38:14 -0700515void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700516{
517 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 const size_t count = currentLayers.size();
519
520 /*
521 * Traversal of the children
522 * (perform the transaction for each of them if needed)
523 */
524
525 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
526 if (layersNeedTransaction) {
527 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700528 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
530 if (!trFlags) continue;
531
532 const uint32_t flags = layer->doTransaction(0);
533 if (flags & Layer::eVisibleRegion)
534 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535 }
536 }
537
538 /*
539 * Perform our own transaction if needed
540 */
541
542 if (transactionFlags & eTransactionNeeded) {
543 if (mCurrentState.orientation != mDrawingState.orientation) {
544 // the orientation has changed, recompute all visible regions
545 // and invalidate everything.
546
547 const int dpy = 0;
548 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700549 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 GraphicPlane& plane(graphicPlane(dpy));
551 plane.setOrientation(orientation);
552
553 // update the shared control block
554 const DisplayHardware& hw(plane.displayHardware());
555 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
556 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800557 dcblk->w = plane.getWidth();
558 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800559
560 mVisibleRegionsDirty = true;
561 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 }
563
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
565 // layers have been added
566 mVisibleRegionsDirty = true;
567 }
568
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 // some layers might have been removed, so
570 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700572 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700574 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700575 const size_t count = previousLayers.size();
576 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700577 const sp<LayerBase>& layer(previousLayers[i]);
578 if (currentLayers.indexOf( layer ) < 0) {
579 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700580 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700581 }
582 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584 }
585
586 commitTransaction();
587}
588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800590 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591{
592 const GraphicPlane& plane(graphicPlane(0));
593 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700594 const DisplayHardware& hw(plane.displayHardware());
595 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596
597 Region aboveOpaqueLayers;
598 Region aboveCoveredLayers;
599 Region dirty;
600
601 bool secureFrameBuffer = false;
602
603 size_t i = currentLayers.size();
604 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 layer->validateVisibility(planeTransform);
607
608 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700609 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610
Mathias Agopianab028732010-03-16 16:41:46 -0700611 /*
612 * opaqueRegion: area of a surface that is fully opaque.
613 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700615
616 /*
617 * visibleRegion: area of a surface that is visible on screen
618 * and not fully transparent. This is essentially the layer's
619 * footprint minus the opaque regions above it.
620 * Areas covered by a translucent surface are considered visible.
621 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700623
624 /*
625 * coveredRegion: area of a surface that is covered by all
626 * visible regions above it (which includes the translucent areas).
627 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700629
630
631 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800632 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700633 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700634 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700636 visibleRegion.andSelf(screenRegion);
637 if (!visibleRegion.isEmpty()) {
638 // Remove the transparent area from the visible region
639 if (translucent) {
640 visibleRegion.subtractSelf(layer->transparentRegionScreen);
641 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642
Mathias Agopianab028732010-03-16 16:41:46 -0700643 // compute the opaque region
644 const int32_t layerOrientation = layer->getOrientation();
645 if (s.alpha==255 && !translucent &&
646 ((layerOrientation & Transform::ROT_INVALID) == false)) {
647 // the opaque region is the layer's footprint
648 opaqueRegion = visibleRegion;
649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 }
651 }
652
Mathias Agopianab028732010-03-16 16:41:46 -0700653 // Clip the covered region to the visible region
654 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
655
656 // Update aboveCoveredLayers for next (lower) layer
657 aboveCoveredLayers.orSelf(visibleRegion);
658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 // subtract the opaque region covered by the layers above us
660 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
662 // compute this layer's dirty region
663 if (layer->contentDirty) {
664 // we need to invalidate the whole region
665 dirty = visibleRegion;
666 // as well, as the old visible region
667 dirty.orSelf(layer->visibleRegionScreen);
668 layer->contentDirty = false;
669 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700670 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700671 * the exposed region consists of two components:
672 * 1) what's VISIBLE now and was COVERED before
673 * 2) what's EXPOSED now less what was EXPOSED before
674 *
675 * note that (1) is conservative, we start with the whole
676 * visible region but only keep what used to be covered by
677 * something -- which mean it may have been exposed.
678 *
679 * (2) handles areas that were not covered by anything but got
680 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700681 */
Mathias Agopianab028732010-03-16 16:41:46 -0700682 const Region newExposed = visibleRegion - coveredRegion;
683 const Region oldVisibleRegion = layer->visibleRegionScreen;
684 const Region oldCoveredRegion = layer->coveredRegionScreen;
685 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
686 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 }
688 dirty.subtractSelf(aboveOpaqueLayers);
689
690 // accumulate to the screen dirty region
691 dirtyRegion.orSelf(dirty);
692
Mathias Agopianab028732010-03-16 16:41:46 -0700693 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696 // Store the visible region is screen space
697 layer->setVisibleRegion(visibleRegion);
698 layer->setCoveredRegion(coveredRegion);
699
Mathias Agopian97011222009-07-28 10:57:27 -0700700 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 if (layer->isSecure() && !visibleRegion.isEmpty()) {
702 secureFrameBuffer = true;
703 }
704 }
705
Mathias Agopian97011222009-07-28 10:57:27 -0700706 // invalidate the areas where a layer was removed
707 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
708 mDirtyRegionRemovedLayer.clear();
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 mSecureFrameBuffer = secureFrameBuffer;
711 opaqueRegion = aboveOpaqueLayers;
712}
713
714
715void SurfaceFlinger::commitTransaction()
716{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800717 if (!mLayersPendingRemoval.isEmpty()) {
718 // Notify removed layers now that they can't be drawn from
719 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
720 mLayersPendingRemoval[i]->onRemoved();
721 }
722 mLayersPendingRemoval.clear();
723 }
724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700726 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700727 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728}
729
730void SurfaceFlinger::handlePageFlip()
731{
732 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800733 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734 visibleRegions |= lockPageFlip(currentLayers);
735
736 const DisplayHardware& hw = graphicPlane(0).displayHardware();
737 const Region screenRegion(hw.bounds());
738 if (visibleRegions) {
739 Region opaqueRegion;
740 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700741
742 /*
743 * rebuild the visible layer list
744 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800745 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700746 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700747 mVisibleLayersSortedByZ.setCapacity(count);
748 for (size_t i=0 ; i<count ; i++) {
749 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
750 mVisibleLayersSortedByZ.add(currentLayers[i]);
751 }
752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 mWormholeRegion = screenRegion.subtract(opaqueRegion);
754 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800755 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 }
757
758 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700759
760 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 mDirtyRegion.andSelf(screenRegion);
762}
763
Mathias Agopianad456f92011-01-13 17:53:01 -0800764void SurfaceFlinger::invalidateHwcGeometry()
765{
766 mHwWorkListDirty = true;
767}
768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
770{
771 bool recomputeVisibleRegions = false;
772 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700773 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700775 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 layer->lockPageFlip(recomputeVisibleRegions);
777 }
778 return recomputeVisibleRegions;
779}
780
781void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
782{
783 const GraphicPlane& plane(graphicPlane(0));
784 const Transform& planeTransform(plane.transform());
785 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700786 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700788 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 layer->unlockPageFlip(planeTransform, mDirtyRegion);
790 }
791}
792
Mathias Agopiana350ff92010-08-10 17:14:02 -0700793void SurfaceFlinger::handleWorkList()
794{
795 mHwWorkListDirty = false;
796 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
797 if (hwc.initCheck() == NO_ERROR) {
798 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
799 const size_t count = currentLayers.size();
800 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700801 hwc_layer_t* const cur(hwc.getLayers());
802 for (size_t i=0 ; cur && i<count ; i++) {
803 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700804 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700805 cur[i].compositionType = HWC_FRAMEBUFFER;
806 cur[i].flags |= HWC_SKIP_LAYER;
807 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700808 }
809 }
810}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700811
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812void SurfaceFlinger::handleRepaint()
813{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700815 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800816
Glenn Kasten99ed2242011-12-15 09:51:17 -0800817 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818 debugFlashRegions();
819 }
820
Mathias Agopianb8a55602009-06-26 19:06:36 -0700821 // set the frame buffer
822 const DisplayHardware& hw(graphicPlane(0).displayHardware());
823 glMatrixMode(GL_MODELVIEW);
824 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825
826 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700827 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
828 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700829 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700830 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
831 // takes a rectangle, we must make sure to update that whole
832 // rectangle in that case
833 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700834 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700835 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700836 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // in the BUFFER_PRESERVED case, obviously, we can update only
839 // what's needed and nothing more.
840 // NOTE: this is NOT a common case, as preserving the backbuffer
841 // is costly and usually involves copying the whole update back.
842 }
843 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700844 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700845 // We need to redraw the rectangle that will be updated
846 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700847 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700849 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700850 } else {
851 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700853 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854 }
855 }
856
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700857 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 composeSurfaces(mDirtyRegion);
859
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700860 // update the swap region and clear the dirty region
861 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862 mDirtyRegion.clear();
863}
864
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700865void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800866{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700867 const DisplayHardware& hw(graphicPlane(0).displayHardware());
868 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700869 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700870 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700871 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700872 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700873
Mathias Agopianf384cc32011-09-08 18:31:55 -0700874 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
875 size_t count = layers.size();
876
Steve Blocke6f43dd2012-01-06 19:20:56 +0000877 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700878 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
879 hwc.getNumLayers(), count);
880
881 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700882 if (hwc.initCheck() == NO_ERROR) {
883 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
884 }
Mathias Agopian45721772010-08-12 15:03:26 -0700885
886 /*
887 * update the per-frame h/w composer data for each layer
888 * and build the transparent region of the FB
889 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700890 for (size_t i=0 ; i<count ; i++) {
891 const sp<LayerBase>& layer(layers[i]);
892 layer->setPerFrameData(&cur[i]);
893 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700894 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700895 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000896 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700897
Mathias Agopianf384cc32011-09-08 18:31:55 -0700898 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700899 // what's happening here is tricky.
900 // we want to clear all the layers with the CLEAR_FB flags
901 // that are opaque.
902 // however, since some GPU are efficient at preserving
903 // the backbuffer, we want to take advantage of that so we do the
904 // clear only in the dirty region (other areas will be preserved
905 // on those GPUs).
906 // NOTE: on non backbuffer preserving GPU, the dirty region
907 // has already been expanded as needed, so the code is correct
908 // there too.
909 //
910 // However, the content of the framebuffer cannot be trusted when
911 // we switch to/from FB/OVERLAY, in which case we need to
912 // expand the dirty region to those areas too.
913 //
914 // Note also that there is a special case when switching from
915 // "no layers in FB" to "some layers in FB", where we need to redraw
916 // the entire FB, since some areas might contain uninitialized
917 // data.
918 //
919 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700920 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700921 // that is, we can't erase anything outside the dirty region.
922
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700923 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700924
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700925 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
926 transparent.set(hw.getBounds());
927 dirtyInOut = transparent;
928 } else {
929 for (size_t i=0 ; i<count ; i++) {
930 const sp<LayerBase>& layer(layers[i]);
931 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
932 transparent.orSelf(layer->visibleRegionScreen);
933 }
934 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
935 if (isOverlay != layer->isOverlay()) {
936 // we transitioned to/from overlay, so add this layer
937 // to the dirty region so the framebuffer can be either
938 // cleared or redrawn.
939 dirtyInOut.orSelf(layer->visibleRegionScreen);
940 }
941 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800942 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700943 // don't erase stuff outside the dirty region
944 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700945 }
946
947 /*
948 * clear the area of the FB that need to be transparent
949 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700950 if (!transparent.isEmpty()) {
951 glClearColor(0,0,0,0);
952 Region::const_iterator it = transparent.begin();
953 Region::const_iterator const end = transparent.end();
954 const int32_t height = hw.getHeight();
955 while (it != end) {
956 const Rect& r(*it++);
957 const GLint sy = height - (r.top + r.height());
958 glScissor(r.left, sy, r.width(), r.height());
959 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800960 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700961 }
962 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700963}
Mathias Agopian45721772010-08-12 15:03:26 -0700964
Mathias Agopianf384cc32011-09-08 18:31:55 -0700965void SurfaceFlinger::composeSurfaces(const Region& dirty)
966{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700967 const DisplayHardware& hw(graphicPlane(0).displayHardware());
968 HWComposer& hwc(hw.getHwComposer());
969
970 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Glenn Kasten99ed2242011-12-15 09:51:17 -0800971 if (CC_UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700972 // should never happen unless the window manager has a bug
973 // draw something...
974 drawWormhole();
975 }
976
Mathias Agopian45721772010-08-12 15:03:26 -0700977 /*
978 * and then, render the layers targeted at the framebuffer
979 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700980 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700981 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
982 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700983 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700984 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700985 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700986 }
987 const sp<LayerBase>& layer(layers[i]);
988 const Region clip(dirty.intersect(layer->visibleRegionScreen));
989 if (!clip.isEmpty()) {
990 layer->draw(clip);
991 }
992 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993}
994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995void SurfaceFlinger::debugFlashRegions()
996{
Mathias Agopian0a917752010-06-14 21:20:00 -0700997 const DisplayHardware& hw(graphicPlane(0).displayHardware());
998 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700999 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -07001000 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -07001001 return;
1002 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001003
Mathias Agopian0a917752010-06-14 21:20:00 -07001004 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
1005 (flags & DisplayHardware::BUFFER_PRESERVED))) {
1006 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
1007 mDirtyRegion.bounds() : hw.bounds());
1008 composeSurfaces(repaint);
1009 }
1010
Mathias Agopianc492e672011-10-18 14:49:27 -07001011 glDisable(GL_TEXTURE_EXTERNAL_OES);
1012 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 glDisable(GL_SCISSOR_TEST);
1015
Mathias Agopian0926f502009-05-04 14:17:04 -07001016 static int toggle = 0;
1017 toggle = 1 - toggle;
1018 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001019 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001020 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001021 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001022 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023
Mathias Agopian20f68782009-05-11 00:03:41 -07001024 Region::const_iterator it = mDirtyRegion.begin();
1025 Region::const_iterator const end = mDirtyRegion.end();
1026 while (it != end) {
1027 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001029 { r.left, height - r.top },
1030 { r.left, height - r.bottom },
1031 { r.right, height - r.bottom },
1032 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 };
1034 glVertexPointer(2, GL_FLOAT, 0, vertices);
1035 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1036 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001037
Mathias Agopian0656a682011-09-20 17:22:44 -07001038 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039
1040 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001041 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042
1043 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044}
1045
1046void SurfaceFlinger::drawWormhole() const
1047{
1048 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1049 if (region.isEmpty())
1050 return;
1051
1052 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1053 const int32_t width = hw.getWidth();
1054 const int32_t height = hw.getHeight();
1055
Glenn Kasten99ed2242011-12-15 09:51:17 -08001056 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001057 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001058 Region::const_iterator it = region.begin();
1059 Region::const_iterator const end = region.end();
1060 while (it != end) {
1061 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 const GLint sy = height - (r.top + r.height());
1063 glScissor(r.left, sy, r.width(), r.height());
1064 glClear(GL_COLOR_BUFFER_BIT);
1065 }
1066 } else {
1067 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1068 { width, height }, { 0, height } };
1069 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001070
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 glVertexPointer(2, GL_SHORT, 0, vertices);
1072 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1073 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001074
1075 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 glEnable(GL_TEXTURE_2D);
1077 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1078 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1079 glMatrixMode(GL_TEXTURE);
1080 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001081
1082 glDisable(GL_BLEND);
1083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001085 Region::const_iterator it = region.begin();
1086 Region::const_iterator const end = region.end();
1087 while (it != end) {
1088 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 const GLint sy = height - (r.top + r.height());
1090 glScissor(r.left, sy, r.width(), r.height());
1091 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1092 }
1093 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001094 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001095 glLoadIdentity();
1096 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097 }
1098}
1099
1100void SurfaceFlinger::debugShowFPS() const
1101{
1102 static int mFrameCount;
1103 static int mLastFrameCount = 0;
1104 static nsecs_t mLastFpsTime = 0;
1105 static float mFps = 0;
1106 mFrameCount++;
1107 nsecs_t now = systemTime();
1108 nsecs_t diff = now - mLastFpsTime;
1109 if (diff > ms2ns(250)) {
1110 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1111 mLastFpsTime = now;
1112 mLastFrameCount = mFrameCount;
1113 }
1114 // XXX: mFPS has the value we want
1115 }
1116
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001117status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118{
1119 Mutex::Autolock _l(mStateLock);
1120 addLayer_l(layer);
1121 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1122 return NO_ERROR;
1123}
1124
Mathias Agopian96f08192010-06-02 23:28:45 -07001125status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1126{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001127 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001128 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1129}
1130
1131ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1132 const sp<LayerBaseClient>& lbc)
1133{
Mathias Agopian96f08192010-06-02 23:28:45 -07001134 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001135 size_t name = client->attachLayer(lbc);
1136
1137 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001138
1139 // add this layer to the current state list
1140 addLayer_l(lbc);
1141
Mathias Agopian4f113742011-05-03 16:21:41 -07001142 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001143}
1144
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001145status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146{
1147 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001148 status_t err = purgatorizeLayer_l(layer);
1149 if (err == NO_ERROR)
1150 setTransactionFlags(eTransactionNeeded);
1151 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152}
1153
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001154status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001156 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1157 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001158 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1161 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001162 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163 return NO_ERROR;
1164 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001165 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166}
1167
Mathias Agopian9a112062009-04-17 19:36:26 -07001168status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1169{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001170 // First add the layer to the purgatory list, which makes sure it won't
1171 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001172 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001173 if (err >= 0) {
1174 mLayerPurgatory.add(layerBase);
1175 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001176
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001177 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001178
Mathias Agopian3d579642009-06-04 18:46:21 -07001179 // it's possible that we don't find a layer, because it might
1180 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001181 // from the user because there is a race between Client::destroySurface(),
1182 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001183 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1184}
1185
Mathias Agopian96f08192010-06-02 23:28:45 -07001186status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187{
Mathias Agopian96f08192010-06-02 23:28:45 -07001188 layer->forceVisibilityTransaction();
1189 setTransactionFlags(eTraversalNeeded);
1190 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191}
1192
Mathias Agopiandea20b12011-05-03 17:04:02 -07001193uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1194{
1195 return android_atomic_release_load(&mTransactionFlags);
1196}
1197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1199{
1200 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1201}
1202
Mathias Agopianbb641242010-05-18 17:06:55 -07001203uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204{
1205 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1206 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001207 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 }
1209 return old;
1210}
1211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001213void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001214 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001215 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001216
Jamie Gennis28378392011-10-12 17:39:00 -07001217 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001218 if (mCurrentState.orientation != orientation) {
1219 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1220 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001221 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001222 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001223 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001224 orientation);
1225 }
1226 }
1227
Mathias Agopian698c0872011-06-28 19:09:31 -07001228 const size_t count = state.size();
1229 for (size_t i=0 ; i<count ; i++) {
1230 const ComposerState& s(state[i]);
1231 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001232 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001233 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001234
Mathias Agopian386aa982011-11-07 21:58:03 -08001235 if (transactionFlags) {
1236 // this triggers the transaction
1237 setTransactionFlags(transactionFlags);
1238
1239 // if this is a synchronous transaction, wait for it to take effect
1240 // before returning.
1241 if (flags & eSynchronous) {
1242 mTransationPending = true;
1243 }
1244 while (mTransationPending) {
1245 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1246 if (CC_UNLIKELY(err != NO_ERROR)) {
1247 // just in case something goes wrong in SF, return to the
1248 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001249 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001250 mTransationPending = false;
1251 break;
1252 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001253 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 }
1255}
1256
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001257int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001258 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259{
Glenn Kasten99ed2242011-12-15 09:51:17 -08001260 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 return BAD_VALUE;
1262
1263 Mutex::Autolock _l(mStateLock);
1264 if (mCurrentState.orientation != orientation) {
1265 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001266 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 mCurrentState.orientation = orientation;
1268 setTransactionFlags(eTransactionNeeded);
1269 mTransactionCV.wait(mStateLock);
1270 } else {
1271 orientation = BAD_VALUE;
1272 }
1273 }
1274 return orientation;
1275}
1276
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001277sp<ISurface> SurfaceFlinger::createSurface(
1278 ISurfaceComposerClient::surface_data_t* params,
1279 const String8& name,
1280 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1282 uint32_t flags)
1283{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001284 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001285 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001286
1287 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001288 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001289 int(w), int(h));
1290 return surfaceHandle;
1291 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001292
Steve Block9d453682011-12-20 16:23:08 +00001293 //ALOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001294 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 switch (flags & eFXSurfaceMask) {
1296 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001297 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1298 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 break;
1300 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001301 // for now we treat Blur as Dim, until we can implement it
1302 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001304 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001306 case eFXSurfaceScreenshot:
1307 layer = createScreenshotSurface(client, d, w, h, flags);
1308 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 }
1310
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001311 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001312 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001313 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001314 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001317 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001319 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001320 if (normalLayer != 0) {
1321 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001322 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001323 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001324 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001325
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328
1329 return surfaceHandle;
1330}
1331
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001332sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001333 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001334 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001335 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336{
1337 // initialize the surfaces
1338 switch (format) { // TODO: take h/w into account
1339 case PIXEL_FORMAT_TRANSPARENT:
1340 case PIXEL_FORMAT_TRANSLUCENT:
1341 format = PIXEL_FORMAT_RGBA_8888;
1342 break;
1343 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001344#ifdef NO_RGBX_8888
1345 format = PIXEL_FORMAT_RGB_565;
1346#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001347 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001348#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 break;
1350 }
1351
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001352#ifdef NO_RGBX_8888
1353 if (format == PIXEL_FORMAT_RGBX_8888)
1354 format = PIXEL_FORMAT_RGBA_8888;
1355#endif
1356
Mathias Agopian96f08192010-06-02 23:28:45 -07001357 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001358 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001359 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001360 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001361 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 }
1363 return layer;
1364}
1365
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001366sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001367 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001368 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369{
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001371 return layer;
1372}
1373
1374sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1375 const sp<Client>& client, DisplayID display,
1376 uint32_t w, uint32_t h, uint32_t flags)
1377{
1378 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 return layer;
1380}
1381
Mathias Agopian96f08192010-06-02 23:28:45 -07001382status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383{
Mathias Agopian9a112062009-04-17 19:36:26 -07001384 /*
1385 * called by the window manager, when a surface should be marked for
1386 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001387 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001388 * The surface is removed from the current and drawing lists, but placed
1389 * in the purgatory queue, so it's not destroyed right-away (we need
1390 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001391 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001392
Mathias Agopian48d819a2009-09-10 19:41:18 -07001393 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001394 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001395 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001396 if (layer != 0) {
1397 err = purgatorizeLayer_l(layer);
1398 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001399 setTransactionFlags(eTransactionNeeded);
1400 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001401 }
1402 return err;
1403}
1404
Mathias Agopianca4d3602011-05-19 15:38:14 -07001405status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001406{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001407 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001408 status_t err = NO_ERROR;
1409 sp<LayerBaseClient> l(layer.promote());
1410 if (l != NULL) {
1411 Mutex::Autolock _l(mStateLock);
1412 err = removeLayer_l(l);
1413 if (err == NAME_NOT_FOUND) {
1414 // The surface wasn't in the current list, which means it was
1415 // removed already, which means it is in the purgatory,
1416 // and need to be removed from there.
1417 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001418 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001419 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001420 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001421 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001422 "error removing layer=%p (%s)", l.get(), strerror(-err));
1423 }
1424 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425}
1426
Mathias Agopian698c0872011-06-28 19:09:31 -07001427uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001428 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001429 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001432 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1433 if (layer != 0) {
1434 const uint32_t what = s.what;
1435 if (what & ePositionChanged) {
1436 if (layer->setPosition(s.x, s.y))
1437 flags |= eTraversalNeeded;
1438 }
1439 if (what & eLayerChanged) {
1440 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1441 if (layer->setLayer(s.z)) {
1442 mCurrentState.layersSortedByZ.removeAt(idx);
1443 mCurrentState.layersSortedByZ.add(layer);
1444 // we need traversal (state changed)
1445 // AND transaction (list changed)
1446 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447 }
1448 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001449 if (what & eSizeChanged) {
1450 if (layer->setSize(s.w, s.h)) {
1451 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001452 }
1453 }
1454 if (what & eAlphaChanged) {
1455 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1456 flags |= eTraversalNeeded;
1457 }
1458 if (what & eMatrixChanged) {
1459 if (layer->setMatrix(s.matrix))
1460 flags |= eTraversalNeeded;
1461 }
1462 if (what & eTransparentRegionChanged) {
1463 if (layer->setTransparentRegionHint(s.transparentRegion))
1464 flags |= eTraversalNeeded;
1465 }
1466 if (what & eVisibilityChanged) {
1467 if (layer->setFlags(s.flags, s.mask))
1468 flags |= eTraversalNeeded;
1469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001471 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472}
1473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474void SurfaceFlinger::screenReleased(int dpy)
1475{
1476 // this may be called by a signal handler, we can't do too much in here
1477 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1478 signalEvent();
1479}
1480
1481void SurfaceFlinger::screenAcquired(int dpy)
1482{
1483 // this may be called by a signal handler, we can't do too much in here
1484 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1485 signalEvent();
1486}
1487
1488status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1489{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001490 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 char buffer[SIZE];
1492 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001493
1494 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 snprintf(buffer, SIZE, "Permission Denial: "
1496 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1497 IPCThreadState::self()->getCallingPid(),
1498 IPCThreadState::self()->getCallingUid());
1499 result.append(buffer);
1500 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001501
1502 // figure out if we're stuck somewhere
1503 const nsecs_t now = systemTime();
1504 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1505 const nsecs_t inTransaction(mDebugInTransaction);
1506 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1507 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1508
1509 // Try to get the main lock, but don't insist if we can't
1510 // (this would indicate SF is stuck, but we want to be able to
1511 // print something in dumpsys).
1512 int retry = 3;
1513 while (mStateLock.tryLock()<0 && --retry>=0) {
1514 usleep(1000000);
1515 }
1516 const bool locked(retry >= 0);
1517 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001518 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001519 "SurfaceFlinger appears to be unresponsive, "
1520 "dumping anyways (no locks held)\n");
1521 result.append(buffer);
1522 }
1523
Mathias Agopian48b888a2011-01-19 16:15:53 -08001524 /*
1525 * Dump the visible layer list
1526 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1528 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001529 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1530 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001532 const sp<LayerBase>& layer(currentLayers[i]);
1533 layer->dump(result, buffer, SIZE);
1534 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 s.transparentRegion.dump(result, "transparentRegion");
1536 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1537 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1538 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001539
Mathias Agopian48b888a2011-01-19 16:15:53 -08001540 /*
1541 * Dump the layers in the purgatory
1542 */
1543
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001544 const size_t purgatorySize = mLayerPurgatory.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001545 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1546 result.append(buffer);
1547 for (size_t i=0 ; i<purgatorySize ; i++) {
1548 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1549 layer->shortDump(result, buffer, SIZE);
1550 }
1551
1552 /*
1553 * Dump SurfaceFlinger global state
1554 */
1555
Mathias Agopianad701862011-07-14 18:01:49 -07001556 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001557 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001558
1559 const GLExtensions& extensions(GLExtensions::getInstance());
1560 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1561 extensions.getVendor(),
1562 extensions.getRenderer(),
1563 extensions.getVersion());
1564 result.append(buffer);
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001565
1566 snprintf(buffer, SIZE, "EGL : %s\n",
1567 eglQueryString(graphicPlane(0).getEGLDisplay(),
1568 EGL_VERSION_HW_ANDROID));
1569 result.append(buffer);
1570
Mathias Agopianad701862011-07-14 18:01:49 -07001571 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1572 result.append(buffer);
1573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 mWormholeRegion.dump(result, "WormholeRegion");
1575 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1576 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001577 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 mCurrentState.orientation, hw.canDraw());
1579 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001580 snprintf(buffer, SIZE,
1581 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001582 " last transaction time : %f us\n"
1583 " refresh-rate : %f fps\n"
1584 " x-dpi : %f\n"
1585 " y-dpi : %f\n",
1586 mLastSwapBufferTime/1000.0,
1587 mLastTransactionTime/1000.0,
1588 hw.getRefreshRate(),
1589 hw.getDpiX(),
1590 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001591 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001592
Mathias Agopian9795c422009-08-26 16:36:26 -07001593 if (inSwapBuffersDuration || !locked) {
1594 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1595 inSwapBuffersDuration/1000.0);
1596 result.append(buffer);
1597 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001598
Mathias Agopian9795c422009-08-26 16:36:26 -07001599 if (inTransactionDuration || !locked) {
1600 snprintf(buffer, SIZE, " transaction time: %f us\n",
1601 inTransactionDuration/1000.0);
1602 result.append(buffer);
1603 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001604
Mathias Agopian48b888a2011-01-19 16:15:53 -08001605 /*
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001606 * VSYNC state
1607 */
1608 mEventThread->dump(result, buffer, SIZE);
1609
1610 /*
Mathias Agopian48b888a2011-01-19 16:15:53 -08001611 * Dump HWComposer state
1612 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001613 HWComposer& hwc(hw.getHwComposer());
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001614 snprintf(buffer, SIZE, "h/w composer state:\n");
1615 result.append(buffer);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001616 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1617 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001618 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001619 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001620 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001621
Mathias Agopian48b888a2011-01-19 16:15:53 -08001622 /*
1623 * Dump gralloc state
1624 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001625 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001626 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001627 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001628
1629 if (locked) {
1630 mStateLock.unlock();
1631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 }
1633 write(fd, result.string(), result.size());
1634 return NO_ERROR;
1635}
1636
1637status_t SurfaceFlinger::onTransact(
1638 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1639{
1640 switch (code) {
1641 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001642 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001645 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001646 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 {
1648 // codes that require permission check
1649 IPCThreadState* ipc = IPCThreadState::self();
1650 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001651 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001652 if ((uid != AID_GRAPHICS) &&
1653 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001654 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001655 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1656 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001658 break;
1659 }
1660 case CAPTURE_SCREEN:
1661 {
1662 // codes that require permission check
1663 IPCThreadState* ipc = IPCThreadState::self();
1664 const int pid = ipc->getCallingPid();
1665 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001666 if ((uid != AID_GRAPHICS) &&
1667 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001668 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001669 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1670 return PERMISSION_DENIED;
1671 }
1672 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 }
1674 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1677 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001678 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001679 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001680 IPCThreadState* ipc = IPCThreadState::self();
1681 const int pid = ipc->getCallingPid();
1682 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001683 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001684 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 return PERMISSION_DENIED;
1686 }
1687 int n;
1688 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001689 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001690 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 return NO_ERROR;
1692 case 1002: // SHOW_UPDATES
1693 n = data.readInt32();
1694 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001695 invalidateHwcGeometry();
1696 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 return NO_ERROR;
1698 case 1003: // SHOW_BACKGROUND
1699 n = data.readInt32();
1700 mDebugBackground = n ? 1 : 0;
1701 return NO_ERROR;
1702 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001703 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001704 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001706 case 1005:{ // force transaction
1707 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1708 return NO_ERROR;
1709 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001710 case 1006:{ // enable/disable GraphicLog
1711 int enabled = data.readInt32();
1712 GraphicLog::getInstance().setEnabled(enabled);
1713 return NO_ERROR;
1714 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001715 case 1008: // toggle use of hw composer
1716 n = data.readInt32();
1717 mDebugDisableHWC = n ? 1 : 0;
1718 invalidateHwcGeometry();
1719 repaintEverything();
1720 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001721 case 1009: // toggle use of transform hint
1722 n = data.readInt32();
1723 mDebugDisableTransformHint = n ? 1 : 0;
1724 invalidateHwcGeometry();
1725 repaintEverything();
1726 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001728 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 reply->writeInt32(0);
1730 reply->writeInt32(mDebugRegion);
1731 reply->writeInt32(mDebugBackground);
1732 return NO_ERROR;
1733 case 1013: {
1734 Mutex::Autolock _l(mStateLock);
1735 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1736 reply->writeInt32(hw.getPageFlipCount());
1737 }
1738 return NO_ERROR;
1739 }
1740 }
1741 return err;
1742}
1743
Mathias Agopian53331da2011-08-22 21:44:41 -07001744void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001745 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001746 const Rect bounds(hw.getBounds());
1747 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001748 signalEvent();
1749}
1750
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001751void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1752 Mutex::Autolock _l(mInvalidateLock);
1753 mInvalidateRegion = reg;
1754}
1755
1756Region SurfaceFlinger::getAndClearInvalidateRegion() {
1757 Mutex::Autolock _l(mInvalidateLock);
1758 Region reg(mInvalidateRegion);
1759 mInvalidateRegion.clear();
1760 return reg;
1761}
1762
Mathias Agopian59119e62010-10-11 12:37:43 -07001763// ---------------------------------------------------------------------------
1764
Mathias Agopian118d0242011-10-13 16:02:48 -07001765status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1766 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1767{
1768 Mutex::Autolock _l(mStateLock);
1769 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1770}
1771
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001772status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1773 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001774{
Mathias Agopian59119e62010-10-11 12:37:43 -07001775 if (!GLExtensions::getInstance().haveFramebufferObject())
1776 return INVALID_OPERATION;
1777
1778 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001779 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001780 const uint32_t hw_w = hw.getWidth();
1781 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001782 GLfloat u = 1;
1783 GLfloat v = 1;
1784
1785 // make sure to clear all GL error flags
1786 while ( glGetError() != GL_NO_ERROR ) ;
1787
1788 // create a FBO
1789 GLuint name, tname;
1790 glGenTextures(1, &tname);
1791 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001792 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1793 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001794 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001795 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 GLint tw = (2 << (31 - clz(hw_w)));
1797 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001798 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1799 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 u = GLfloat(hw_w) / tw;
1801 v = GLfloat(hw_h) / th;
1802 }
1803 glGenFramebuffersOES(1, &name);
1804 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001805 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1806 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001807
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001808 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001809 glDisable(GL_TEXTURE_EXTERNAL_OES);
1810 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001811 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001812 glClearColor(0,0,0,1);
1813 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001814 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001815 glMatrixMode(GL_MODELVIEW);
1816 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001817 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1818 const size_t count = layers.size();
1819 for (size_t i=0 ; i<count ; ++i) {
1820 const sp<LayerBase>& layer(layers[i]);
1821 layer->drawForSreenShot();
1822 }
1823
Mathias Agopian118d0242011-10-13 16:02:48 -07001824 hw.compositionComplete();
1825
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001826 // back to main framebuffer
1827 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1828 glDisable(GL_SCISSOR_TEST);
1829 glDeleteFramebuffersOES(1, &name);
1830
1831 *textureName = tname;
1832 *uOut = u;
1833 *vOut = v;
1834 return NO_ERROR;
1835}
1836
1837// ---------------------------------------------------------------------------
1838
1839status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1840{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001841 // get screen geometry
1842 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1843 const uint32_t hw_w = hw.getWidth();
1844 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001845 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001846
1847 GLfloat u, v;
1848 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001849 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001850 if (result != NO_ERROR) {
1851 return result;
1852 }
1853
1854 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001855 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 glBindTexture(GL_TEXTURE_2D, tname);
1857 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1858 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1859 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001860 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1861 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001862 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1863 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1864 glVertexPointer(2, GL_FLOAT, 0, vtx);
1865
Mathias Agopianffcf4652011-07-07 17:30:31 -07001866 /*
1867 * Texture coordinate mapping
1868 *
1869 * u
1870 * 1 +----------+---+
1871 * | | | | image is inverted
1872 * | V | | w.r.t. the texture
1873 * 1-v +----------+ | coordinates
1874 * | |
1875 * | |
1876 * | |
1877 * 0 +--------------+
1878 * 0 1
1879 *
1880 */
1881
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001882 class s_curve_interpolator {
1883 const float nbFrames, s, v;
1884 public:
1885 s_curve_interpolator(int nbFrames, float s)
1886 : nbFrames(1.0f / (nbFrames-1)), s(s),
1887 v(1.0f + expf(-s + 0.5f*s)) {
1888 }
1889 float operator()(int f) {
1890 const float x = f * nbFrames;
1891 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1892 }
1893 };
1894
1895 class v_stretch {
1896 const GLfloat hw_w, hw_h;
1897 public:
1898 v_stretch(uint32_t hw_w, uint32_t hw_h)
1899 : hw_w(hw_w), hw_h(hw_h) {
1900 }
1901 void operator()(GLfloat* vtx, float v) {
1902 const GLfloat w = hw_w + (hw_w * v);
1903 const GLfloat h = hw_h - (hw_h * v);
1904 const GLfloat x = (hw_w - w) * 0.5f;
1905 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001906 vtx[0] = x; vtx[1] = y;
1907 vtx[2] = x; vtx[3] = y + h;
1908 vtx[4] = x + w; vtx[5] = y + h;
1909 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001910 }
1911 };
1912
1913 class h_stretch {
1914 const GLfloat hw_w, hw_h;
1915 public:
1916 h_stretch(uint32_t hw_w, uint32_t hw_h)
1917 : hw_w(hw_w), hw_h(hw_h) {
1918 }
1919 void operator()(GLfloat* vtx, float v) {
1920 const GLfloat w = hw_w - (hw_w * v);
1921 const GLfloat h = 1.0f;
1922 const GLfloat x = (hw_w - w) * 0.5f;
1923 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001924 vtx[0] = x; vtx[1] = y;
1925 vtx[2] = x; vtx[3] = y + h;
1926 vtx[4] = x + w; vtx[5] = y + h;
1927 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001928 }
1929 };
1930
1931 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001932 char value[PROPERTY_VALUE_MAX];
1933 property_get("debug.sf.electron_frames", value, "24");
1934 int nbFrames = (atoi(value) + 1) >> 1;
1935 if (nbFrames <= 0) // just in case
1936 nbFrames = 24;
1937
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001938 s_curve_interpolator itr(nbFrames, 7.5f);
1939 s_curve_interpolator itg(nbFrames, 8.0f);
1940 s_curve_interpolator itb(nbFrames, 8.5f);
1941
1942 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001943
1944 glMatrixMode(GL_TEXTURE);
1945 glLoadIdentity();
1946 glMatrixMode(GL_MODELVIEW);
1947 glLoadIdentity();
1948
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001949 glEnable(GL_BLEND);
1950 glBlendFunc(GL_ONE, GL_ONE);
1951 for (int i=0 ; i<nbFrames ; i++) {
1952 float x, y, w, h;
1953 const float vr = itr(i);
1954 const float vg = itg(i);
1955 const float vb = itb(i);
1956
1957 // clear screen
1958 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001959 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001960 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001961
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001962 // draw the red plane
1963 vverts(vtx, vr);
1964 glColorMask(1,0,0,1);
1965 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001966
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001967 // draw the green plane
1968 vverts(vtx, vg);
1969 glColorMask(0,1,0,1);
1970 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001971
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 // draw the blue plane
1973 vverts(vtx, vb);
1974 glColorMask(0,0,1,1);
1975 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001976
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001978 glDisable(GL_TEXTURE_2D);
1979 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001980 glColor4f(vg, vg, vg, 1);
1981 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1982 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 }
1984
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 h_stretch hverts(hw_w, hw_h);
1986 glDisable(GL_BLEND);
1987 glDisable(GL_TEXTURE_2D);
1988 glColorMask(1,1,1,1);
1989 for (int i=0 ; i<nbFrames ; i++) {
1990 const float v = itg(i);
1991 hverts(vtx, v);
1992 glClear(GL_COLOR_BUFFER_BIT);
1993 glColor4f(1-v, 1-v, 1-v, 1);
1994 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1995 hw.flip(screenBounds);
1996 }
1997
1998 glColorMask(1,1,1,1);
1999 glEnable(GL_SCISSOR_TEST);
2000 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2001 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002002 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002003 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 return NO_ERROR;
2005}
2006
2007status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2008{
2009 status_t result = PERMISSION_DENIED;
2010
2011 if (!GLExtensions::getInstance().haveFramebufferObject())
2012 return INVALID_OPERATION;
2013
2014
2015 // get screen geometry
2016 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2017 const uint32_t hw_w = hw.getWidth();
2018 const uint32_t hw_h = hw.getHeight();
2019 const Region screenBounds(hw.bounds());
2020
2021 GLfloat u, v;
2022 GLuint tname;
2023 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2024 if (result != NO_ERROR) {
2025 return result;
2026 }
2027
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002028 GLfloat vtx[8];
2029 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002030 glBindTexture(GL_TEXTURE_2D, tname);
2031 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2032 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2033 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002034 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2035 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002036 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2037 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2038 glVertexPointer(2, GL_FLOAT, 0, vtx);
2039
2040 class s_curve_interpolator {
2041 const float nbFrames, s, v;
2042 public:
2043 s_curve_interpolator(int nbFrames, float s)
2044 : nbFrames(1.0f / (nbFrames-1)), s(s),
2045 v(1.0f + expf(-s + 0.5f*s)) {
2046 }
2047 float operator()(int f) {
2048 const float x = f * nbFrames;
2049 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2050 }
2051 };
2052
2053 class v_stretch {
2054 const GLfloat hw_w, hw_h;
2055 public:
2056 v_stretch(uint32_t hw_w, uint32_t hw_h)
2057 : hw_w(hw_w), hw_h(hw_h) {
2058 }
2059 void operator()(GLfloat* vtx, float v) {
2060 const GLfloat w = hw_w + (hw_w * v);
2061 const GLfloat h = hw_h - (hw_h * v);
2062 const GLfloat x = (hw_w - w) * 0.5f;
2063 const GLfloat y = (hw_h - h) * 0.5f;
2064 vtx[0] = x; vtx[1] = y;
2065 vtx[2] = x; vtx[3] = y + h;
2066 vtx[4] = x + w; vtx[5] = y + h;
2067 vtx[6] = x + w; vtx[7] = y;
2068 }
2069 };
2070
2071 class h_stretch {
2072 const GLfloat hw_w, hw_h;
2073 public:
2074 h_stretch(uint32_t hw_w, uint32_t hw_h)
2075 : hw_w(hw_w), hw_h(hw_h) {
2076 }
2077 void operator()(GLfloat* vtx, float v) {
2078 const GLfloat w = hw_w - (hw_w * v);
2079 const GLfloat h = 1.0f;
2080 const GLfloat x = (hw_w - w) * 0.5f;
2081 const GLfloat y = (hw_h - h) * 0.5f;
2082 vtx[0] = x; vtx[1] = y;
2083 vtx[2] = x; vtx[3] = y + h;
2084 vtx[4] = x + w; vtx[5] = y + h;
2085 vtx[6] = x + w; vtx[7] = y;
2086 }
2087 };
2088
Mathias Agopiana6546e52010-10-14 12:33:07 -07002089 // the full animation is 12 frames
2090 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002091 s_curve_interpolator itr(nbFrames, 7.5f);
2092 s_curve_interpolator itg(nbFrames, 8.0f);
2093 s_curve_interpolator itb(nbFrames, 8.5f);
2094
2095 h_stretch hverts(hw_w, hw_h);
2096 glDisable(GL_BLEND);
2097 glDisable(GL_TEXTURE_2D);
2098 glColorMask(1,1,1,1);
2099 for (int i=nbFrames-1 ; i>=0 ; i--) {
2100 const float v = itg(i);
2101 hverts(vtx, v);
2102 glClear(GL_COLOR_BUFFER_BIT);
2103 glColor4f(1-v, 1-v, 1-v, 1);
2104 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2105 hw.flip(screenBounds);
2106 }
2107
Mathias Agopiana6546e52010-10-14 12:33:07 -07002108 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002109 v_stretch vverts(hw_w, hw_h);
2110 glEnable(GL_BLEND);
2111 glBlendFunc(GL_ONE, GL_ONE);
2112 for (int i=nbFrames-1 ; i>=0 ; i--) {
2113 float x, y, w, h;
2114 const float vr = itr(i);
2115 const float vg = itg(i);
2116 const float vb = itb(i);
2117
2118 // clear screen
2119 glColorMask(1,1,1,1);
2120 glClear(GL_COLOR_BUFFER_BIT);
2121 glEnable(GL_TEXTURE_2D);
2122
2123 // draw the red plane
2124 vverts(vtx, vr);
2125 glColorMask(1,0,0,1);
2126 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2127
2128 // draw the green plane
2129 vverts(vtx, vg);
2130 glColorMask(0,1,0,1);
2131 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2132
2133 // draw the blue plane
2134 vverts(vtx, vb);
2135 glColorMask(0,0,1,1);
2136 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2137
2138 hw.flip(screenBounds);
2139 }
2140
2141 glColorMask(1,1,1,1);
2142 glEnable(GL_SCISSOR_TEST);
2143 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002144 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002145 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002146 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002147
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002148 return NO_ERROR;
2149}
2150
2151// ---------------------------------------------------------------------------
2152
Mathias Agopianabd671a2010-10-14 14:54:06 -07002153status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002154{
2155 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2156 if (!hw.canDraw()) {
2157 // we're already off
2158 return NO_ERROR;
2159 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002160
2161 // turn off hwc while we're doing the animation
2162 hw.getHwComposer().disable();
2163 // and make sure to turn it back on (if needed) next time we compose
2164 invalidateHwcGeometry();
2165
Mathias Agopianabd671a2010-10-14 14:54:06 -07002166 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2167 electronBeamOffAnimationImplLocked();
2168 }
2169
2170 // always clear the whole screen at the end of the animation
2171 glClearColor(0,0,0,1);
2172 glDisable(GL_SCISSOR_TEST);
2173 glClear(GL_COLOR_BUFFER_BIT);
2174 glEnable(GL_SCISSOR_TEST);
2175 hw.flip( Region(hw.bounds()) );
2176
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002177 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002178}
2179
2180status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2181{
Mathias Agopian59119e62010-10-11 12:37:43 -07002182 class MessageTurnElectronBeamOff : public MessageBase {
2183 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002184 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002185 status_t result;
2186 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002187 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2188 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002189 }
2190 status_t getResult() const {
2191 return result;
2192 }
2193 virtual bool handler() {
2194 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002195 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002196 return true;
2197 }
2198 };
2199
Mathias Agopianabd671a2010-10-14 14:54:06 -07002200 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002201 status_t res = postMessageSync(msg);
2202 if (res == NO_ERROR) {
2203 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002204
2205 // work-around: when the power-manager calls us we activate the
2206 // animation. eventually, the "on" animation will be called
2207 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002208 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002209 }
2210 return res;
2211}
2212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002213// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002214
Mathias Agopianabd671a2010-10-14 14:54:06 -07002215status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002216{
2217 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2218 if (hw.canDraw()) {
2219 // we're already on
2220 return NO_ERROR;
2221 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002222 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2223 electronBeamOnAnimationImplLocked();
2224 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002225
2226 // make sure to redraw the whole screen when the animation is done
2227 mDirtyRegion.set(hw.bounds());
2228 signalEvent();
2229
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002230 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231}
2232
2233status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2234{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002235 class MessageTurnElectronBeamOn : public MessageBase {
2236 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002237 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002238 status_t result;
2239 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002240 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2241 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002242 }
2243 status_t getResult() const {
2244 return result;
2245 }
2246 virtual bool handler() {
2247 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002248 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002249 return true;
2250 }
2251 };
2252
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254 return NO_ERROR;
2255}
2256
2257// ---------------------------------------------------------------------------
2258
Mathias Agopian74c40c02010-09-29 13:02:36 -07002259status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2260 sp<IMemoryHeap>* heap,
2261 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002262 uint32_t sw, uint32_t sh,
2263 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002264{
2265 status_t result = PERMISSION_DENIED;
2266
2267 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002268 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002269 return BAD_VALUE;
2270
2271 if (!GLExtensions::getInstance().haveFramebufferObject())
2272 return INVALID_OPERATION;
2273
2274 // get screen geometry
2275 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2276 const uint32_t hw_w = hw.getWidth();
2277 const uint32_t hw_h = hw.getHeight();
2278
2279 if ((sw > hw_w) || (sh > hw_h))
2280 return BAD_VALUE;
2281
2282 sw = (!sw) ? hw_w : sw;
2283 sh = (!sh) ? hw_h : sh;
2284 const size_t size = sw * sh * 4;
2285
Steve Block9d453682011-12-20 16:23:08 +00002286 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002287 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002288
Mathias Agopian74c40c02010-09-29 13:02:36 -07002289 // make sure to clear all GL error flags
2290 while ( glGetError() != GL_NO_ERROR ) ;
2291
2292 // create a FBO
2293 GLuint name, tname;
2294 glGenRenderbuffersOES(1, &tname);
2295 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2296 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2297 glGenFramebuffersOES(1, &name);
2298 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2299 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2300 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2301
2302 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002303
Mathias Agopian74c40c02010-09-29 13:02:36 -07002304 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2305
2306 // invert everything, b/c glReadPixel() below will invert the FB
2307 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002308 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002309 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310 glMatrixMode(GL_PROJECTION);
2311 glPushMatrix();
2312 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002313 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 glMatrixMode(GL_MODELVIEW);
2315
2316 // redraw the screen entirely...
2317 glClearColor(0,0,0,1);
2318 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002319
Jamie Gennis9575f602011-10-07 14:51:16 -07002320 const LayerVector& layers(mDrawingState.layersSortedByZ);
2321 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 for (size_t i=0 ; i<count ; ++i) {
2323 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002324 const uint32_t flags = layer->drawingState().flags;
2325 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2326 const uint32_t z = layer->drawingState().z;
2327 if (z >= minLayerZ && z <= maxLayerZ) {
2328 layer->drawForSreenShot();
2329 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002330 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002331 }
2332
2333 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002334 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 glScissor(0, 0, sw, sh);
2336
2337 // check for errors and return screen capture
2338 if (glGetError() != GL_NO_ERROR) {
2339 // error while rendering
2340 result = INVALID_OPERATION;
2341 } else {
2342 // allocate shared memory large enough to hold the
2343 // screen capture
2344 sp<MemoryHeapBase> base(
2345 new MemoryHeapBase(size, 0, "screen-capture") );
2346 void* const ptr = base->getBase();
2347 if (ptr) {
2348 // capture the screen with glReadPixels()
2349 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2350 if (glGetError() == GL_NO_ERROR) {
2351 *heap = base;
2352 *w = sw;
2353 *h = sh;
2354 *f = PIXEL_FORMAT_RGBA_8888;
2355 result = NO_ERROR;
2356 }
2357 } else {
2358 result = NO_MEMORY;
2359 }
2360 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361 glEnable(GL_SCISSOR_TEST);
2362 glViewport(0, 0, hw_w, hw_h);
2363 glMatrixMode(GL_PROJECTION);
2364 glPopMatrix();
2365 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002366 } else {
2367 result = BAD_VALUE;
2368 }
2369
2370 // release FBO resources
2371 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2372 glDeleteRenderbuffersOES(1, &tname);
2373 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002374
2375 hw.compositionComplete();
2376
Steve Block9d453682011-12-20 16:23:08 +00002377 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002378
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 return result;
2380}
2381
2382
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002383status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2384 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002385 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002386 uint32_t sw, uint32_t sh,
2387 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002388{
2389 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002390 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002391 return BAD_VALUE;
2392
2393 if (!GLExtensions::getInstance().haveFramebufferObject())
2394 return INVALID_OPERATION;
2395
2396 class MessageCaptureScreen : public MessageBase {
2397 SurfaceFlinger* flinger;
2398 DisplayID dpy;
2399 sp<IMemoryHeap>* heap;
2400 uint32_t* w;
2401 uint32_t* h;
2402 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002403 uint32_t sw;
2404 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002405 uint32_t minLayerZ;
2406 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002407 status_t result;
2408 public:
2409 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002410 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002411 uint32_t sw, uint32_t sh,
2412 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002413 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002414 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2415 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2416 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002417 {
2418 }
2419 status_t getResult() const {
2420 return result;
2421 }
2422 virtual bool handler() {
2423 Mutex::Autolock _l(flinger->mStateLock);
2424
2425 // if we have secure windows, never allow the screen capture
2426 if (flinger->mSecureFrameBuffer)
2427 return true;
2428
Mathias Agopian74c40c02010-09-29 13:02:36 -07002429 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002430 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002431
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002432 return true;
2433 }
2434 };
2435
2436 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002437 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002438 status_t res = postMessageSync(msg);
2439 if (res == NO_ERROR) {
2440 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2441 }
2442 return res;
2443}
2444
2445// ---------------------------------------------------------------------------
2446
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002447sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2448{
2449 sp<Layer> result;
2450 Mutex::Autolock _l(mStateLock);
2451 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2452 return result;
2453}
2454
2455// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456
Mathias Agopian96f08192010-06-02 23:28:45 -07002457Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002458 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002459{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460}
2461
Mathias Agopian96f08192010-06-02 23:28:45 -07002462Client::~Client()
2463{
Mathias Agopian96f08192010-06-02 23:28:45 -07002464 const size_t count = mLayers.size();
2465 for (size_t i=0 ; i<count ; i++) {
2466 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2467 if (layer != 0) {
2468 mFlinger->removeLayer(layer);
2469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470 }
2471}
2472
Mathias Agopian96f08192010-06-02 23:28:45 -07002473status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002474 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002475}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002476
Mathias Agopian4f113742011-05-03 16:21:41 -07002477size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002478{
Mathias Agopian4f113742011-05-03 16:21:41 -07002479 Mutex::Autolock _l(mLock);
2480 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002481 mLayers.add(name, layer);
2482 return name;
2483}
2484
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002485void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002486{
Mathias Agopian4f113742011-05-03 16:21:41 -07002487 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002488 // we do a linear search here, because this doesn't happen often
2489 const size_t count = mLayers.size();
2490 for (size_t i=0 ; i<count ; i++) {
2491 if (mLayers.valueAt(i) == layer) {
2492 mLayers.removeItemsAt(i, 1);
2493 break;
2494 }
2495 }
2496}
Mathias Agopian4f113742011-05-03 16:21:41 -07002497sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2498{
2499 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002500 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002501 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002502 if (layer != 0) {
2503 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002504 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002505 }
2506 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507}
2508
Mathias Agopiana67932f2011-04-20 14:20:59 -07002509
2510status_t Client::onTransact(
2511 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2512{
2513 // these must be checked
2514 IPCThreadState* ipc = IPCThreadState::self();
2515 const int pid = ipc->getCallingPid();
2516 const int uid = ipc->getCallingUid();
2517 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002518 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002519 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002520 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002521 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002522 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002523 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2524 return PERMISSION_DENIED;
2525 }
2526 }
2527 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002528}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002529
2530
Mathias Agopian96f08192010-06-02 23:28:45 -07002531sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002532 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002533 const String8& name,
2534 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002535 uint32_t flags)
2536{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002537 /*
2538 * createSurface must be called from the GL thread so that it can
2539 * have access to the GL context.
2540 */
2541
2542 class MessageCreateSurface : public MessageBase {
2543 sp<ISurface> result;
2544 SurfaceFlinger* flinger;
2545 ISurfaceComposerClient::surface_data_t* params;
2546 Client* client;
2547 const String8& name;
2548 DisplayID display;
2549 uint32_t w, h;
2550 PixelFormat format;
2551 uint32_t flags;
2552 public:
2553 MessageCreateSurface(SurfaceFlinger* flinger,
2554 ISurfaceComposerClient::surface_data_t* params,
2555 const String8& name, Client* client,
2556 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2557 uint32_t flags)
2558 : flinger(flinger), params(params), client(client), name(name),
2559 display(display), w(w), h(h), format(format), flags(flags)
2560 {
2561 }
2562 sp<ISurface> getResult() const { return result; }
2563 virtual bool handler() {
2564 result = flinger->createSurface(params, name, client,
2565 display, w, h, format, flags);
2566 return true;
2567 }
2568 };
2569
2570 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2571 params, name, this, display, w, h, format, flags);
2572 mFlinger->postMessageSync(msg);
2573 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574}
Mathias Agopian96f08192010-06-02 23:28:45 -07002575status_t Client::destroySurface(SurfaceID sid) {
2576 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002578
2579// ---------------------------------------------------------------------------
2580
Jamie Gennis9a78c902011-01-12 18:30:40 -08002581GraphicBufferAlloc::GraphicBufferAlloc() {}
2582
2583GraphicBufferAlloc::~GraphicBufferAlloc() {}
2584
2585sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002586 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002587 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2588 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002589 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002590 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002591 if (err == NO_MEMORY) {
2592 GraphicBuffer::dumpAllocationsToSystemLog();
2593 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002594 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002595 "failed (%s), handle=%p",
2596 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002597 return 0;
2598 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002599 return graphicBuffer;
2600}
2601
Jamie Gennis9a78c902011-01-12 18:30:40 -08002602// ---------------------------------------------------------------------------
2603
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002604GraphicPlane::GraphicPlane()
2605 : mHw(0)
2606{
2607}
2608
2609GraphicPlane::~GraphicPlane() {
2610 delete mHw;
2611}
2612
2613bool GraphicPlane::initialized() const {
2614 return mHw ? true : false;
2615}
2616
Mathias Agopian2b92d892010-02-08 15:49:35 -08002617int GraphicPlane::getWidth() const {
2618 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619}
2620
Mathias Agopian2b92d892010-02-08 15:49:35 -08002621int GraphicPlane::getHeight() const {
2622 return mHeight;
2623}
2624
2625void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2626{
2627 mHw = hw;
2628
2629 // initialize the display orientation transform.
2630 // it's a constant that should come from the display driver.
2631 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2632 char property[PROPERTY_VALUE_MAX];
2633 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2634 //displayOrientation
2635 switch (atoi(property)) {
2636 case 90:
2637 displayOrientation = ISurfaceComposer::eOrientation90;
2638 break;
2639 case 270:
2640 displayOrientation = ISurfaceComposer::eOrientation270;
2641 break;
2642 }
2643 }
2644
2645 const float w = hw->getWidth();
2646 const float h = hw->getHeight();
2647 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2648 &mDisplayTransform);
2649 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2650 mDisplayWidth = h;
2651 mDisplayHeight = w;
2652 } else {
2653 mDisplayWidth = w;
2654 mDisplayHeight = h;
2655 }
2656
2657 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658}
2659
2660status_t GraphicPlane::orientationToTransfrom(
2661 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002662{
2663 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002664 switch (orientation) {
2665 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002666 flags = Transform::ROT_0;
2667 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002669 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670 break;
2671 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002672 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673 break;
2674 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002675 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002676 break;
2677 default:
2678 return BAD_VALUE;
2679 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002680 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002681 return NO_ERROR;
2682}
2683
2684status_t GraphicPlane::setOrientation(int orientation)
2685{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686 // If the rotation can be handled in hardware, this is where
2687 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002688
2689 const DisplayHardware& hw(displayHardware());
2690 const float w = mDisplayWidth;
2691 const float h = mDisplayHeight;
2692 mWidth = int(w);
2693 mHeight = int(h);
2694
2695 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002696 GraphicPlane::orientationToTransfrom(orientation, w, h,
2697 &orientationTransform);
2698 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2699 mWidth = int(h);
2700 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002701 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002702
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002703 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002704 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002705 return NO_ERROR;
2706}
2707
2708const DisplayHardware& GraphicPlane::displayHardware() const {
2709 return *mHw;
2710}
2711
Mathias Agopian59119e62010-10-11 12:37:43 -07002712DisplayHardware& GraphicPlane::editDisplayHardware() {
2713 return *mHw;
2714}
2715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716const Transform& GraphicPlane::transform() const {
2717 return mGlobalTransform;
2718}
2719
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002720EGLDisplay GraphicPlane::getEGLDisplay() const {
2721 return mHw->getEGLDisplay();
2722}
2723
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002724// ---------------------------------------------------------------------------
2725
2726}; // namespace android