blob: 93dd3a432e83ff6dd8180da282f83eb433a74ada [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
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080053#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054#include "GLExtensions.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
Glenn Kasten1db13d72011-12-19 13:55:34 -080063#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include <private/gui/SharedBufferStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showbackground", value, "0");
115 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
Colin Cross3854ed52012-03-23 14:17:18 -0700117#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
Colin Cross3854ed52012-03-23 14:17:18 -0700123#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Steve Blocka19954a2012-01-04 20:05:49 +0000125 ALOGI_IF(mDebugRegion, "showupdates enabled");
126 ALOGI_IF(mDebugBackground, "showbackground enabled");
127 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128}
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130void SurfaceFlinger::onFirstRef()
131{
132 mEventQueue.init(this);
133
134 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
135
136 // Wait for the main thread to be done with its initialization
137 mReadyToRunBarrier.wait();
138}
139
140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141SurfaceFlinger::~SurfaceFlinger()
142{
143 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146void SurfaceFlinger::binderDied(const wp<IBinder>& who)
147{
148 // the window manager died on us. prepare its eulogy.
149
150 // reset screen orientation
151 Vector<ComposerState> state;
152 setTransactionState(state, eOrientationDefault, 0);
153
154 // restart the boot-animation
155 property_set("ctl.start", "bootanim");
156}
157
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700158sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700160 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161}
162
Mathias Agopian7e27f052010-05-28 14:22:23 -0700163sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Mathias Agopian96f08192010-06-02 23:28:45 -0700165 sp<ISurfaceComposerClient> bclient;
166 sp<Client> client(new Client(this));
167 status_t err = client->initCheck();
168 if (err == NO_ERROR) {
169 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 return bclient;
172}
173
Jamie Gennis9a78c902011-01-12 18:30:40 -0800174sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
175{
176 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
177 return gba;
178}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
181{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000182 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 const GraphicPlane& plane(mGraphicPlanes[dpy]);
184 return plane;
185}
186
187GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
188{
189 return const_cast<GraphicPlane&>(
190 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
191}
192
193void SurfaceFlinger::bootFinished()
194{
195 const nsecs_t now = systemTime();
196 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000197 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700199
200 // wait patiently for the window manager death
201 const String16 name("window");
202 sp<IBinder> window(defaultServiceManager()->getService(name));
203 if (window != 0) {
204 window->linkToDeath(this);
205 }
206
207 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700208 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209}
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211static inline uint16_t pack565(int r, int g, int b) {
212 return (r<<11)|(g<<5)|b;
213}
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215status_t SurfaceFlinger::readyToRun()
216{
Steve Blocka19954a2012-01-04 20:05:49 +0000217 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 "Initializing graphics H/W...");
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 // we only support one display currently
221 int dpy = 0;
222
223 {
224 // initialize the main display
225 GraphicPlane& plane(graphicPlane(dpy));
226 DisplayHardware* const hw = new DisplayHardware(this, dpy);
227 plane.setDisplayHardware(hw);
228 }
229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 // create the shared control-block
231 mServerHeap = new MemoryHeapBase(4096,
232 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700235 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700237
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700238 new(mServerCblk) surface_flinger_cblk_t;
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 // initialize primary screen
241 // (other display should be initialized in the same manner, but
242 // asynchronously, as they could come and go. None of this is supported
243 // yet).
244 const GraphicPlane& plane(graphicPlane(dpy));
245 const DisplayHardware& hw = plane.displayHardware();
246 const uint32_t w = hw.getWidth();
247 const uint32_t h = hw.getHeight();
248 const uint32_t f = hw.getFormat();
249 hw.makeCurrent();
250
251 // initialize the shared control block
252 mServerCblk->connected |= 1<<dpy;
253 display_cblk_t* dcblk = mServerCblk->displays + dpy;
254 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800255 dcblk->w = plane.getWidth();
256 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 dcblk->format = f;
258 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
259 dcblk->xdpi = hw.getDpiX();
260 dcblk->ydpi = hw.getDpiY();
261 dcblk->fps = hw.getRefreshRate();
262 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
264 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700266 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 glShadeModel(GL_FLAT);
269 glDisable(GL_DITHER);
270 glDisable(GL_CULL_FACE);
271
272 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
273 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700274 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 glGenTextures(1, &mWormholeTexName);
276 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
281 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700282 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
283
284 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
285 glGenTextures(1, &mProtectedTexName);
286 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
290 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
291 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
292 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293
294 glViewport(0, 0, w, h);
295 glMatrixMode(GL_PROJECTION);
296 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700297 // put the origin in the left-bottom corner
298 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 -0800299
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800300
301 // start the EventThread
302 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800303 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800304 hw.startSleepManagement();
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// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800319
Jamie Gennis582270d2011-08-17 18:19:00 -0700320bool SurfaceFlinger::authenticateSurfaceTexture(
321 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800322 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700323 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800324
325 // Check the visible layer list for the ISurface
326 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
327 size_t count = currentLayers.size();
328 for (size_t i=0 ; i<count ; i++) {
329 const sp<LayerBase>& layer(currentLayers[i]);
330 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700331 if (lbc != NULL) {
332 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
333 if (lbcBinder == surfaceTextureBinder) {
334 return true;
335 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800336 }
337 }
338
339 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700340 // SurfaceTexture gets destroyed before all the clients are done using it,
341 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800342 // will instead fail later on when the client tries to use the surface,
343 // which should be reported as "surface XYZ returned an -ENODEV". The
344 // purgatorized layers are no less authentic than the visible ones, so this
345 // should not cause any harm.
346 size_t purgatorySize = mLayerPurgatory.size();
347 for (size_t i=0 ; i<purgatorySize ; i++) {
348 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
349 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700350 if (lbc != NULL) {
351 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
352 if (lbcBinder == surfaceTextureBinder) {
353 return true;
354 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800355 }
356 }
357
358 return false;
359}
360
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800361// ----------------------------------------------------------------------------
362
363sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800364 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700365}
366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800368
369void SurfaceFlinger::waitForEvent() {
370 mEventQueue.waitMessage();
371}
372
373void SurfaceFlinger::signalTransaction() {
374 mEventQueue.invalidate();
375}
376
377void SurfaceFlinger::signalLayerUpdate() {
378 mEventQueue.invalidate();
379}
380
381void SurfaceFlinger::signalRefresh() {
382 mEventQueue.refresh();
383}
384
385status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
386 nsecs_t reltime, uint32_t flags) {
387 return mEventQueue.postMessage(msg, reltime);
388}
389
390status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
391 nsecs_t reltime, uint32_t flags) {
392 status_t res = mEventQueue.postMessage(msg, reltime);
393 if (res == NO_ERROR) {
394 msg->wait();
395 }
396 return res;
397}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800398
399bool SurfaceFlinger::threadLoop()
400{
401 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 return true;
403}
404
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800405void SurfaceFlinger::onMessageReceived(int32_t what)
406{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800407 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800408 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700409 case MessageQueue::REFRESH: {
410// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800411 // if we're in a global transaction, don't do anything.
412 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
413 uint32_t transactionFlags = peekTransactionFlags(mask);
414 if (CC_UNLIKELY(transactionFlags)) {
415 handleTransaction(transactionFlags);
416 }
417
418 // post surfaces (if needed)
419 handlePageFlip();
420
Mathias Agopian69a655c2012-04-11 20:43:19 -0700421// signalRefresh();
422//
423// } break;
424//
425// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800426
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800427 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800428
Mathias Agopian303d5382012-02-05 01:49:16 -0800429 const DisplayHardware& hw(graphicPlane(0).displayHardware());
430
Mathias Agopian69a655c2012-04-11 20:43:19 -0700431// if (mDirtyRegion.isEmpty()) {
432// return;
433// }
434
Mathias Agopianb048cef2012-02-04 15:44:04 -0800435 if (CC_UNLIKELY(mHwWorkListDirty)) {
436 // build the h/w work list
437 handleWorkList();
438 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800439
Mathias Agopianb048cef2012-02-04 15:44:04 -0800440 if (CC_LIKELY(hw.canDraw())) {
441 // repaint the framebuffer (if needed)
442 handleRepaint();
443 // inform the h/w that we're done compositing
444 hw.compositionComplete();
445 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800446 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800447 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800448 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800449 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800450
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800451 } break;
452 }
453}
454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455void SurfaceFlinger::postFramebuffer()
456{
Mathias Agopian841cde52012-03-01 15:44:37 -0800457 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800458 // mSwapRegion can be empty here is some cases, for instance if a hidden
459 // or fully transparent window is updating.
460 // in that case, we need to flip anyways to not risk a deadlock with
461 // h/w composer.
462
Mathias Agopiana44b0412011-10-16 18:46:35 -0700463 const DisplayHardware& hw(graphicPlane(0).displayHardware());
464 const nsecs_t now = systemTime();
465 mDebugInSwapBuffers = now;
466 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800467
468 size_t numLayers = mVisibleLayersSortedByZ.size();
469 for (size_t i = 0; i < numLayers; i++) {
470 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
471 }
472
Mathias Agopiana44b0412011-10-16 18:46:35 -0700473 mLastSwapBufferTime = systemTime() - now;
474 mDebugInSwapBuffers = 0;
475 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
479{
Mathias Agopian841cde52012-03-01 15:44:37 -0800480 ATRACE_CALL();
481
Mathias Agopianca4d3602011-05-19 15:38:14 -0700482 Mutex::Autolock _l(mStateLock);
483 const nsecs_t now = systemTime();
484 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485
Mathias Agopianca4d3602011-05-19 15:38:14 -0700486 // Here we're guaranteed that some transaction flags are set
487 // so we can call handleTransactionLocked() unconditionally.
488 // We call getTransactionFlags(), which will also clear the flags,
489 // with mStateLock held to guarantee that mCurrentState won't change
490 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
493 transactionFlags = getTransactionFlags(mask);
494 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700495
Mathias Agopianca4d3602011-05-19 15:38:14 -0700496 mLastTransactionTime = systemTime() - now;
497 mDebugInTransaction = 0;
498 invalidateHwcGeometry();
499 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700500}
501
Mathias Agopianca4d3602011-05-19 15:38:14 -0700502void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700503{
504 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 const size_t count = currentLayers.size();
506
507 /*
508 * Traversal of the children
509 * (perform the transaction for each of them if needed)
510 */
511
512 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
513 if (layersNeedTransaction) {
514 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700515 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
517 if (!trFlags) continue;
518
519 const uint32_t flags = layer->doTransaction(0);
520 if (flags & Layer::eVisibleRegion)
521 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 }
523 }
524
525 /*
526 * Perform our own transaction if needed
527 */
528
529 if (transactionFlags & eTransactionNeeded) {
530 if (mCurrentState.orientation != mDrawingState.orientation) {
531 // the orientation has changed, recompute all visible regions
532 // and invalidate everything.
533
534 const int dpy = 0;
535 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700536 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537 GraphicPlane& plane(graphicPlane(dpy));
538 plane.setOrientation(orientation);
539
540 // update the shared control block
541 const DisplayHardware& hw(plane.displayHardware());
542 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
543 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800544 dcblk->w = plane.getWidth();
545 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546
547 mVisibleRegionsDirty = true;
548 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800549 }
550
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700551 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
552 // layers have been added
553 mVisibleRegionsDirty = true;
554 }
555
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 // some layers might have been removed, so
557 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700558 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700559 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700561 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700562 const size_t count = previousLayers.size();
563 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 const sp<LayerBase>& layer(previousLayers[i]);
565 if (currentLayers.indexOf( layer ) < 0) {
566 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700567 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700568 }
569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571 }
572
573 commitTransaction();
574}
575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800577 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578{
Mathias Agopian841cde52012-03-01 15:44:37 -0800579 ATRACE_CALL();
580
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 const GraphicPlane& plane(graphicPlane(0));
582 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700583 const DisplayHardware& hw(plane.displayHardware());
584 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585
586 Region aboveOpaqueLayers;
587 Region aboveCoveredLayers;
588 Region dirty;
589
590 bool secureFrameBuffer = false;
591
592 size_t i = currentLayers.size();
593 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700594 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 layer->validateVisibility(planeTransform);
596
597 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700598 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599
Mathias Agopianab028732010-03-16 16:41:46 -0700600 /*
601 * opaqueRegion: area of a surface that is fully opaque.
602 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700604
605 /*
606 * visibleRegion: area of a surface that is visible on screen
607 * and not fully transparent. This is essentially the layer's
608 * footprint minus the opaque regions above it.
609 * Areas covered by a translucent surface are considered visible.
610 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700612
613 /*
614 * coveredRegion: area of a surface that is covered by all
615 * visible regions above it (which includes the translucent areas).
616 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700618
619
620 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800621 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700622 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700623 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700625 visibleRegion.andSelf(screenRegion);
626 if (!visibleRegion.isEmpty()) {
627 // Remove the transparent area from the visible region
628 if (translucent) {
629 visibleRegion.subtractSelf(layer->transparentRegionScreen);
630 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631
Mathias Agopianab028732010-03-16 16:41:46 -0700632 // compute the opaque region
633 const int32_t layerOrientation = layer->getOrientation();
634 if (s.alpha==255 && !translucent &&
635 ((layerOrientation & Transform::ROT_INVALID) == false)) {
636 // the opaque region is the layer's footprint
637 opaqueRegion = visibleRegion;
638 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 }
640 }
641
Mathias Agopianab028732010-03-16 16:41:46 -0700642 // Clip the covered region to the visible region
643 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
644
645 // Update aboveCoveredLayers for next (lower) layer
646 aboveCoveredLayers.orSelf(visibleRegion);
647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800648 // subtract the opaque region covered by the layers above us
649 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
651 // compute this layer's dirty region
652 if (layer->contentDirty) {
653 // we need to invalidate the whole region
654 dirty = visibleRegion;
655 // as well, as the old visible region
656 dirty.orSelf(layer->visibleRegionScreen);
657 layer->contentDirty = false;
658 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700659 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700660 * the exposed region consists of two components:
661 * 1) what's VISIBLE now and was COVERED before
662 * 2) what's EXPOSED now less what was EXPOSED before
663 *
664 * note that (1) is conservative, we start with the whole
665 * visible region but only keep what used to be covered by
666 * something -- which mean it may have been exposed.
667 *
668 * (2) handles areas that were not covered by anything but got
669 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700670 */
Mathias Agopianab028732010-03-16 16:41:46 -0700671 const Region newExposed = visibleRegion - coveredRegion;
672 const Region oldVisibleRegion = layer->visibleRegionScreen;
673 const Region oldCoveredRegion = layer->coveredRegionScreen;
674 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
675 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 }
677 dirty.subtractSelf(aboveOpaqueLayers);
678
679 // accumulate to the screen dirty region
680 dirtyRegion.orSelf(dirty);
681
Mathias Agopianab028732010-03-16 16:41:46 -0700682 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 // Store the visible region is screen space
686 layer->setVisibleRegion(visibleRegion);
687 layer->setCoveredRegion(coveredRegion);
688
Mathias Agopian97011222009-07-28 10:57:27 -0700689 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 if (layer->isSecure() && !visibleRegion.isEmpty()) {
691 secureFrameBuffer = true;
692 }
693 }
694
Mathias Agopian97011222009-07-28 10:57:27 -0700695 // invalidate the areas where a layer was removed
696 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
697 mDirtyRegionRemovedLayer.clear();
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 mSecureFrameBuffer = secureFrameBuffer;
700 opaqueRegion = aboveOpaqueLayers;
701}
702
703
704void SurfaceFlinger::commitTransaction()
705{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800706 if (!mLayersPendingRemoval.isEmpty()) {
707 // Notify removed layers now that they can't be drawn from
708 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
709 mLayersPendingRemoval[i]->onRemoved();
710 }
711 mLayersPendingRemoval.clear();
712 }
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700715 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700716 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717}
718
719void SurfaceFlinger::handlePageFlip()
720{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800721 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800722 const DisplayHardware& hw = graphicPlane(0).displayHardware();
723 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800725 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
726 const bool visibleRegions = lockPageFlip(currentLayers);
727
728 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 Region opaqueRegion;
730 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700731
732 /*
733 * rebuild the visible layer list
734 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800735 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700736 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700737 mVisibleLayersSortedByZ.setCapacity(count);
738 for (size_t i=0 ; i<count ; i++) {
739 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
740 mVisibleLayersSortedByZ.add(currentLayers[i]);
741 }
742
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743 mWormholeRegion = screenRegion.subtract(opaqueRegion);
744 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800745 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 }
747
748 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700749
750 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 mDirtyRegion.andSelf(screenRegion);
752}
753
Mathias Agopianad456f92011-01-13 17:53:01 -0800754void SurfaceFlinger::invalidateHwcGeometry()
755{
756 mHwWorkListDirty = true;
757}
758
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
760{
761 bool recomputeVisibleRegions = false;
762 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700763 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700765 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 layer->lockPageFlip(recomputeVisibleRegions);
767 }
768 return recomputeVisibleRegions;
769}
770
771void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
772{
773 const GraphicPlane& plane(graphicPlane(0));
774 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800775 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700776 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700778 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 layer->unlockPageFlip(planeTransform, mDirtyRegion);
780 }
781}
782
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800783void SurfaceFlinger::handleRefresh()
784{
785 bool needInvalidate = false;
786 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
787 const size_t count = currentLayers.size();
788 for (size_t i=0 ; i<count ; i++) {
789 const sp<LayerBase>& layer(currentLayers[i]);
790 if (layer->onPreComposition()) {
791 needInvalidate = true;
792 }
793 }
794 if (needInvalidate) {
795 signalLayerUpdate();
796 }
797}
798
799
Mathias Agopiana350ff92010-08-10 17:14:02 -0700800void SurfaceFlinger::handleWorkList()
801{
802 mHwWorkListDirty = false;
803 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
804 if (hwc.initCheck() == NO_ERROR) {
805 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
806 const size_t count = currentLayers.size();
807 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700808 hwc_layer_t* const cur(hwc.getLayers());
809 for (size_t i=0 ; cur && i<count ; i++) {
810 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700811 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700812 cur[i].compositionType = HWC_FRAMEBUFFER;
813 cur[i].flags |= HWC_SKIP_LAYER;
814 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700815 }
816 }
817}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819void SurfaceFlinger::handleRepaint()
820{
Mathias Agopian841cde52012-03-01 15:44:37 -0800821 ATRACE_CALL();
822
Mathias Agopianb8a55602009-06-26 19:06:36 -0700823 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700824 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825
Glenn Kasten99ed2242011-12-15 09:51:17 -0800826 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 debugFlashRegions();
828 }
829
Mathias Agopianb8a55602009-06-26 19:06:36 -0700830 // set the frame buffer
831 const DisplayHardware& hw(graphicPlane(0).displayHardware());
832 glMatrixMode(GL_MODELVIEW);
833 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834
835 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700836 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700837 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
838 // takes a rectangle, we must make sure to update that whole
839 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700840 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700842 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 // We need to redraw the rectangle that will be updated
844 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700845 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700846 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700847 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700848 } else {
849 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700851 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 }
853 }
854
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700855 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 composeSurfaces(mDirtyRegion);
857
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700858 // update the swap region and clear the dirty region
859 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 mDirtyRegion.clear();
861}
862
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700863void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700865 const DisplayHardware& hw(graphicPlane(0).displayHardware());
866 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700867 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700868 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700869 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700870 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700871
Mathias Agopianf384cc32011-09-08 18:31:55 -0700872 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
873 size_t count = layers.size();
874
Steve Blocke6f43dd2012-01-06 19:20:56 +0000875 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700876 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
877 hwc.getNumLayers(), count);
878
879 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700880 if (hwc.initCheck() == NO_ERROR) {
881 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
882 }
Mathias Agopian45721772010-08-12 15:03:26 -0700883
884 /*
885 * update the per-frame h/w composer data for each layer
886 * and build the transparent region of the FB
887 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700888 for (size_t i=0 ; i<count ; i++) {
889 const sp<LayerBase>& layer(layers[i]);
890 layer->setPerFrameData(&cur[i]);
891 }
892 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000893 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700894}
Mathias Agopian45721772010-08-12 15:03:26 -0700895
Mathias Agopianf384cc32011-09-08 18:31:55 -0700896void SurfaceFlinger::composeSurfaces(const Region& dirty)
897{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700898 const DisplayHardware& hw(graphicPlane(0).displayHardware());
899 HWComposer& hwc(hw.getHwComposer());
900
901 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700902 if (fbLayerCount) {
903 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700904
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700905 if (!mWormholeRegion.isEmpty()) {
906 // can happen with SurfaceView
907 drawWormhole();
908 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700909
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700910 /*
911 * and then, render the layers targeted at the framebuffer
912 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700913
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700914 hwc_layer_t* const cur(hwc.getLayers());
915 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
916 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700917
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700918 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700919 const sp<LayerBase>& layer(layers[i]);
920 const Region clip(dirty.intersect(layer->visibleRegionScreen));
921 if (!clip.isEmpty()) {
922 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
923 if ((cur[i].hints & HWC_HINT_CLEAR_FB)
924 && layer->isOpaque()) {
925 layer->clearWithOpenGL(clip);
926 }
927 continue;
928 }
929 // render the layer
930 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700931 }
932 }
933 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934}
935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936void SurfaceFlinger::debugFlashRegions()
937{
Mathias Agopian0a917752010-06-14 21:20:00 -0700938 const DisplayHardware& hw(graphicPlane(0).displayHardware());
939 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700940 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700941 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700942 return;
943 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700944
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700945 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
947 mDirtyRegion.bounds() : hw.bounds());
948 composeSurfaces(repaint);
949 }
950
Mathias Agopianc492e672011-10-18 14:49:27 -0700951 glDisable(GL_TEXTURE_EXTERNAL_OES);
952 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 glDisable(GL_SCISSOR_TEST);
955
Mathias Agopian0926f502009-05-04 14:17:04 -0700956 static int toggle = 0;
957 toggle = 1 - toggle;
958 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700959 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700960 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700961 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700962 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963
Mathias Agopian20f68782009-05-11 00:03:41 -0700964 Region::const_iterator it = mDirtyRegion.begin();
965 Region::const_iterator const end = mDirtyRegion.end();
966 while (it != end) {
967 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700969 { r.left, height - r.top },
970 { r.left, height - r.bottom },
971 { r.right, height - r.bottom },
972 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 };
974 glVertexPointer(2, GL_FLOAT, 0, vertices);
975 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
976 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700977
Mathias Agopian0656a682011-09-20 17:22:44 -0700978 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979
980 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700981 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982}
983
984void SurfaceFlinger::drawWormhole() const
985{
986 const Region region(mWormholeRegion.intersect(mDirtyRegion));
987 if (region.isEmpty())
988 return;
989
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700990 glDisable(GL_TEXTURE_EXTERNAL_OES);
991 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
Glenn Kasten99ed2242011-12-15 09:51:17 -0800993 if (CC_LIKELY(!mDebugBackground)) {
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700994 glDisable(GL_TEXTURE_2D);
995 glColor4f(0,0,0,0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 } else {
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700997 const DisplayHardware& hw(graphicPlane(0).displayHardware());
998 const int32_t width = hw.getWidth();
999 const int32_t height = hw.getHeight();
1000 const GLfloat tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1001 glTexCoordPointer(2, GL_FLOAT, 0, tcoords);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1003 glEnable(GL_TEXTURE_2D);
1004 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1005 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1006 glMatrixMode(GL_TEXTURE);
1007 glLoadIdentity();
1008 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 }
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001010
1011 GLfloat vertices[4][2];
1012 glVertexPointer(2, GL_FLOAT, 0, vertices);
1013 Region::const_iterator it = region.begin();
1014 Region::const_iterator const end = region.end();
1015 while (it != end) {
1016 const Rect& r = *it++;
1017 vertices[0][0] = r.left;
1018 vertices[0][1] = r.top;
1019 vertices[1][0] = r.right;
1020 vertices[1][1] = r.top;
1021 vertices[2][0] = r.right;
1022 vertices[2][1] = r.bottom;
1023 vertices[3][0] = r.left;
1024 vertices[3][1] = r.bottom;
1025 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1026 }
1027
1028 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1029 glDisable(GL_TEXTURE_2D);
1030 glLoadIdentity();
1031 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032}
1033
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001034status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035{
1036 Mutex::Autolock _l(mStateLock);
1037 addLayer_l(layer);
1038 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1039 return NO_ERROR;
1040}
1041
Mathias Agopian96f08192010-06-02 23:28:45 -07001042status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1043{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001044 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001045 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1046}
1047
1048ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1049 const sp<LayerBaseClient>& lbc)
1050{
Mathias Agopian96f08192010-06-02 23:28:45 -07001051 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001052 size_t name = client->attachLayer(lbc);
1053
1054 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001055
1056 // add this layer to the current state list
1057 addLayer_l(lbc);
1058
Mathias Agopian4f113742011-05-03 16:21:41 -07001059 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001060}
1061
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001062status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063{
1064 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001065 status_t err = purgatorizeLayer_l(layer);
1066 if (err == NO_ERROR)
1067 setTransactionFlags(eTransactionNeeded);
1068 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001071status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001073 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1074 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001075 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001076 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1078 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001079 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 return NO_ERROR;
1081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083}
1084
Mathias Agopian9a112062009-04-17 19:36:26 -07001085status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1086{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001087 // First add the layer to the purgatory list, which makes sure it won't
1088 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001089 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001090 if (err >= 0) {
1091 mLayerPurgatory.add(layerBase);
1092 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001093
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001094 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001095
Mathias Agopian3d579642009-06-04 18:46:21 -07001096 // it's possible that we don't find a layer, because it might
1097 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001098 // from the user because there is a race between Client::destroySurface(),
1099 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001100 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1101}
1102
Mathias Agopian96f08192010-06-02 23:28:45 -07001103status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104{
Mathias Agopian96f08192010-06-02 23:28:45 -07001105 layer->forceVisibilityTransaction();
1106 setTransactionFlags(eTraversalNeeded);
1107 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}
1109
Mathias Agopiandea20b12011-05-03 17:04:02 -07001110uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1111{
1112 return android_atomic_release_load(&mTransactionFlags);
1113}
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1116{
1117 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1118}
1119
Mathias Agopianbb641242010-05-18 17:06:55 -07001120uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121{
1122 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1123 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001124 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 }
1126 return old;
1127}
1128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001130void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001131 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001132 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001133
Jamie Gennis28378392011-10-12 17:39:00 -07001134 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001135 if (mCurrentState.orientation != orientation) {
1136 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1137 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001138 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001139 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001140 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001141 orientation);
1142 }
1143 }
1144
Mathias Agopian698c0872011-06-28 19:09:31 -07001145 const size_t count = state.size();
1146 for (size_t i=0 ; i<count ; i++) {
1147 const ComposerState& s(state[i]);
1148 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001149 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001150 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001151
Mathias Agopian386aa982011-11-07 21:58:03 -08001152 if (transactionFlags) {
1153 // this triggers the transaction
1154 setTransactionFlags(transactionFlags);
1155
1156 // if this is a synchronous transaction, wait for it to take effect
1157 // before returning.
1158 if (flags & eSynchronous) {
1159 mTransationPending = true;
1160 }
1161 while (mTransationPending) {
1162 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1163 if (CC_UNLIKELY(err != NO_ERROR)) {
1164 // just in case something goes wrong in SF, return to the
1165 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001166 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001167 mTransationPending = false;
1168 break;
1169 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 }
1172}
1173
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001174sp<ISurface> SurfaceFlinger::createSurface(
1175 ISurfaceComposerClient::surface_data_t* params,
1176 const String8& name,
1177 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1179 uint32_t flags)
1180{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001181 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001182 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001183
1184 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001185 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001186 int(w), int(h));
1187 return surfaceHandle;
1188 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001189
Mathias Agopianff615cc2012-02-24 14:58:36 -08001190 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001191 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 switch (flags & eFXSurfaceMask) {
1193 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001194 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1195 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 break;
1197 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001198 // for now we treat Blur as Dim, until we can implement it
1199 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001201 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001203 case eFXSurfaceScreenshot:
1204 layer = createScreenshotSurface(client, d, w, h, flags);
1205 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
1207
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001208 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001209 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001210 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001211 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001214 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001215 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001216 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001217 if (normalLayer != 0) {
1218 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001219 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001220 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001221 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001222
Mathias Agopian96f08192010-06-02 23:28:45 -07001223 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 }
1225
1226 return surfaceHandle;
1227}
1228
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001229sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001230 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001231 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001232 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
1234 // initialize the surfaces
1235 switch (format) { // TODO: take h/w into account
1236 case PIXEL_FORMAT_TRANSPARENT:
1237 case PIXEL_FORMAT_TRANSLUCENT:
1238 format = PIXEL_FORMAT_RGBA_8888;
1239 break;
1240 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001241#ifdef NO_RGBX_8888
1242 format = PIXEL_FORMAT_RGB_565;
1243#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001244 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001245#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 break;
1247 }
1248
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001249#ifdef NO_RGBX_8888
1250 if (format == PIXEL_FORMAT_RGBX_8888)
1251 format = PIXEL_FORMAT_RGBA_8888;
1252#endif
1253
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001255 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001256 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001257 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001258 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 }
1260 return layer;
1261}
1262
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001263sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001264 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001265 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
Mathias Agopian96f08192010-06-02 23:28:45 -07001267 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001268 return layer;
1269}
1270
1271sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1272 const sp<Client>& client, DisplayID display,
1273 uint32_t w, uint32_t h, uint32_t flags)
1274{
1275 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001276 return layer;
1277}
1278
Mathias Agopian96f08192010-06-02 23:28:45 -07001279status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280{
Mathias Agopian9a112062009-04-17 19:36:26 -07001281 /*
1282 * called by the window manager, when a surface should be marked for
1283 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001284 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001285 * The surface is removed from the current and drawing lists, but placed
1286 * in the purgatory queue, so it's not destroyed right-away (we need
1287 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001288 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001289
Mathias Agopian48d819a2009-09-10 19:41:18 -07001290 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001291 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001293
Mathias Agopian48d819a2009-09-10 19:41:18 -07001294 if (layer != 0) {
1295 err = purgatorizeLayer_l(layer);
1296 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001297 setTransactionFlags(eTransactionNeeded);
1298 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001299 }
1300 return err;
1301}
1302
Mathias Agopianca4d3602011-05-19 15:38:14 -07001303status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001304{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001305 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001306 status_t err = NO_ERROR;
1307 sp<LayerBaseClient> l(layer.promote());
1308 if (l != NULL) {
1309 Mutex::Autolock _l(mStateLock);
1310 err = removeLayer_l(l);
1311 if (err == NAME_NOT_FOUND) {
1312 // The surface wasn't in the current list, which means it was
1313 // removed already, which means it is in the purgatory,
1314 // and need to be removed from there.
1315 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001316 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001317 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001318 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001319 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001320 "error removing layer=%p (%s)", l.get(), strerror(-err));
1321 }
1322 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323}
1324
Mathias Agopian698c0872011-06-28 19:09:31 -07001325uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001326 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001327 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001330 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1331 if (layer != 0) {
1332 const uint32_t what = s.what;
1333 if (what & ePositionChanged) {
1334 if (layer->setPosition(s.x, s.y))
1335 flags |= eTraversalNeeded;
1336 }
1337 if (what & eLayerChanged) {
1338 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1339 if (layer->setLayer(s.z)) {
1340 mCurrentState.layersSortedByZ.removeAt(idx);
1341 mCurrentState.layersSortedByZ.add(layer);
1342 // we need traversal (state changed)
1343 // AND transaction (list changed)
1344 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 }
1346 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001347 if (what & eSizeChanged) {
1348 if (layer->setSize(s.w, s.h)) {
1349 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001350 }
1351 }
1352 if (what & eAlphaChanged) {
1353 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1354 flags |= eTraversalNeeded;
1355 }
1356 if (what & eMatrixChanged) {
1357 if (layer->setMatrix(s.matrix))
1358 flags |= eTraversalNeeded;
1359 }
1360 if (what & eTransparentRegionChanged) {
1361 if (layer->setTransparentRegionHint(s.transparentRegion))
1362 flags |= eTraversalNeeded;
1363 }
1364 if (what & eVisibilityChanged) {
1365 if (layer->setFlags(s.flags, s.mask))
1366 flags |= eTraversalNeeded;
1367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001369 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
Mathias Agopianb60314a2012-04-10 22:09:54 -07001372// ---------------------------------------------------------------------------
1373
1374void SurfaceFlinger::onScreenAcquired() {
1375 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1376 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001377 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001378 // this is a temporary work-around, eventually this should be called
1379 // by the power-manager
1380 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001381 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001382 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383}
1384
Mathias Agopianb60314a2012-04-10 22:09:54 -07001385void SurfaceFlinger::onScreenReleased() {
1386 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1387 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001388 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001389 hw.releaseScreen();
1390 // from this point on, SF will stop drawing
1391 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392}
1393
Mathias Agopianb60314a2012-04-10 22:09:54 -07001394void SurfaceFlinger::screenAcquired() {
1395 class MessageScreenAcquired : public MessageBase {
1396 SurfaceFlinger* flinger;
1397 public:
1398 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1399 virtual bool handler() {
1400 flinger->onScreenAcquired();
1401 return true;
1402 }
1403 };
1404 sp<MessageBase> msg = new MessageScreenAcquired(this);
1405 postMessageSync(msg);
1406}
1407
1408void SurfaceFlinger::screenReleased() {
1409 class MessageScreenReleased : public MessageBase {
1410 SurfaceFlinger* flinger;
1411 public:
1412 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1413 virtual bool handler() {
1414 flinger->onScreenReleased();
1415 return true;
1416 }
1417 };
1418 sp<MessageBase> msg = new MessageScreenReleased(this);
1419 postMessageSync(msg);
1420}
1421
1422// ---------------------------------------------------------------------------
1423
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1425{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001426 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427 char buffer[SIZE];
1428 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001429
1430 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 snprintf(buffer, SIZE, "Permission Denial: "
1432 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1433 IPCThreadState::self()->getCallingPid(),
1434 IPCThreadState::self()->getCallingUid());
1435 result.append(buffer);
1436 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001437 // Try to get the main lock, but don't insist if we can't
1438 // (this would indicate SF is stuck, but we want to be able to
1439 // print something in dumpsys).
1440 int retry = 3;
1441 while (mStateLock.tryLock()<0 && --retry>=0) {
1442 usleep(1000000);
1443 }
1444 const bool locked(retry >= 0);
1445 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001446 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001447 "SurfaceFlinger appears to be unresponsive, "
1448 "dumping anyways (no locks held)\n");
1449 result.append(buffer);
1450 }
1451
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001452 bool dumpAll = true;
1453 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001454 size_t numArgs = args.size();
1455 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001456 if ((index < numArgs) &&
1457 (args[index] == String16("--list"))) {
1458 index++;
1459 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001460 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001461 }
1462
1463 if ((index < numArgs) &&
1464 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001465 index++;
1466 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001467 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001468 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001469
1470 if ((index < numArgs) &&
1471 (args[index] == String16("--latency-clear"))) {
1472 index++;
1473 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001474 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001475 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001477
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001478 if (dumpAll) {
1479 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001480 }
1481
Mathias Agopian9795c422009-08-26 16:36:26 -07001482 if (locked) {
1483 mStateLock.unlock();
1484 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 }
1486 write(fd, result.string(), result.size());
1487 return NO_ERROR;
1488}
1489
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001490void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1491 String8& result, char* buffer, size_t SIZE) const
1492{
1493 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1494 const size_t count = currentLayers.size();
1495 for (size_t i=0 ; i<count ; i++) {
1496 const sp<LayerBase>& layer(currentLayers[i]);
1497 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1498 result.append(buffer);
1499 }
1500}
1501
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001502void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1503 String8& result, char* buffer, size_t SIZE) const
1504{
1505 String8 name;
1506 if (index < args.size()) {
1507 name = String8(args[index]);
1508 index++;
1509 }
1510
1511 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1512 const size_t count = currentLayers.size();
1513 for (size_t i=0 ; i<count ; i++) {
1514 const sp<LayerBase>& layer(currentLayers[i]);
1515 if (name.isEmpty()) {
1516 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1517 result.append(buffer);
1518 }
1519 if (name.isEmpty() || (name == layer->getName())) {
1520 layer->dumpStats(result, buffer, SIZE);
1521 }
1522 }
1523}
1524
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001525void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1526 String8& result, char* buffer, size_t SIZE) const
1527{
1528 String8 name;
1529 if (index < args.size()) {
1530 name = String8(args[index]);
1531 index++;
1532 }
1533
1534 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1535 const size_t count = currentLayers.size();
1536 for (size_t i=0 ; i<count ; i++) {
1537 const sp<LayerBase>& layer(currentLayers[i]);
1538 if (name.isEmpty() || (name == layer->getName())) {
1539 layer->clearStats();
1540 }
1541 }
1542}
1543
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001544void SurfaceFlinger::dumpAllLocked(
1545 String8& result, char* buffer, size_t SIZE) const
1546{
1547 // figure out if we're stuck somewhere
1548 const nsecs_t now = systemTime();
1549 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1550 const nsecs_t inTransaction(mDebugInTransaction);
1551 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1552 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1553
1554 /*
1555 * Dump the visible layer list
1556 */
1557 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1558 const size_t count = currentLayers.size();
1559 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1560 result.append(buffer);
1561 for (size_t i=0 ; i<count ; i++) {
1562 const sp<LayerBase>& layer(currentLayers[i]);
1563 layer->dump(result, buffer, SIZE);
1564 }
1565
1566 /*
1567 * Dump the layers in the purgatory
1568 */
1569
1570 const size_t purgatorySize = mLayerPurgatory.size();
1571 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1572 result.append(buffer);
1573 for (size_t i=0 ; i<purgatorySize ; i++) {
1574 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1575 layer->shortDump(result, buffer, SIZE);
1576 }
1577
1578 /*
1579 * Dump SurfaceFlinger global state
1580 */
1581
1582 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1583 result.append(buffer);
1584
1585 const GLExtensions& extensions(GLExtensions::getInstance());
1586 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1587 extensions.getVendor(),
1588 extensions.getRenderer(),
1589 extensions.getVersion());
1590 result.append(buffer);
1591
1592 snprintf(buffer, SIZE, "EGL : %s\n",
1593 eglQueryString(graphicPlane(0).getEGLDisplay(),
1594 EGL_VERSION_HW_ANDROID));
1595 result.append(buffer);
1596
1597 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1598 result.append(buffer);
1599
1600 mWormholeRegion.dump(result, "WormholeRegion");
1601 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1602 snprintf(buffer, SIZE,
1603 " orientation=%d, canDraw=%d\n",
1604 mCurrentState.orientation, hw.canDraw());
1605 result.append(buffer);
1606 snprintf(buffer, SIZE,
1607 " last eglSwapBuffers() time: %f us\n"
1608 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001609 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001610 " refresh-rate : %f fps\n"
1611 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001612 " y-dpi : %f\n"
1613 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001614 mLastSwapBufferTime/1000.0,
1615 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001616 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001617 hw.getRefreshRate(),
1618 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001619 hw.getDpiY(),
1620 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001621 result.append(buffer);
1622
1623 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1624 inSwapBuffersDuration/1000.0);
1625 result.append(buffer);
1626
1627 snprintf(buffer, SIZE, " transaction time: %f us\n",
1628 inTransactionDuration/1000.0);
1629 result.append(buffer);
1630
1631 /*
1632 * VSYNC state
1633 */
1634 mEventThread->dump(result, buffer, SIZE);
1635
1636 /*
1637 * Dump HWComposer state
1638 */
1639 HWComposer& hwc(hw.getHwComposer());
1640 snprintf(buffer, SIZE, "h/w composer state:\n");
1641 result.append(buffer);
1642 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1643 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1644 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1645 result.append(buffer);
1646 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1647
1648 /*
1649 * Dump gralloc state
1650 */
1651 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1652 alloc.dump(result);
1653 hw.dump(result);
1654}
1655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656status_t SurfaceFlinger::onTransact(
1657 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1658{
1659 switch (code) {
1660 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001661 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001664 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001665 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 {
1667 // codes that require permission check
1668 IPCThreadState* ipc = IPCThreadState::self();
1669 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001670 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001671 if ((uid != AID_GRAPHICS) &&
1672 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001673 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001674 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1675 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001677 break;
1678 }
1679 case CAPTURE_SCREEN:
1680 {
1681 // codes that require permission check
1682 IPCThreadState* ipc = IPCThreadState::self();
1683 const int pid = ipc->getCallingPid();
1684 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001685 if ((uid != AID_GRAPHICS) &&
1686 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001687 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001688 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1689 return PERMISSION_DENIED;
1690 }
1691 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 }
1693 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1696 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001697 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001698 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001699 IPCThreadState* ipc = IPCThreadState::self();
1700 const int pid = ipc->getCallingPid();
1701 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001702 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001703 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 return PERMISSION_DENIED;
1705 }
1706 int n;
1707 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001708 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001709 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 return NO_ERROR;
1711 case 1002: // SHOW_UPDATES
1712 n = data.readInt32();
1713 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001714 invalidateHwcGeometry();
1715 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 return NO_ERROR;
1717 case 1003: // SHOW_BACKGROUND
1718 n = data.readInt32();
1719 mDebugBackground = n ? 1 : 0;
1720 return NO_ERROR;
1721 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001722 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001723 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001725 case 1005:{ // force transaction
1726 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1727 return NO_ERROR;
1728 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001729 case 1006:{ // send empty update
1730 signalRefresh();
1731 return NO_ERROR;
1732 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001733 case 1008: // toggle use of hw composer
1734 n = data.readInt32();
1735 mDebugDisableHWC = n ? 1 : 0;
1736 invalidateHwcGeometry();
1737 repaintEverything();
1738 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001739 case 1009: // toggle use of transform hint
1740 n = data.readInt32();
1741 mDebugDisableTransformHint = n ? 1 : 0;
1742 invalidateHwcGeometry();
1743 repaintEverything();
1744 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001746 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 reply->writeInt32(0);
1748 reply->writeInt32(mDebugRegion);
1749 reply->writeInt32(mDebugBackground);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001750 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751 return NO_ERROR;
1752 case 1013: {
1753 Mutex::Autolock _l(mStateLock);
1754 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1755 reply->writeInt32(hw.getPageFlipCount());
1756 }
1757 return NO_ERROR;
1758 }
1759 }
1760 return err;
1761}
1762
Mathias Agopian53331da2011-08-22 21:44:41 -07001763void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001764 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001765 const Rect bounds(hw.getBounds());
1766 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001767 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001768}
1769
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001770void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1771 Mutex::Autolock _l(mInvalidateLock);
1772 mInvalidateRegion = reg;
1773}
1774
1775Region SurfaceFlinger::getAndClearInvalidateRegion() {
1776 Mutex::Autolock _l(mInvalidateLock);
1777 Region reg(mInvalidateRegion);
1778 mInvalidateRegion.clear();
1779 return reg;
1780}
1781
Mathias Agopian59119e62010-10-11 12:37:43 -07001782// ---------------------------------------------------------------------------
1783
Mathias Agopian118d0242011-10-13 16:02:48 -07001784status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1785 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1786{
1787 Mutex::Autolock _l(mStateLock);
1788 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1789}
1790
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001791status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1792 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001793{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001794 ATRACE_CALL();
1795
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 if (!GLExtensions::getInstance().haveFramebufferObject())
1797 return INVALID_OPERATION;
1798
1799 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001801 const uint32_t hw_w = hw.getWidth();
1802 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001803 GLfloat u = 1;
1804 GLfloat v = 1;
1805
1806 // make sure to clear all GL error flags
1807 while ( glGetError() != GL_NO_ERROR ) ;
1808
1809 // create a FBO
1810 GLuint name, tname;
1811 glGenTextures(1, &tname);
1812 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001813 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1814 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001815 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1816 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001818 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001819 GLint tw = (2 << (31 - clz(hw_w)));
1820 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001821 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1822 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001823 u = GLfloat(hw_w) / tw;
1824 v = GLfloat(hw_h) / th;
1825 }
1826 glGenFramebuffersOES(1, &name);
1827 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1829 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001830
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001831 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001832 glDisable(GL_TEXTURE_EXTERNAL_OES);
1833 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001834 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001835 glClearColor(0,0,0,1);
1836 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001837 glMatrixMode(GL_MODELVIEW);
1838 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001839 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1840 const size_t count = layers.size();
1841 for (size_t i=0 ; i<count ; ++i) {
1842 const sp<LayerBase>& layer(layers[i]);
1843 layer->drawForSreenShot();
1844 }
1845
Mathias Agopian118d0242011-10-13 16:02:48 -07001846 hw.compositionComplete();
1847
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001848 // back to main framebuffer
1849 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1850 glDisable(GL_SCISSOR_TEST);
1851 glDeleteFramebuffersOES(1, &name);
1852
1853 *textureName = tname;
1854 *uOut = u;
1855 *vOut = v;
1856 return NO_ERROR;
1857}
1858
1859// ---------------------------------------------------------------------------
1860
1861status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1862{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001863 // get screen geometry
1864 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1865 const uint32_t hw_w = hw.getWidth();
1866 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001867 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001868
1869 GLfloat u, v;
1870 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001871 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001872 if (result != NO_ERROR) {
1873 return result;
1874 }
1875
1876 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001877 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001878 glBindTexture(GL_TEXTURE_2D, tname);
1879 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1880 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1881 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001882 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1883 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1885 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1886 glVertexPointer(2, GL_FLOAT, 0, vtx);
1887
Mathias Agopianffcf4652011-07-07 17:30:31 -07001888 /*
1889 * Texture coordinate mapping
1890 *
1891 * u
1892 * 1 +----------+---+
1893 * | | | | image is inverted
1894 * | V | | w.r.t. the texture
1895 * 1-v +----------+ | coordinates
1896 * | |
1897 * | |
1898 * | |
1899 * 0 +--------------+
1900 * 0 1
1901 *
1902 */
1903
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001904 class s_curve_interpolator {
1905 const float nbFrames, s, v;
1906 public:
1907 s_curve_interpolator(int nbFrames, float s)
1908 : nbFrames(1.0f / (nbFrames-1)), s(s),
1909 v(1.0f + expf(-s + 0.5f*s)) {
1910 }
1911 float operator()(int f) {
1912 const float x = f * nbFrames;
1913 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1914 }
1915 };
1916
1917 class v_stretch {
1918 const GLfloat hw_w, hw_h;
1919 public:
1920 v_stretch(uint32_t hw_w, uint32_t hw_h)
1921 : hw_w(hw_w), hw_h(hw_h) {
1922 }
1923 void operator()(GLfloat* vtx, float v) {
1924 const GLfloat w = hw_w + (hw_w * v);
1925 const GLfloat h = hw_h - (hw_h * v);
1926 const GLfloat x = (hw_w - w) * 0.5f;
1927 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001928 vtx[0] = x; vtx[1] = y;
1929 vtx[2] = x; vtx[3] = y + h;
1930 vtx[4] = x + w; vtx[5] = y + h;
1931 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001932 }
1933 };
1934
1935 class h_stretch {
1936 const GLfloat hw_w, hw_h;
1937 public:
1938 h_stretch(uint32_t hw_w, uint32_t hw_h)
1939 : hw_w(hw_w), hw_h(hw_h) {
1940 }
1941 void operator()(GLfloat* vtx, float v) {
1942 const GLfloat w = hw_w - (hw_w * v);
1943 const GLfloat h = 1.0f;
1944 const GLfloat x = (hw_w - w) * 0.5f;
1945 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001946 vtx[0] = x; vtx[1] = y;
1947 vtx[2] = x; vtx[3] = y + h;
1948 vtx[4] = x + w; vtx[5] = y + h;
1949 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001950 }
1951 };
1952
1953 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001954 char value[PROPERTY_VALUE_MAX];
1955 property_get("debug.sf.electron_frames", value, "24");
1956 int nbFrames = (atoi(value) + 1) >> 1;
1957 if (nbFrames <= 0) // just in case
1958 nbFrames = 24;
1959
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001960 s_curve_interpolator itr(nbFrames, 7.5f);
1961 s_curve_interpolator itg(nbFrames, 8.0f);
1962 s_curve_interpolator itb(nbFrames, 8.5f);
1963
1964 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001965
1966 glMatrixMode(GL_TEXTURE);
1967 glLoadIdentity();
1968 glMatrixMode(GL_MODELVIEW);
1969 glLoadIdentity();
1970
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 glEnable(GL_BLEND);
1972 glBlendFunc(GL_ONE, GL_ONE);
1973 for (int i=0 ; i<nbFrames ; i++) {
1974 float x, y, w, h;
1975 const float vr = itr(i);
1976 const float vg = itg(i);
1977 const float vb = itb(i);
1978
1979 // clear screen
1980 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001981 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001982 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001983
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001984 // draw the red plane
1985 vverts(vtx, vr);
1986 glColorMask(1,0,0,1);
1987 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001988
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001989 // draw the green plane
1990 vverts(vtx, vg);
1991 glColorMask(0,1,0,1);
1992 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001993
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001994 // draw the blue plane
1995 vverts(vtx, vb);
1996 glColorMask(0,0,1,1);
1997 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001998
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001999 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002000 glDisable(GL_TEXTURE_2D);
2001 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 glColor4f(vg, vg, vg, 1);
2003 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2004 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002005 }
2006
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007 h_stretch hverts(hw_w, hw_h);
2008 glDisable(GL_BLEND);
2009 glDisable(GL_TEXTURE_2D);
2010 glColorMask(1,1,1,1);
2011 for (int i=0 ; i<nbFrames ; i++) {
2012 const float v = itg(i);
2013 hverts(vtx, v);
2014 glClear(GL_COLOR_BUFFER_BIT);
2015 glColor4f(1-v, 1-v, 1-v, 1);
2016 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2017 hw.flip(screenBounds);
2018 }
2019
2020 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2022 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002023 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002024 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002025 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002026 return NO_ERROR;
2027}
2028
2029status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2030{
2031 status_t result = PERMISSION_DENIED;
2032
2033 if (!GLExtensions::getInstance().haveFramebufferObject())
2034 return INVALID_OPERATION;
2035
2036
2037 // get screen geometry
2038 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2039 const uint32_t hw_w = hw.getWidth();
2040 const uint32_t hw_h = hw.getHeight();
2041 const Region screenBounds(hw.bounds());
2042
2043 GLfloat u, v;
2044 GLuint tname;
2045 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2046 if (result != NO_ERROR) {
2047 return result;
2048 }
2049
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050 GLfloat vtx[8];
2051 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002052 glBindTexture(GL_TEXTURE_2D, tname);
2053 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2054 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2055 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002056 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2057 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002058 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2059 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2060 glVertexPointer(2, GL_FLOAT, 0, vtx);
2061
2062 class s_curve_interpolator {
2063 const float nbFrames, s, v;
2064 public:
2065 s_curve_interpolator(int nbFrames, float s)
2066 : nbFrames(1.0f / (nbFrames-1)), s(s),
2067 v(1.0f + expf(-s + 0.5f*s)) {
2068 }
2069 float operator()(int f) {
2070 const float x = f * nbFrames;
2071 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2072 }
2073 };
2074
2075 class v_stretch {
2076 const GLfloat hw_w, hw_h;
2077 public:
2078 v_stretch(uint32_t hw_w, uint32_t hw_h)
2079 : hw_w(hw_w), hw_h(hw_h) {
2080 }
2081 void operator()(GLfloat* vtx, float v) {
2082 const GLfloat w = hw_w + (hw_w * v);
2083 const GLfloat h = hw_h - (hw_h * v);
2084 const GLfloat x = (hw_w - w) * 0.5f;
2085 const GLfloat y = (hw_h - h) * 0.5f;
2086 vtx[0] = x; vtx[1] = y;
2087 vtx[2] = x; vtx[3] = y + h;
2088 vtx[4] = x + w; vtx[5] = y + h;
2089 vtx[6] = x + w; vtx[7] = y;
2090 }
2091 };
2092
2093 class h_stretch {
2094 const GLfloat hw_w, hw_h;
2095 public:
2096 h_stretch(uint32_t hw_w, uint32_t hw_h)
2097 : hw_w(hw_w), hw_h(hw_h) {
2098 }
2099 void operator()(GLfloat* vtx, float v) {
2100 const GLfloat w = hw_w - (hw_w * v);
2101 const GLfloat h = 1.0f;
2102 const GLfloat x = (hw_w - w) * 0.5f;
2103 const GLfloat y = (hw_h - h) * 0.5f;
2104 vtx[0] = x; vtx[1] = y;
2105 vtx[2] = x; vtx[3] = y + h;
2106 vtx[4] = x + w; vtx[5] = y + h;
2107 vtx[6] = x + w; vtx[7] = y;
2108 }
2109 };
2110
Mathias Agopiana6546e52010-10-14 12:33:07 -07002111 // the full animation is 12 frames
2112 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002113 s_curve_interpolator itr(nbFrames, 7.5f);
2114 s_curve_interpolator itg(nbFrames, 8.0f);
2115 s_curve_interpolator itb(nbFrames, 8.5f);
2116
2117 h_stretch hverts(hw_w, hw_h);
2118 glDisable(GL_BLEND);
2119 glDisable(GL_TEXTURE_2D);
2120 glColorMask(1,1,1,1);
2121 for (int i=nbFrames-1 ; i>=0 ; i--) {
2122 const float v = itg(i);
2123 hverts(vtx, v);
2124 glClear(GL_COLOR_BUFFER_BIT);
2125 glColor4f(1-v, 1-v, 1-v, 1);
2126 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2127 hw.flip(screenBounds);
2128 }
2129
Mathias Agopiana6546e52010-10-14 12:33:07 -07002130 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002131 v_stretch vverts(hw_w, hw_h);
2132 glEnable(GL_BLEND);
2133 glBlendFunc(GL_ONE, GL_ONE);
2134 for (int i=nbFrames-1 ; i>=0 ; i--) {
2135 float x, y, w, h;
2136 const float vr = itr(i);
2137 const float vg = itg(i);
2138 const float vb = itb(i);
2139
2140 // clear screen
2141 glColorMask(1,1,1,1);
2142 glClear(GL_COLOR_BUFFER_BIT);
2143 glEnable(GL_TEXTURE_2D);
2144
2145 // draw the red plane
2146 vverts(vtx, vr);
2147 glColorMask(1,0,0,1);
2148 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2149
2150 // draw the green plane
2151 vverts(vtx, vg);
2152 glColorMask(0,1,0,1);
2153 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2154
2155 // draw the blue plane
2156 vverts(vtx, vb);
2157 glColorMask(0,0,1,1);
2158 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2159
2160 hw.flip(screenBounds);
2161 }
2162
2163 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002164 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002165 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002166 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002167 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002168 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59119e62010-10-11 12:37:43 -07002169
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002170 return NO_ERROR;
2171}
2172
2173// ---------------------------------------------------------------------------
2174
Mathias Agopianabd671a2010-10-14 14:54:06 -07002175status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002177 ATRACE_CALL();
2178
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002179 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2180 if (!hw.canDraw()) {
2181 // we're already off
2182 return NO_ERROR;
2183 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002184
2185 // turn off hwc while we're doing the animation
2186 hw.getHwComposer().disable();
2187 // and make sure to turn it back on (if needed) next time we compose
2188 invalidateHwcGeometry();
2189
Mathias Agopianabd671a2010-10-14 14:54:06 -07002190 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2191 electronBeamOffAnimationImplLocked();
2192 }
2193
2194 // always clear the whole screen at the end of the animation
2195 glClearColor(0,0,0,1);
2196 glDisable(GL_SCISSOR_TEST);
2197 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002198 hw.flip( Region(hw.bounds()) );
2199
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002200 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002201}
2202
2203status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2204{
Mathias Agopian59119e62010-10-11 12:37:43 -07002205 class MessageTurnElectronBeamOff : public MessageBase {
2206 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002207 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002208 status_t result;
2209 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002210 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2211 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002212 }
2213 status_t getResult() const {
2214 return result;
2215 }
2216 virtual bool handler() {
2217 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002218 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002219 return true;
2220 }
2221 };
2222
Mathias Agopianabd671a2010-10-14 14:54:06 -07002223 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002224 status_t res = postMessageSync(msg);
2225 if (res == NO_ERROR) {
2226 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002227
2228 // work-around: when the power-manager calls us we activate the
2229 // animation. eventually, the "on" animation will be called
2230 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002231 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002232 }
2233 return res;
2234}
2235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002236// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002237
Mathias Agopianabd671a2010-10-14 14:54:06 -07002238status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002239{
2240 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2241 if (hw.canDraw()) {
2242 // we're already on
2243 return NO_ERROR;
2244 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002245 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2246 electronBeamOnAnimationImplLocked();
2247 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002248
2249 // make sure to redraw the whole screen when the animation is done
2250 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002251 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002252
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002253 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002254}
2255
2256status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2257{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002258 class MessageTurnElectronBeamOn : public MessageBase {
2259 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002260 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002261 status_t result;
2262 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002263 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2264 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002265 }
2266 status_t getResult() const {
2267 return result;
2268 }
2269 virtual bool handler() {
2270 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002271 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002272 return true;
2273 }
2274 };
2275
Mathias Agopianabd671a2010-10-14 14:54:06 -07002276 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002277 return NO_ERROR;
2278}
2279
2280// ---------------------------------------------------------------------------
2281
Mathias Agopian74c40c02010-09-29 13:02:36 -07002282status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2283 sp<IMemoryHeap>* heap,
2284 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002285 uint32_t sw, uint32_t sh,
2286 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002288 ATRACE_CALL();
2289
Mathias Agopian74c40c02010-09-29 13:02:36 -07002290 status_t result = PERMISSION_DENIED;
2291
2292 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002293 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002294 return BAD_VALUE;
2295
2296 if (!GLExtensions::getInstance().haveFramebufferObject())
2297 return INVALID_OPERATION;
2298
2299 // get screen geometry
2300 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2301 const uint32_t hw_w = hw.getWidth();
2302 const uint32_t hw_h = hw.getHeight();
2303
2304 if ((sw > hw_w) || (sh > hw_h))
2305 return BAD_VALUE;
2306
2307 sw = (!sw) ? hw_w : sw;
2308 sh = (!sh) ? hw_h : sh;
2309 const size_t size = sw * sh * 4;
2310
Steve Block9d453682011-12-20 16:23:08 +00002311 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002312 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002313
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314 // make sure to clear all GL error flags
2315 while ( glGetError() != GL_NO_ERROR ) ;
2316
2317 // create a FBO
2318 GLuint name, tname;
2319 glGenRenderbuffersOES(1, &tname);
2320 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2321 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002322
Mathias Agopian74c40c02010-09-29 13:02:36 -07002323 glGenFramebuffersOES(1, &name);
2324 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2325 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2326 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2327
2328 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002329
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2331
2332 // invert everything, b/c glReadPixel() below will invert the FB
2333 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002334 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002335 glMatrixMode(GL_PROJECTION);
2336 glPushMatrix();
2337 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002338 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002339 glMatrixMode(GL_MODELVIEW);
2340
2341 // redraw the screen entirely...
2342 glClearColor(0,0,0,1);
2343 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002344
Jamie Gennis9575f602011-10-07 14:51:16 -07002345 const LayerVector& layers(mDrawingState.layersSortedByZ);
2346 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347 for (size_t i=0 ; i<count ; ++i) {
2348 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002349 const uint32_t flags = layer->drawingState().flags;
2350 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2351 const uint32_t z = layer->drawingState().z;
2352 if (z >= minLayerZ && z <= maxLayerZ) {
2353 layer->drawForSreenShot();
2354 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002355 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 }
2357
2358 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002359 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002360 glScissor(0, 0, sw, sh);
2361
2362 // check for errors and return screen capture
2363 if (glGetError() != GL_NO_ERROR) {
2364 // error while rendering
2365 result = INVALID_OPERATION;
2366 } else {
2367 // allocate shared memory large enough to hold the
2368 // screen capture
2369 sp<MemoryHeapBase> base(
2370 new MemoryHeapBase(size, 0, "screen-capture") );
2371 void* const ptr = base->getBase();
2372 if (ptr) {
2373 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002374 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2376 if (glGetError() == GL_NO_ERROR) {
2377 *heap = base;
2378 *w = sw;
2379 *h = sh;
2380 *f = PIXEL_FORMAT_RGBA_8888;
2381 result = NO_ERROR;
2382 }
2383 } else {
2384 result = NO_MEMORY;
2385 }
2386 }
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002387 glDisable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 glViewport(0, 0, hw_w, hw_h);
2389 glMatrixMode(GL_PROJECTION);
2390 glPopMatrix();
2391 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 } else {
2393 result = BAD_VALUE;
2394 }
2395
2396 // release FBO resources
2397 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2398 glDeleteRenderbuffersOES(1, &tname);
2399 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002400
2401 hw.compositionComplete();
2402
Steve Block9d453682011-12-20 16:23:08 +00002403 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002404
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 return result;
2406}
2407
2408
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002409status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2410 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002411 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002412 uint32_t sw, uint32_t sh,
2413 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002414{
2415 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002416 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002417 return BAD_VALUE;
2418
2419 if (!GLExtensions::getInstance().haveFramebufferObject())
2420 return INVALID_OPERATION;
2421
2422 class MessageCaptureScreen : public MessageBase {
2423 SurfaceFlinger* flinger;
2424 DisplayID dpy;
2425 sp<IMemoryHeap>* heap;
2426 uint32_t* w;
2427 uint32_t* h;
2428 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002429 uint32_t sw;
2430 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002431 uint32_t minLayerZ;
2432 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002433 status_t result;
2434 public:
2435 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002436 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002437 uint32_t sw, uint32_t sh,
2438 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002439 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002440 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2441 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2442 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443 {
2444 }
2445 status_t getResult() const {
2446 return result;
2447 }
2448 virtual bool handler() {
2449 Mutex::Autolock _l(flinger->mStateLock);
2450
2451 // if we have secure windows, never allow the screen capture
2452 if (flinger->mSecureFrameBuffer)
2453 return true;
2454
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002456 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002457
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458 return true;
2459 }
2460 };
2461
2462 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002464 status_t res = postMessageSync(msg);
2465 if (res == NO_ERROR) {
2466 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2467 }
2468 return res;
2469}
2470
2471// ---------------------------------------------------------------------------
2472
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002473sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2474{
2475 sp<Layer> result;
2476 Mutex::Autolock _l(mStateLock);
2477 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2478 return result;
2479}
2480
2481// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482
Mathias Agopian96f08192010-06-02 23:28:45 -07002483Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002484 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486}
2487
Mathias Agopian96f08192010-06-02 23:28:45 -07002488Client::~Client()
2489{
Mathias Agopian96f08192010-06-02 23:28:45 -07002490 const size_t count = mLayers.size();
2491 for (size_t i=0 ; i<count ; i++) {
2492 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2493 if (layer != 0) {
2494 mFlinger->removeLayer(layer);
2495 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496 }
2497}
2498
Mathias Agopian96f08192010-06-02 23:28:45 -07002499status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002500 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002502
Mathias Agopian4f113742011-05-03 16:21:41 -07002503size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002504{
Mathias Agopian4f113742011-05-03 16:21:41 -07002505 Mutex::Autolock _l(mLock);
2506 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002507 mLayers.add(name, layer);
2508 return name;
2509}
2510
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002511void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002512{
Mathias Agopian4f113742011-05-03 16:21:41 -07002513 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002514 // we do a linear search here, because this doesn't happen often
2515 const size_t count = mLayers.size();
2516 for (size_t i=0 ; i<count ; i++) {
2517 if (mLayers.valueAt(i) == layer) {
2518 mLayers.removeItemsAt(i, 1);
2519 break;
2520 }
2521 }
2522}
Mathias Agopian4f113742011-05-03 16:21:41 -07002523sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2524{
2525 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002526 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002527 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002528 if (layer != 0) {
2529 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002530 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002531 }
2532 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533}
2534
Mathias Agopiana67932f2011-04-20 14:20:59 -07002535
2536status_t Client::onTransact(
2537 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2538{
2539 // these must be checked
2540 IPCThreadState* ipc = IPCThreadState::self();
2541 const int pid = ipc->getCallingPid();
2542 const int uid = ipc->getCallingUid();
2543 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002544 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002545 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002546 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002547 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002548 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002549 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2550 return PERMISSION_DENIED;
2551 }
2552 }
2553 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002554}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002555
2556
Mathias Agopian96f08192010-06-02 23:28:45 -07002557sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002558 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002559 const String8& name,
2560 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561 uint32_t flags)
2562{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002563 /*
2564 * createSurface must be called from the GL thread so that it can
2565 * have access to the GL context.
2566 */
2567
2568 class MessageCreateSurface : public MessageBase {
2569 sp<ISurface> result;
2570 SurfaceFlinger* flinger;
2571 ISurfaceComposerClient::surface_data_t* params;
2572 Client* client;
2573 const String8& name;
2574 DisplayID display;
2575 uint32_t w, h;
2576 PixelFormat format;
2577 uint32_t flags;
2578 public:
2579 MessageCreateSurface(SurfaceFlinger* flinger,
2580 ISurfaceComposerClient::surface_data_t* params,
2581 const String8& name, Client* client,
2582 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2583 uint32_t flags)
2584 : flinger(flinger), params(params), client(client), name(name),
2585 display(display), w(w), h(h), format(format), flags(flags)
2586 {
2587 }
2588 sp<ISurface> getResult() const { return result; }
2589 virtual bool handler() {
2590 result = flinger->createSurface(params, name, client,
2591 display, w, h, format, flags);
2592 return true;
2593 }
2594 };
2595
2596 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2597 params, name, this, display, w, h, format, flags);
2598 mFlinger->postMessageSync(msg);
2599 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600}
Mathias Agopian96f08192010-06-02 23:28:45 -07002601status_t Client::destroySurface(SurfaceID sid) {
2602 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002603}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002604
2605// ---------------------------------------------------------------------------
2606
Jamie Gennis9a78c902011-01-12 18:30:40 -08002607GraphicBufferAlloc::GraphicBufferAlloc() {}
2608
2609GraphicBufferAlloc::~GraphicBufferAlloc() {}
2610
2611sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002612 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002613 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2614 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002615 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002616 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002617 if (err == NO_MEMORY) {
2618 GraphicBuffer::dumpAllocationsToSystemLog();
2619 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002620 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002621 "failed (%s), handle=%p",
2622 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002623 return 0;
2624 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002625 return graphicBuffer;
2626}
2627
Jamie Gennis9a78c902011-01-12 18:30:40 -08002628// ---------------------------------------------------------------------------
2629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630GraphicPlane::GraphicPlane()
2631 : mHw(0)
2632{
2633}
2634
2635GraphicPlane::~GraphicPlane() {
2636 delete mHw;
2637}
2638
2639bool GraphicPlane::initialized() const {
2640 return mHw ? true : false;
2641}
2642
Mathias Agopian2b92d892010-02-08 15:49:35 -08002643int GraphicPlane::getWidth() const {
2644 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002645}
2646
Mathias Agopian2b92d892010-02-08 15:49:35 -08002647int GraphicPlane::getHeight() const {
2648 return mHeight;
2649}
2650
2651void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2652{
2653 mHw = hw;
2654
2655 // initialize the display orientation transform.
2656 // it's a constant that should come from the display driver.
2657 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2658 char property[PROPERTY_VALUE_MAX];
2659 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2660 //displayOrientation
2661 switch (atoi(property)) {
2662 case 90:
2663 displayOrientation = ISurfaceComposer::eOrientation90;
2664 break;
2665 case 270:
2666 displayOrientation = ISurfaceComposer::eOrientation270;
2667 break;
2668 }
2669 }
2670
2671 const float w = hw->getWidth();
2672 const float h = hw->getHeight();
2673 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2674 &mDisplayTransform);
2675 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2676 mDisplayWidth = h;
2677 mDisplayHeight = w;
2678 } else {
2679 mDisplayWidth = w;
2680 mDisplayHeight = h;
2681 }
2682
2683 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684}
2685
2686status_t GraphicPlane::orientationToTransfrom(
2687 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002688{
2689 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690 switch (orientation) {
2691 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002692 flags = Transform::ROT_0;
2693 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002695 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696 break;
2697 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002698 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002699 break;
2700 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002701 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002702 break;
2703 default:
2704 return BAD_VALUE;
2705 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002706 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002707 return NO_ERROR;
2708}
2709
2710status_t GraphicPlane::setOrientation(int orientation)
2711{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002712 // If the rotation can be handled in hardware, this is where
2713 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002714
2715 const DisplayHardware& hw(displayHardware());
2716 const float w = mDisplayWidth;
2717 const float h = mDisplayHeight;
2718 mWidth = int(w);
2719 mHeight = int(h);
2720
2721 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002722 GraphicPlane::orientationToTransfrom(orientation, w, h,
2723 &orientationTransform);
2724 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2725 mWidth = int(h);
2726 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002727 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002728
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002729 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002730 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002731 return NO_ERROR;
2732}
2733
2734const DisplayHardware& GraphicPlane::displayHardware() const {
2735 return *mHw;
2736}
2737
Mathias Agopian59119e62010-10-11 12:37:43 -07002738DisplayHardware& GraphicPlane::editDisplayHardware() {
2739 return *mHw;
2740}
2741
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002742const Transform& GraphicPlane::transform() const {
2743 return mGlobalTransform;
2744}
2745
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002746EGLDisplay GraphicPlane::getEGLDisplay() const {
2747 return mHw->getEGLDisplay();
2748}
2749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002750// ---------------------------------------------------------------------------
2751
2752}; // namespace android