blob: 0d4d2b97ae19ad929a44432f69c184fdfae24bc8 [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),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mSecureFrameBuffer(0)
99{
100 init();
101}
102
103void SurfaceFlinger::init()
104{
Steve Blocka19954a2012-01-04 20:05:49 +0000105 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 property_get("debug.sf.showupdates", value, "0");
111 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
Colin Cross3854ed52012-03-23 14:17:18 -0700113#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114 property_get("debug.sf.ddms", value, "0");
115 mDebugDDMS = atoi(value);
116 if (mDebugDDMS) {
117 DdmConnection::start(getServiceName());
118 }
Colin Cross3854ed52012-03-23 14:17:18 -0700119#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
Steve Blocka19954a2012-01-04 20:05:49 +0000121 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800125void SurfaceFlinger::onFirstRef()
126{
127 mEventQueue.init(this);
128
129 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
130
131 // Wait for the main thread to be done with its initialization
132 mReadyToRunBarrier.wait();
133}
134
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136SurfaceFlinger::~SurfaceFlinger()
137{
138 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141void SurfaceFlinger::binderDied(const wp<IBinder>& who)
142{
143 // the window manager died on us. prepare its eulogy.
144
145 // reset screen orientation
146 Vector<ComposerState> state;
147 setTransactionState(state, eOrientationDefault, 0);
148
149 // restart the boot-animation
150 property_set("ctl.start", "bootanim");
151}
152
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700153sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700155 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian7e27f052010-05-28 14:22:23 -0700158sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian96f08192010-06-02 23:28:45 -0700160 sp<ISurfaceComposerClient> bclient;
161 sp<Client> client(new Client(this));
162 status_t err = client->initCheck();
163 if (err == NO_ERROR) {
164 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 return bclient;
167}
168
Jamie Gennis9a78c902011-01-12 18:30:40 -0800169sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
170{
171 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
172 return gba;
173}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
176{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000177 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 const GraphicPlane& plane(mGraphicPlanes[dpy]);
179 return plane;
180}
181
182GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
183{
184 return const_cast<GraphicPlane&>(
185 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
186}
187
188void SurfaceFlinger::bootFinished()
189{
190 const nsecs_t now = systemTime();
191 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000192 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700193 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700194
195 // wait patiently for the window manager death
196 const String16 name("window");
197 sp<IBinder> window(defaultServiceManager()->getService(name));
198 if (window != 0) {
199 window->linkToDeath(this);
200 }
201
202 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700203 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206static inline uint16_t pack565(int r, int g, int b) {
207 return (r<<11)|(g<<5)|b;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210status_t SurfaceFlinger::readyToRun()
211{
Steve Blocka19954a2012-01-04 20:05:49 +0000212 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 "Initializing graphics H/W...");
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // we only support one display currently
216 int dpy = 0;
217
218 {
219 // initialize the main display
220 GraphicPlane& plane(graphicPlane(dpy));
221 DisplayHardware* const hw = new DisplayHardware(this, dpy);
222 plane.setDisplayHardware(hw);
223 }
224
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 // create the shared control-block
226 mServerHeap = new MemoryHeapBase(4096,
227 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000228 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 new(mServerCblk) surface_flinger_cblk_t;
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 // initialize primary screen
236 // (other display should be initialized in the same manner, but
237 // asynchronously, as they could come and go. None of this is supported
238 // yet).
239 const GraphicPlane& plane(graphicPlane(dpy));
240 const DisplayHardware& hw = plane.displayHardware();
241 const uint32_t w = hw.getWidth();
242 const uint32_t h = hw.getHeight();
243 const uint32_t f = hw.getFormat();
244 hw.makeCurrent();
245
246 // initialize the shared control block
247 mServerCblk->connected |= 1<<dpy;
248 display_cblk_t* dcblk = mServerCblk->displays + dpy;
249 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800250 dcblk->w = plane.getWidth();
251 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 dcblk->format = f;
253 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
254 dcblk->xdpi = hw.getDpiX();
255 dcblk->ydpi = hw.getDpiY();
256 dcblk->fps = hw.getRefreshRate();
257 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700261 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glShadeModel(GL_FLAT);
264 glDisable(GL_DITHER);
265 glDisable(GL_CULL_FACE);
266
267 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
268 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700269 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glGenTextures(1, &mWormholeTexName);
271 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
272 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
276 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
278
279 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
280 glGenTextures(1, &mProtectedTexName);
281 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
287 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
289 glViewport(0, 0, w, h);
290 glMatrixMode(GL_PROJECTION);
291 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700292 // put the origin in the left-bottom corner
293 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 -0800294
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800295
296 // start the EventThread
297 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800298 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800299 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
301 /*
302 * We're now ready to accept clients...
303 */
304
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800305 mReadyToRunBarrier.open();
306
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700307 // start boot animation
308 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return NO_ERROR;
311}
312
313// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800314
Jamie Gennis582270d2011-08-17 18:19:00 -0700315bool SurfaceFlinger::authenticateSurfaceTexture(
316 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800317 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700318 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800319
320 // Check the visible layer list for the ISurface
321 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
322 size_t count = currentLayers.size();
323 for (size_t i=0 ; i<count ; i++) {
324 const sp<LayerBase>& layer(currentLayers[i]);
325 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700326 if (lbc != NULL) {
327 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
328 if (lbcBinder == surfaceTextureBinder) {
329 return true;
330 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800331 }
332 }
333
334 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700335 // SurfaceTexture gets destroyed before all the clients are done using it,
336 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800337 // will instead fail later on when the client tries to use the surface,
338 // which should be reported as "surface XYZ returned an -ENODEV". The
339 // purgatorized layers are no less authentic than the visible ones, so this
340 // should not cause any harm.
341 size_t purgatorySize = mLayerPurgatory.size();
342 for (size_t i=0 ; i<purgatorySize ; i++) {
343 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
344 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700345 if (lbc != NULL) {
346 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
347 if (lbcBinder == surfaceTextureBinder) {
348 return true;
349 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800350 }
351 }
352
353 return false;
354}
355
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800356// ----------------------------------------------------------------------------
357
358sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800359 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700360}
361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800363
364void SurfaceFlinger::waitForEvent() {
365 mEventQueue.waitMessage();
366}
367
368void SurfaceFlinger::signalTransaction() {
369 mEventQueue.invalidate();
370}
371
372void SurfaceFlinger::signalLayerUpdate() {
373 mEventQueue.invalidate();
374}
375
376void SurfaceFlinger::signalRefresh() {
377 mEventQueue.refresh();
378}
379
380status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
381 nsecs_t reltime, uint32_t flags) {
382 return mEventQueue.postMessage(msg, reltime);
383}
384
385status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
386 nsecs_t reltime, uint32_t flags) {
387 status_t res = mEventQueue.postMessage(msg, reltime);
388 if (res == NO_ERROR) {
389 msg->wait();
390 }
391 return res;
392}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393
394bool SurfaceFlinger::threadLoop()
395{
396 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 return true;
398}
399
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800400void SurfaceFlinger::onMessageReceived(int32_t what)
401{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800402 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800403 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700404 case MessageQueue::REFRESH: {
405// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406 // if we're in a global transaction, don't do anything.
407 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
408 uint32_t transactionFlags = peekTransactionFlags(mask);
409 if (CC_UNLIKELY(transactionFlags)) {
410 handleTransaction(transactionFlags);
411 }
412
413 // post surfaces (if needed)
414 handlePageFlip();
415
Mathias Agopian69a655c2012-04-11 20:43:19 -0700416// signalRefresh();
417//
418// } break;
419//
420// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800421
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800422 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800423
Mathias Agopian303d5382012-02-05 01:49:16 -0800424 const DisplayHardware& hw(graphicPlane(0).displayHardware());
425
Mathias Agopian69a655c2012-04-11 20:43:19 -0700426// if (mDirtyRegion.isEmpty()) {
427// return;
428// }
429
Mathias Agopianb048cef2012-02-04 15:44:04 -0800430 if (CC_UNLIKELY(mHwWorkListDirty)) {
431 // build the h/w work list
432 handleWorkList();
433 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800434
Mathias Agopianb048cef2012-02-04 15:44:04 -0800435 if (CC_LIKELY(hw.canDraw())) {
436 // repaint the framebuffer (if needed)
437 handleRepaint();
438 // inform the h/w that we're done compositing
439 hw.compositionComplete();
440 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800441 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800442 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800443 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800444 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800445
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800446 } break;
447 }
448}
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450void SurfaceFlinger::postFramebuffer()
451{
Mathias Agopian841cde52012-03-01 15:44:37 -0800452 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800453 // mSwapRegion can be empty here is some cases, for instance if a hidden
454 // or fully transparent window is updating.
455 // in that case, we need to flip anyways to not risk a deadlock with
456 // h/w composer.
457
Mathias Agopiana44b0412011-10-16 18:46:35 -0700458 const DisplayHardware& hw(graphicPlane(0).displayHardware());
459 const nsecs_t now = systemTime();
460 mDebugInSwapBuffers = now;
461 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800462
463 size_t numLayers = mVisibleLayersSortedByZ.size();
464 for (size_t i = 0; i < numLayers; i++) {
465 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
466 }
467
Mathias Agopiana44b0412011-10-16 18:46:35 -0700468 mLastSwapBufferTime = systemTime() - now;
469 mDebugInSwapBuffers = 0;
470 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
474{
Mathias Agopian841cde52012-03-01 15:44:37 -0800475 ATRACE_CALL();
476
Mathias Agopianca4d3602011-05-19 15:38:14 -0700477 Mutex::Autolock _l(mStateLock);
478 const nsecs_t now = systemTime();
479 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
Mathias Agopianca4d3602011-05-19 15:38:14 -0700481 // Here we're guaranteed that some transaction flags are set
482 // so we can call handleTransactionLocked() unconditionally.
483 // We call getTransactionFlags(), which will also clear the flags,
484 // with mStateLock held to guarantee that mCurrentState won't change
485 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700486
Mathias Agopianca4d3602011-05-19 15:38:14 -0700487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 transactionFlags = getTransactionFlags(mask);
489 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700490
Mathias Agopianca4d3602011-05-19 15:38:14 -0700491 mLastTransactionTime = systemTime() - now;
492 mDebugInTransaction = 0;
493 invalidateHwcGeometry();
494 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700495}
496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700498{
499 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 const size_t count = currentLayers.size();
501
502 /*
503 * Traversal of the children
504 * (perform the transaction for each of them if needed)
505 */
506
507 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
508 if (layersNeedTransaction) {
509 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
512 if (!trFlags) continue;
513
514 const uint32_t flags = layer->doTransaction(0);
515 if (flags & Layer::eVisibleRegion)
516 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
518 }
519
520 /*
521 * Perform our own transaction if needed
522 */
523
524 if (transactionFlags & eTransactionNeeded) {
525 if (mCurrentState.orientation != mDrawingState.orientation) {
526 // the orientation has changed, recompute all visible regions
527 // and invalidate everything.
528
529 const int dpy = 0;
530 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700531 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 GraphicPlane& plane(graphicPlane(dpy));
533 plane.setOrientation(orientation);
534
535 // update the shared control block
536 const DisplayHardware& hw(plane.displayHardware());
537 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
538 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800539 dcblk->w = plane.getWidth();
540 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541
542 mVisibleRegionsDirty = true;
543 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 }
545
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700546 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
547 // layers have been added
548 mVisibleRegionsDirty = true;
549 }
550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 // some layers might have been removed, so
552 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700554 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700557 const size_t count = previousLayers.size();
558 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700559 const sp<LayerBase>& layer(previousLayers[i]);
560 if (currentLayers.indexOf( layer ) < 0) {
561 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700562 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700563 }
564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
567
568 commitTransaction();
569}
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800572 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573{
Mathias Agopian841cde52012-03-01 15:44:37 -0800574 ATRACE_CALL();
575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 const GraphicPlane& plane(graphicPlane(0));
577 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700578 const DisplayHardware& hw(plane.displayHardware());
579 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580
581 Region aboveOpaqueLayers;
582 Region aboveCoveredLayers;
583 Region dirty;
584
585 bool secureFrameBuffer = false;
586
587 size_t i = currentLayers.size();
588 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 layer->validateVisibility(planeTransform);
591
592 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700593 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594
Mathias Agopianab028732010-03-16 16:41:46 -0700595 /*
596 * opaqueRegion: area of a surface that is fully opaque.
597 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700599
600 /*
601 * visibleRegion: area of a surface that is visible on screen
602 * and not fully transparent. This is essentially the layer's
603 * footprint minus the opaque regions above it.
604 * Areas covered by a translucent surface are considered visible.
605 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700607
608 /*
609 * coveredRegion: area of a surface that is covered by all
610 * visible regions above it (which includes the translucent areas).
611 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700613
614
615 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800616 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700617 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700618 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700620 visibleRegion.andSelf(screenRegion);
621 if (!visibleRegion.isEmpty()) {
622 // Remove the transparent area from the visible region
623 if (translucent) {
624 visibleRegion.subtractSelf(layer->transparentRegionScreen);
625 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopianab028732010-03-16 16:41:46 -0700627 // compute the opaque region
628 const int32_t layerOrientation = layer->getOrientation();
629 if (s.alpha==255 && !translucent &&
630 ((layerOrientation & Transform::ROT_INVALID) == false)) {
631 // the opaque region is the layer's footprint
632 opaqueRegion = visibleRegion;
633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 }
635 }
636
Mathias Agopianab028732010-03-16 16:41:46 -0700637 // Clip the covered region to the visible region
638 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
639
640 // Update aboveCoveredLayers for next (lower) layer
641 aboveCoveredLayers.orSelf(visibleRegion);
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 // subtract the opaque region covered by the layers above us
644 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645
646 // compute this layer's dirty region
647 if (layer->contentDirty) {
648 // we need to invalidate the whole region
649 dirty = visibleRegion;
650 // as well, as the old visible region
651 dirty.orSelf(layer->visibleRegionScreen);
652 layer->contentDirty = false;
653 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700654 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700655 * the exposed region consists of two components:
656 * 1) what's VISIBLE now and was COVERED before
657 * 2) what's EXPOSED now less what was EXPOSED before
658 *
659 * note that (1) is conservative, we start with the whole
660 * visible region but only keep what used to be covered by
661 * something -- which mean it may have been exposed.
662 *
663 * (2) handles areas that were not covered by anything but got
664 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700665 */
Mathias Agopianab028732010-03-16 16:41:46 -0700666 const Region newExposed = visibleRegion - coveredRegion;
667 const Region oldVisibleRegion = layer->visibleRegionScreen;
668 const Region oldCoveredRegion = layer->coveredRegionScreen;
669 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
670 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 }
672 dirty.subtractSelf(aboveOpaqueLayers);
673
674 // accumulate to the screen dirty region
675 dirtyRegion.orSelf(dirty);
676
Mathias Agopianab028732010-03-16 16:41:46 -0700677 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 // Store the visible region is screen space
681 layer->setVisibleRegion(visibleRegion);
682 layer->setCoveredRegion(coveredRegion);
683
Mathias Agopian97011222009-07-28 10:57:27 -0700684 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 if (layer->isSecure() && !visibleRegion.isEmpty()) {
686 secureFrameBuffer = true;
687 }
688 }
689
Mathias Agopian97011222009-07-28 10:57:27 -0700690 // invalidate the areas where a layer was removed
691 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
692 mDirtyRegionRemovedLayer.clear();
693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 mSecureFrameBuffer = secureFrameBuffer;
695 opaqueRegion = aboveOpaqueLayers;
696}
697
698
699void SurfaceFlinger::commitTransaction()
700{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800701 if (!mLayersPendingRemoval.isEmpty()) {
702 // Notify removed layers now that they can't be drawn from
703 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
704 mLayersPendingRemoval[i]->onRemoved();
705 }
706 mLayersPendingRemoval.clear();
707 }
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700710 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700711 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712}
713
714void SurfaceFlinger::handlePageFlip()
715{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800716 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800717 const DisplayHardware& hw = graphicPlane(0).displayHardware();
718 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800720 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
721 const bool visibleRegions = lockPageFlip(currentLayers);
722
723 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 Region opaqueRegion;
725 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700726
727 /*
728 * rebuild the visible layer list
729 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800730 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700731 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.setCapacity(count);
733 for (size_t i=0 ; i<count ; i++) {
734 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
735 mVisibleLayersSortedByZ.add(currentLayers[i]);
736 }
737
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 mWormholeRegion = screenRegion.subtract(opaqueRegion);
739 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800740 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 }
742
743 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700744
745 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 mDirtyRegion.andSelf(screenRegion);
747}
748
Mathias Agopianad456f92011-01-13 17:53:01 -0800749void SurfaceFlinger::invalidateHwcGeometry()
750{
751 mHwWorkListDirty = true;
752}
753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
755{
756 bool recomputeVisibleRegions = false;
757 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700758 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700760 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 layer->lockPageFlip(recomputeVisibleRegions);
762 }
763 return recomputeVisibleRegions;
764}
765
766void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
767{
768 const GraphicPlane& plane(graphicPlane(0));
769 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800770 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700771 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700773 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 layer->unlockPageFlip(planeTransform, mDirtyRegion);
775 }
776}
777
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800778void SurfaceFlinger::handleRefresh()
779{
780 bool needInvalidate = false;
781 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
782 const size_t count = currentLayers.size();
783 for (size_t i=0 ; i<count ; i++) {
784 const sp<LayerBase>& layer(currentLayers[i]);
785 if (layer->onPreComposition()) {
786 needInvalidate = true;
787 }
788 }
789 if (needInvalidate) {
790 signalLayerUpdate();
791 }
792}
793
794
Mathias Agopiana350ff92010-08-10 17:14:02 -0700795void SurfaceFlinger::handleWorkList()
796{
797 mHwWorkListDirty = false;
798 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
799 if (hwc.initCheck() == NO_ERROR) {
800 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
801 const size_t count = currentLayers.size();
802 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700803 hwc_layer_t* const cur(hwc.getLayers());
804 for (size_t i=0 ; cur && i<count ; i++) {
805 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700806 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700807 cur[i].compositionType = HWC_FRAMEBUFFER;
808 cur[i].flags |= HWC_SKIP_LAYER;
809 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700810 }
811 }
812}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814void SurfaceFlinger::handleRepaint()
815{
Mathias Agopian841cde52012-03-01 15:44:37 -0800816 ATRACE_CALL();
817
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700819 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820
Glenn Kasten99ed2242011-12-15 09:51:17 -0800821 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 debugFlashRegions();
823 }
824
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // set the frame buffer
826 const DisplayHardware& hw(graphicPlane(0).displayHardware());
827 glMatrixMode(GL_MODELVIEW);
828 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
830 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700831 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700832 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
833 // takes a rectangle, we must make sure to update that whole
834 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700835 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700837 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // We need to redraw the rectangle that will be updated
839 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700840 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700842 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 } else {
844 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700846 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
848 }
849
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700850 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 composeSurfaces(mDirtyRegion);
852
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853 // update the swap region and clear the dirty region
854 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.clear();
856}
857
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700858void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700860 const DisplayHardware& hw(graphicPlane(0).displayHardware());
861 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700862 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700863 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700864 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700865 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700866
Mathias Agopianf384cc32011-09-08 18:31:55 -0700867 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
868 size_t count = layers.size();
869
Steve Blocke6f43dd2012-01-06 19:20:56 +0000870 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700871 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
872 hwc.getNumLayers(), count);
873
874 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700875 if (hwc.initCheck() == NO_ERROR) {
876 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
877 }
Mathias Agopian45721772010-08-12 15:03:26 -0700878
879 /*
880 * update the per-frame h/w composer data for each layer
881 * and build the transparent region of the FB
882 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700883 for (size_t i=0 ; i<count ; i++) {
884 const sp<LayerBase>& layer(layers[i]);
885 layer->setPerFrameData(&cur[i]);
886 }
887 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000888 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700889}
Mathias Agopian45721772010-08-12 15:03:26 -0700890
Mathias Agopianf384cc32011-09-08 18:31:55 -0700891void SurfaceFlinger::composeSurfaces(const Region& dirty)
892{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700893 const DisplayHardware& hw(graphicPlane(0).displayHardware());
894 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700895 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700896
897 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700898 if (!cur || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700899 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900
Mathias Agopianb9494d52012-04-18 02:28:45 -0700901 if (hwc.getLayerCount(HWC_OVERLAY)) {
902 // when using overlays, we assume a fully transparent framebuffer
903 // NOTE: we could reduce how much we need to clear, for instance
904 // remove where there are opaque FB layers. however, on some
905 // GPUs doing a "clean slate" glClear might be more efficient.
906 // We'll revisit later if needed.
907 glClearColor(0, 0, 0, 0);
908 glClear(GL_COLOR_BUFFER_BIT);
909 } else {
910 // screen is already cleared here
911 if (!mWormholeRegion.isEmpty()) {
912 // can happen with SurfaceView
913 drawWormhole();
914 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700915 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700916
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700917 /*
918 * and then, render the layers targeted at the framebuffer
919 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700920
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700921 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
922 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700923
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700925 const sp<LayerBase>& layer(layers[i]);
926 const Region clip(dirty.intersect(layer->visibleRegionScreen));
927 if (!clip.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700928 if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
929 if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700931 // never clear the very first layer since we're
932 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700933 layer->clearWithOpenGL(clip);
934 }
935 continue;
936 }
937 // render the layer
938 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700939 }
940 }
941 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942}
943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944void SurfaceFlinger::debugFlashRegions()
945{
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 const DisplayHardware& hw(graphicPlane(0).displayHardware());
947 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700948 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700949 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700950 return;
951 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700952
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700953 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700954 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
955 mDirtyRegion.bounds() : hw.bounds());
956 composeSurfaces(repaint);
957 }
958
Mathias Agopianc492e672011-10-18 14:49:27 -0700959 glDisable(GL_TEXTURE_EXTERNAL_OES);
960 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 glDisable(GL_SCISSOR_TEST);
963
Mathias Agopian0926f502009-05-04 14:17:04 -0700964 static int toggle = 0;
965 toggle = 1 - toggle;
966 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700967 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700968 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700969 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700970 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Mathias Agopian20f68782009-05-11 00:03:41 -0700972 Region::const_iterator it = mDirtyRegion.begin();
973 Region::const_iterator const end = mDirtyRegion.end();
974 while (it != end) {
975 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700977 { r.left, height - r.top },
978 { r.left, height - r.bottom },
979 { r.right, height - r.bottom },
980 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 };
982 glVertexPointer(2, GL_FLOAT, 0, vertices);
983 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
984 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700985
Mathias Agopian0656a682011-09-20 17:22:44 -0700986 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
988 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700989 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990}
991
992void SurfaceFlinger::drawWormhole() const
993{
994 const Region region(mWormholeRegion.intersect(mDirtyRegion));
995 if (region.isEmpty())
996 return;
997
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700998 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -0700999 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001000 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001001 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001002
1003 GLfloat vertices[4][2];
1004 glVertexPointer(2, GL_FLOAT, 0, vertices);
1005 Region::const_iterator it = region.begin();
1006 Region::const_iterator const end = region.end();
1007 while (it != end) {
1008 const Rect& r = *it++;
1009 vertices[0][0] = r.left;
1010 vertices[0][1] = r.top;
1011 vertices[1][0] = r.right;
1012 vertices[1][1] = r.top;
1013 vertices[2][0] = r.right;
1014 vertices[2][1] = r.bottom;
1015 vertices[3][0] = r.left;
1016 vertices[3][1] = r.bottom;
1017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019}
1020
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001021status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022{
1023 Mutex::Autolock _l(mStateLock);
1024 addLayer_l(layer);
1025 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1026 return NO_ERROR;
1027}
1028
Mathias Agopian96f08192010-06-02 23:28:45 -07001029status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1030{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001031 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001032 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1033}
1034
1035ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1036 const sp<LayerBaseClient>& lbc)
1037{
Mathias Agopian96f08192010-06-02 23:28:45 -07001038 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001039 size_t name = client->attachLayer(lbc);
1040
1041 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001042
1043 // add this layer to the current state list
1044 addLayer_l(lbc);
1045
Mathias Agopian4f113742011-05-03 16:21:41 -07001046 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001047}
1048
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001049status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050{
1051 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001052 status_t err = purgatorizeLayer_l(layer);
1053 if (err == NO_ERROR)
1054 setTransactionFlags(eTransactionNeeded);
1055 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001058status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001060 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1061 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001062 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1065 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 return NO_ERROR;
1068 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001069 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070}
1071
Mathias Agopian9a112062009-04-17 19:36:26 -07001072status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1073{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001074 // First add the layer to the purgatory list, which makes sure it won't
1075 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001076 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001077 if (err >= 0) {
1078 mLayerPurgatory.add(layerBase);
1079 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001080
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001081 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001082
Mathias Agopian3d579642009-06-04 18:46:21 -07001083 // it's possible that we don't find a layer, because it might
1084 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001085 // from the user because there is a race between Client::destroySurface(),
1086 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001087 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1088}
1089
Mathias Agopian96f08192010-06-02 23:28:45 -07001090status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
Mathias Agopian96f08192010-06-02 23:28:45 -07001092 layer->forceVisibilityTransaction();
1093 setTransactionFlags(eTraversalNeeded);
1094 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopiandea20b12011-05-03 17:04:02 -07001097uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1098{
1099 return android_atomic_release_load(&mTransactionFlags);
1100}
1101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1103{
1104 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1105}
1106
Mathias Agopianbb641242010-05-18 17:06:55 -07001107uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108{
1109 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1110 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001111 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 }
1113 return old;
1114}
1115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001117void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001118 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001119 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001120
Jamie Gennis28378392011-10-12 17:39:00 -07001121 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001122 if (mCurrentState.orientation != orientation) {
1123 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1124 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001125 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001126 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001127 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001128 orientation);
1129 }
1130 }
1131
Mathias Agopian698c0872011-06-28 19:09:31 -07001132 const size_t count = state.size();
1133 for (size_t i=0 ; i<count ; i++) {
1134 const ComposerState& s(state[i]);
1135 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001136 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001137 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001138
Mathias Agopian386aa982011-11-07 21:58:03 -08001139 if (transactionFlags) {
1140 // this triggers the transaction
1141 setTransactionFlags(transactionFlags);
1142
1143 // if this is a synchronous transaction, wait for it to take effect
1144 // before returning.
1145 if (flags & eSynchronous) {
1146 mTransationPending = true;
1147 }
1148 while (mTransationPending) {
1149 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1150 if (CC_UNLIKELY(err != NO_ERROR)) {
1151 // just in case something goes wrong in SF, return to the
1152 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001153 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001154 mTransationPending = false;
1155 break;
1156 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 }
1159}
1160
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001161sp<ISurface> SurfaceFlinger::createSurface(
1162 ISurfaceComposerClient::surface_data_t* params,
1163 const String8& name,
1164 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1166 uint32_t flags)
1167{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001168 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001169 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001170
1171 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001172 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001173 int(w), int(h));
1174 return surfaceHandle;
1175 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001176
Mathias Agopianff615cc2012-02-24 14:58:36 -08001177 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001178 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 switch (flags & eFXSurfaceMask) {
1180 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001181 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1182 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 break;
1184 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001185 // for now we treat Blur as Dim, until we can implement it
1186 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001188 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001190 case eFXSurfaceScreenshot:
1191 layer = createScreenshotSurface(client, d, w, h, flags);
1192 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 }
1194
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001195 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001197 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001201 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001203 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001204 if (normalLayer != 0) {
1205 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001206 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001207 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001208 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 }
1212
1213 return surfaceHandle;
1214}
1215
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001216sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001217 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001219 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220{
1221 // initialize the surfaces
1222 switch (format) { // TODO: take h/w into account
1223 case PIXEL_FORMAT_TRANSPARENT:
1224 case PIXEL_FORMAT_TRANSLUCENT:
1225 format = PIXEL_FORMAT_RGBA_8888;
1226 break;
1227 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001228#ifdef NO_RGBX_8888
1229 format = PIXEL_FORMAT_RGB_565;
1230#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001231 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001232#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001236#ifdef NO_RGBX_8888
1237 if (format == PIXEL_FORMAT_RGBX_8888)
1238 format = PIXEL_FORMAT_RGBA_8888;
1239#endif
1240
Mathias Agopian96f08192010-06-02 23:28:45 -07001241 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001242 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001243 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001244 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247 return layer;
1248}
1249
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001250sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001251 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001255 return layer;
1256}
1257
1258sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1259 const sp<Client>& client, DisplayID display,
1260 uint32_t w, uint32_t h, uint32_t flags)
1261{
1262 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 return layer;
1264}
1265
Mathias Agopian96f08192010-06-02 23:28:45 -07001266status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267{
Mathias Agopian9a112062009-04-17 19:36:26 -07001268 /*
1269 * called by the window manager, when a surface should be marked for
1270 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001271 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001272 * The surface is removed from the current and drawing lists, but placed
1273 * in the purgatory queue, so it's not destroyed right-away (we need
1274 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001275 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001276
Mathias Agopian48d819a2009-09-10 19:41:18 -07001277 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001278 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001279 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001280
Mathias Agopian48d819a2009-09-10 19:41:18 -07001281 if (layer != 0) {
1282 err = purgatorizeLayer_l(layer);
1283 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001284 setTransactionFlags(eTransactionNeeded);
1285 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001286 }
1287 return err;
1288}
1289
Mathias Agopianca4d3602011-05-19 15:38:14 -07001290status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001291{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001292 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001293 status_t err = NO_ERROR;
1294 sp<LayerBaseClient> l(layer.promote());
1295 if (l != NULL) {
1296 Mutex::Autolock _l(mStateLock);
1297 err = removeLayer_l(l);
1298 if (err == NAME_NOT_FOUND) {
1299 // The surface wasn't in the current list, which means it was
1300 // removed already, which means it is in the purgatory,
1301 // and need to be removed from there.
1302 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001303 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001304 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001305 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001306 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001307 "error removing layer=%p (%s)", l.get(), strerror(-err));
1308 }
1309 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310}
1311
Mathias Agopian698c0872011-06-28 19:09:31 -07001312uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001313 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001314 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001317 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1318 if (layer != 0) {
1319 const uint32_t what = s.what;
1320 if (what & ePositionChanged) {
1321 if (layer->setPosition(s.x, s.y))
1322 flags |= eTraversalNeeded;
1323 }
1324 if (what & eLayerChanged) {
1325 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1326 if (layer->setLayer(s.z)) {
1327 mCurrentState.layersSortedByZ.removeAt(idx);
1328 mCurrentState.layersSortedByZ.add(layer);
1329 // we need traversal (state changed)
1330 // AND transaction (list changed)
1331 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 }
1333 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001334 if (what & eSizeChanged) {
1335 if (layer->setSize(s.w, s.h)) {
1336 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 }
1338 }
1339 if (what & eAlphaChanged) {
1340 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1341 flags |= eTraversalNeeded;
1342 }
1343 if (what & eMatrixChanged) {
1344 if (layer->setMatrix(s.matrix))
1345 flags |= eTraversalNeeded;
1346 }
1347 if (what & eTransparentRegionChanged) {
1348 if (layer->setTransparentRegionHint(s.transparentRegion))
1349 flags |= eTraversalNeeded;
1350 }
1351 if (what & eVisibilityChanged) {
1352 if (layer->setFlags(s.flags, s.mask))
1353 flags |= eTraversalNeeded;
1354 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001356 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357}
1358
Mathias Agopianb60314a2012-04-10 22:09:54 -07001359// ---------------------------------------------------------------------------
1360
1361void SurfaceFlinger::onScreenAcquired() {
1362 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1363 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001364 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001365 // this is a temporary work-around, eventually this should be called
1366 // by the power-manager
1367 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001368 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001369 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
Mathias Agopianb60314a2012-04-10 22:09:54 -07001372void SurfaceFlinger::onScreenReleased() {
1373 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1374 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001375 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001376 hw.releaseScreen();
1377 // from this point on, SF will stop drawing
1378 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379}
1380
Mathias Agopianb60314a2012-04-10 22:09:54 -07001381void SurfaceFlinger::screenAcquired() {
1382 class MessageScreenAcquired : public MessageBase {
1383 SurfaceFlinger* flinger;
1384 public:
1385 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1386 virtual bool handler() {
1387 flinger->onScreenAcquired();
1388 return true;
1389 }
1390 };
1391 sp<MessageBase> msg = new MessageScreenAcquired(this);
1392 postMessageSync(msg);
1393}
1394
1395void SurfaceFlinger::screenReleased() {
1396 class MessageScreenReleased : public MessageBase {
1397 SurfaceFlinger* flinger;
1398 public:
1399 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1400 virtual bool handler() {
1401 flinger->onScreenReleased();
1402 return true;
1403 }
1404 };
1405 sp<MessageBase> msg = new MessageScreenReleased(this);
1406 postMessageSync(msg);
1407}
1408
1409// ---------------------------------------------------------------------------
1410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1412{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001413 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 char buffer[SIZE];
1415 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001416
1417 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 snprintf(buffer, SIZE, "Permission Denial: "
1419 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1420 IPCThreadState::self()->getCallingPid(),
1421 IPCThreadState::self()->getCallingUid());
1422 result.append(buffer);
1423 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001424 // Try to get the main lock, but don't insist if we can't
1425 // (this would indicate SF is stuck, but we want to be able to
1426 // print something in dumpsys).
1427 int retry = 3;
1428 while (mStateLock.tryLock()<0 && --retry>=0) {
1429 usleep(1000000);
1430 }
1431 const bool locked(retry >= 0);
1432 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001433 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001434 "SurfaceFlinger appears to be unresponsive, "
1435 "dumping anyways (no locks held)\n");
1436 result.append(buffer);
1437 }
1438
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001439 bool dumpAll = true;
1440 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001441 size_t numArgs = args.size();
1442 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001443 if ((index < numArgs) &&
1444 (args[index] == String16("--list"))) {
1445 index++;
1446 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001447 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001448 }
1449
1450 if ((index < numArgs) &&
1451 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001452 index++;
1453 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001454 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001455 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001456
1457 if ((index < numArgs) &&
1458 (args[index] == String16("--latency-clear"))) {
1459 index++;
1460 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001461 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001462 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001464
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001465 if (dumpAll) {
1466 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001467 }
1468
Mathias Agopian9795c422009-08-26 16:36:26 -07001469 if (locked) {
1470 mStateLock.unlock();
1471 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 }
1473 write(fd, result.string(), result.size());
1474 return NO_ERROR;
1475}
1476
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001477void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1478 String8& result, char* buffer, size_t SIZE) const
1479{
1480 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1481 const size_t count = currentLayers.size();
1482 for (size_t i=0 ; i<count ; i++) {
1483 const sp<LayerBase>& layer(currentLayers[i]);
1484 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1485 result.append(buffer);
1486 }
1487}
1488
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001489void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1490 String8& result, char* buffer, size_t SIZE) const
1491{
1492 String8 name;
1493 if (index < args.size()) {
1494 name = String8(args[index]);
1495 index++;
1496 }
1497
1498 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1499 const size_t count = currentLayers.size();
1500 for (size_t i=0 ; i<count ; i++) {
1501 const sp<LayerBase>& layer(currentLayers[i]);
1502 if (name.isEmpty()) {
1503 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1504 result.append(buffer);
1505 }
1506 if (name.isEmpty() || (name == layer->getName())) {
1507 layer->dumpStats(result, buffer, SIZE);
1508 }
1509 }
1510}
1511
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001512void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1513 String8& result, char* buffer, size_t SIZE) const
1514{
1515 String8 name;
1516 if (index < args.size()) {
1517 name = String8(args[index]);
1518 index++;
1519 }
1520
1521 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1522 const size_t count = currentLayers.size();
1523 for (size_t i=0 ; i<count ; i++) {
1524 const sp<LayerBase>& layer(currentLayers[i]);
1525 if (name.isEmpty() || (name == layer->getName())) {
1526 layer->clearStats();
1527 }
1528 }
1529}
1530
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001531void SurfaceFlinger::dumpAllLocked(
1532 String8& result, char* buffer, size_t SIZE) const
1533{
1534 // figure out if we're stuck somewhere
1535 const nsecs_t now = systemTime();
1536 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1537 const nsecs_t inTransaction(mDebugInTransaction);
1538 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1539 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1540
1541 /*
1542 * Dump the visible layer list
1543 */
1544 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1545 const size_t count = currentLayers.size();
1546 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1547 result.append(buffer);
1548 for (size_t i=0 ; i<count ; i++) {
1549 const sp<LayerBase>& layer(currentLayers[i]);
1550 layer->dump(result, buffer, SIZE);
1551 }
1552
1553 /*
1554 * Dump the layers in the purgatory
1555 */
1556
1557 const size_t purgatorySize = mLayerPurgatory.size();
1558 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1559 result.append(buffer);
1560 for (size_t i=0 ; i<purgatorySize ; i++) {
1561 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1562 layer->shortDump(result, buffer, SIZE);
1563 }
1564
1565 /*
1566 * Dump SurfaceFlinger global state
1567 */
1568
1569 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1570 result.append(buffer);
1571
1572 const GLExtensions& extensions(GLExtensions::getInstance());
1573 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1574 extensions.getVendor(),
1575 extensions.getRenderer(),
1576 extensions.getVersion());
1577 result.append(buffer);
1578
1579 snprintf(buffer, SIZE, "EGL : %s\n",
1580 eglQueryString(graphicPlane(0).getEGLDisplay(),
1581 EGL_VERSION_HW_ANDROID));
1582 result.append(buffer);
1583
1584 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1585 result.append(buffer);
1586
1587 mWormholeRegion.dump(result, "WormholeRegion");
1588 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1589 snprintf(buffer, SIZE,
1590 " orientation=%d, canDraw=%d\n",
1591 mCurrentState.orientation, hw.canDraw());
1592 result.append(buffer);
1593 snprintf(buffer, SIZE,
1594 " last eglSwapBuffers() time: %f us\n"
1595 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001596 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001597 " refresh-rate : %f fps\n"
1598 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001599 " y-dpi : %f\n"
1600 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001601 mLastSwapBufferTime/1000.0,
1602 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001603 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001604 hw.getRefreshRate(),
1605 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001606 hw.getDpiY(),
1607 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001608 result.append(buffer);
1609
1610 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1611 inSwapBuffersDuration/1000.0);
1612 result.append(buffer);
1613
1614 snprintf(buffer, SIZE, " transaction time: %f us\n",
1615 inTransactionDuration/1000.0);
1616 result.append(buffer);
1617
1618 /*
1619 * VSYNC state
1620 */
1621 mEventThread->dump(result, buffer, SIZE);
1622
1623 /*
1624 * Dump HWComposer state
1625 */
1626 HWComposer& hwc(hw.getHwComposer());
1627 snprintf(buffer, SIZE, "h/w composer state:\n");
1628 result.append(buffer);
1629 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1630 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1631 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1632 result.append(buffer);
1633 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1634
1635 /*
1636 * Dump gralloc state
1637 */
1638 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1639 alloc.dump(result);
1640 hw.dump(result);
1641}
1642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643status_t SurfaceFlinger::onTransact(
1644 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1645{
1646 switch (code) {
1647 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001648 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001651 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001652 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 {
1654 // codes that require permission check
1655 IPCThreadState* ipc = IPCThreadState::self();
1656 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001657 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001658 if ((uid != AID_GRAPHICS) &&
1659 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001660 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001661 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1662 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001664 break;
1665 }
1666 case CAPTURE_SCREEN:
1667 {
1668 // codes that require permission check
1669 IPCThreadState* ipc = IPCThreadState::self();
1670 const int pid = ipc->getCallingPid();
1671 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001672 if ((uid != AID_GRAPHICS) &&
1673 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001674 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001675 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1676 return PERMISSION_DENIED;
1677 }
1678 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 }
1680 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1683 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001684 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001685 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001686 IPCThreadState* ipc = IPCThreadState::self();
1687 const int pid = ipc->getCallingPid();
1688 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001689 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001690 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 return PERMISSION_DENIED;
1692 }
1693 int n;
1694 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001695 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001696 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 return NO_ERROR;
1698 case 1002: // SHOW_UPDATES
1699 n = data.readInt32();
1700 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001701 invalidateHwcGeometry();
1702 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001705 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001706 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001708 case 1005:{ // force transaction
1709 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1710 return NO_ERROR;
1711 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001712 case 1006:{ // send empty update
1713 signalRefresh();
1714 return NO_ERROR;
1715 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001716 case 1008: // toggle use of hw composer
1717 n = data.readInt32();
1718 mDebugDisableHWC = n ? 1 : 0;
1719 invalidateHwcGeometry();
1720 repaintEverything();
1721 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001722 case 1009: // toggle use of transform hint
1723 n = data.readInt32();
1724 mDebugDisableTransformHint = n ? 1 : 0;
1725 invalidateHwcGeometry();
1726 repaintEverything();
1727 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001729 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730 reply->writeInt32(0);
1731 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001732 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001733 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 return NO_ERROR;
1735 case 1013: {
1736 Mutex::Autolock _l(mStateLock);
1737 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1738 reply->writeInt32(hw.getPageFlipCount());
1739 }
1740 return NO_ERROR;
1741 }
1742 }
1743 return err;
1744}
1745
Mathias Agopian53331da2011-08-22 21:44:41 -07001746void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001747 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001748 const Rect bounds(hw.getBounds());
1749 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001750 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001751}
1752
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001753void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1754 Mutex::Autolock _l(mInvalidateLock);
1755 mInvalidateRegion = reg;
1756}
1757
1758Region SurfaceFlinger::getAndClearInvalidateRegion() {
1759 Mutex::Autolock _l(mInvalidateLock);
1760 Region reg(mInvalidateRegion);
1761 mInvalidateRegion.clear();
1762 return reg;
1763}
1764
Mathias Agopian59119e62010-10-11 12:37:43 -07001765// ---------------------------------------------------------------------------
1766
Mathias Agopian118d0242011-10-13 16:02:48 -07001767status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1768 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1769{
1770 Mutex::Autolock _l(mStateLock);
1771 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1772}
1773
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001774status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1775 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001776{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001777 ATRACE_CALL();
1778
Mathias Agopian59119e62010-10-11 12:37:43 -07001779 if (!GLExtensions::getInstance().haveFramebufferObject())
1780 return INVALID_OPERATION;
1781
1782 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001783 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001784 const uint32_t hw_w = hw.getWidth();
1785 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001786 GLfloat u = 1;
1787 GLfloat v = 1;
1788
1789 // make sure to clear all GL error flags
1790 while ( glGetError() != GL_NO_ERROR ) ;
1791
1792 // create a FBO
1793 GLuint name, tname;
1794 glGenTextures(1, &tname);
1795 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001796 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1797 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001798 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1799 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001800 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001801 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001802 GLint tw = (2 << (31 - clz(hw_w)));
1803 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001804 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1805 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001806 u = GLfloat(hw_w) / tw;
1807 v = GLfloat(hw_h) / th;
1808 }
1809 glGenFramebuffersOES(1, &name);
1810 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001811 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1812 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001813
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001814 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001815 glDisable(GL_TEXTURE_EXTERNAL_OES);
1816 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001817 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 glClearColor(0,0,0,1);
1819 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001820 glMatrixMode(GL_MODELVIEW);
1821 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1823 const size_t count = layers.size();
1824 for (size_t i=0 ; i<count ; ++i) {
1825 const sp<LayerBase>& layer(layers[i]);
1826 layer->drawForSreenShot();
1827 }
1828
Mathias Agopian118d0242011-10-13 16:02:48 -07001829 hw.compositionComplete();
1830
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001831 // back to main framebuffer
1832 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1833 glDisable(GL_SCISSOR_TEST);
1834 glDeleteFramebuffersOES(1, &name);
1835
1836 *textureName = tname;
1837 *uOut = u;
1838 *vOut = v;
1839 return NO_ERROR;
1840}
1841
1842// ---------------------------------------------------------------------------
1843
1844status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1845{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001846 // get screen geometry
1847 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1848 const uint32_t hw_w = hw.getWidth();
1849 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001850 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001851
1852 GLfloat u, v;
1853 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001854 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001855 if (result != NO_ERROR) {
1856 return result;
1857 }
1858
1859 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001860 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 glBindTexture(GL_TEXTURE_2D, tname);
1862 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1863 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1864 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001865 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1866 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001867 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1868 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1869 glVertexPointer(2, GL_FLOAT, 0, vtx);
1870
Mathias Agopianffcf4652011-07-07 17:30:31 -07001871 /*
1872 * Texture coordinate mapping
1873 *
1874 * u
1875 * 1 +----------+---+
1876 * | | | | image is inverted
1877 * | V | | w.r.t. the texture
1878 * 1-v +----------+ | coordinates
1879 * | |
1880 * | |
1881 * | |
1882 * 0 +--------------+
1883 * 0 1
1884 *
1885 */
1886
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001887 class s_curve_interpolator {
1888 const float nbFrames, s, v;
1889 public:
1890 s_curve_interpolator(int nbFrames, float s)
1891 : nbFrames(1.0f / (nbFrames-1)), s(s),
1892 v(1.0f + expf(-s + 0.5f*s)) {
1893 }
1894 float operator()(int f) {
1895 const float x = f * nbFrames;
1896 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1897 }
1898 };
1899
1900 class v_stretch {
1901 const GLfloat hw_w, hw_h;
1902 public:
1903 v_stretch(uint32_t hw_w, uint32_t hw_h)
1904 : hw_w(hw_w), hw_h(hw_h) {
1905 }
1906 void operator()(GLfloat* vtx, float v) {
1907 const GLfloat w = hw_w + (hw_w * v);
1908 const GLfloat h = hw_h - (hw_h * v);
1909 const GLfloat x = (hw_w - w) * 0.5f;
1910 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001911 vtx[0] = x; vtx[1] = y;
1912 vtx[2] = x; vtx[3] = y + h;
1913 vtx[4] = x + w; vtx[5] = y + h;
1914 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001915 }
1916 };
1917
1918 class h_stretch {
1919 const GLfloat hw_w, hw_h;
1920 public:
1921 h_stretch(uint32_t hw_w, uint32_t hw_h)
1922 : hw_w(hw_w), hw_h(hw_h) {
1923 }
1924 void operator()(GLfloat* vtx, float v) {
1925 const GLfloat w = hw_w - (hw_w * v);
1926 const GLfloat h = 1.0f;
1927 const GLfloat x = (hw_w - w) * 0.5f;
1928 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001929 vtx[0] = x; vtx[1] = y;
1930 vtx[2] = x; vtx[3] = y + h;
1931 vtx[4] = x + w; vtx[5] = y + h;
1932 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001933 }
1934 };
1935
1936 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001937 char value[PROPERTY_VALUE_MAX];
1938 property_get("debug.sf.electron_frames", value, "24");
1939 int nbFrames = (atoi(value) + 1) >> 1;
1940 if (nbFrames <= 0) // just in case
1941 nbFrames = 24;
1942
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001943 s_curve_interpolator itr(nbFrames, 7.5f);
1944 s_curve_interpolator itg(nbFrames, 8.0f);
1945 s_curve_interpolator itb(nbFrames, 8.5f);
1946
1947 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001948
1949 glMatrixMode(GL_TEXTURE);
1950 glLoadIdentity();
1951 glMatrixMode(GL_MODELVIEW);
1952 glLoadIdentity();
1953
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001954 glEnable(GL_BLEND);
1955 glBlendFunc(GL_ONE, GL_ONE);
1956 for (int i=0 ; i<nbFrames ; i++) {
1957 float x, y, w, h;
1958 const float vr = itr(i);
1959 const float vg = itg(i);
1960 const float vb = itb(i);
1961
1962 // clear screen
1963 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001964 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001965 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001966
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001967 // draw the red plane
1968 vverts(vtx, vr);
1969 glColorMask(1,0,0,1);
1970 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001971
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 // draw the green plane
1973 vverts(vtx, vg);
1974 glColorMask(0,1,0,1);
1975 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001976
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001977 // draw the blue plane
1978 vverts(vtx, vb);
1979 glColorMask(0,0,1,1);
1980 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001981
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001983 glDisable(GL_TEXTURE_2D);
1984 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001985 glColor4f(vg, vg, vg, 1);
1986 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1987 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001988 }
1989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 h_stretch hverts(hw_w, hw_h);
1991 glDisable(GL_BLEND);
1992 glDisable(GL_TEXTURE_2D);
1993 glColorMask(1,1,1,1);
1994 for (int i=0 ; i<nbFrames ; i++) {
1995 const float v = itg(i);
1996 hverts(vtx, v);
1997 glClear(GL_COLOR_BUFFER_BIT);
1998 glColor4f(1-v, 1-v, 1-v, 1);
1999 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2000 hw.flip(screenBounds);
2001 }
2002
2003 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2005 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002006 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002007 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002008 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 return NO_ERROR;
2010}
2011
2012status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2013{
2014 status_t result = PERMISSION_DENIED;
2015
2016 if (!GLExtensions::getInstance().haveFramebufferObject())
2017 return INVALID_OPERATION;
2018
2019
2020 // get screen geometry
2021 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2022 const uint32_t hw_w = hw.getWidth();
2023 const uint32_t hw_h = hw.getHeight();
2024 const Region screenBounds(hw.bounds());
2025
2026 GLfloat u, v;
2027 GLuint tname;
2028 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2029 if (result != NO_ERROR) {
2030 return result;
2031 }
2032
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002033 GLfloat vtx[8];
2034 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002035 glBindTexture(GL_TEXTURE_2D, tname);
2036 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2037 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2038 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002039 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2040 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002041 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2042 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2043 glVertexPointer(2, GL_FLOAT, 0, vtx);
2044
2045 class s_curve_interpolator {
2046 const float nbFrames, s, v;
2047 public:
2048 s_curve_interpolator(int nbFrames, float s)
2049 : nbFrames(1.0f / (nbFrames-1)), s(s),
2050 v(1.0f + expf(-s + 0.5f*s)) {
2051 }
2052 float operator()(int f) {
2053 const float x = f * nbFrames;
2054 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2055 }
2056 };
2057
2058 class v_stretch {
2059 const GLfloat hw_w, hw_h;
2060 public:
2061 v_stretch(uint32_t hw_w, uint32_t hw_h)
2062 : hw_w(hw_w), hw_h(hw_h) {
2063 }
2064 void operator()(GLfloat* vtx, float v) {
2065 const GLfloat w = hw_w + (hw_w * v);
2066 const GLfloat h = hw_h - (hw_h * v);
2067 const GLfloat x = (hw_w - w) * 0.5f;
2068 const GLfloat y = (hw_h - h) * 0.5f;
2069 vtx[0] = x; vtx[1] = y;
2070 vtx[2] = x; vtx[3] = y + h;
2071 vtx[4] = x + w; vtx[5] = y + h;
2072 vtx[6] = x + w; vtx[7] = y;
2073 }
2074 };
2075
2076 class h_stretch {
2077 const GLfloat hw_w, hw_h;
2078 public:
2079 h_stretch(uint32_t hw_w, uint32_t hw_h)
2080 : hw_w(hw_w), hw_h(hw_h) {
2081 }
2082 void operator()(GLfloat* vtx, float v) {
2083 const GLfloat w = hw_w - (hw_w * v);
2084 const GLfloat h = 1.0f;
2085 const GLfloat x = (hw_w - w) * 0.5f;
2086 const GLfloat y = (hw_h - h) * 0.5f;
2087 vtx[0] = x; vtx[1] = y;
2088 vtx[2] = x; vtx[3] = y + h;
2089 vtx[4] = x + w; vtx[5] = y + h;
2090 vtx[6] = x + w; vtx[7] = y;
2091 }
2092 };
2093
Mathias Agopiana6546e52010-10-14 12:33:07 -07002094 // the full animation is 12 frames
2095 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002096 s_curve_interpolator itr(nbFrames, 7.5f);
2097 s_curve_interpolator itg(nbFrames, 8.0f);
2098 s_curve_interpolator itb(nbFrames, 8.5f);
2099
2100 h_stretch hverts(hw_w, hw_h);
2101 glDisable(GL_BLEND);
2102 glDisable(GL_TEXTURE_2D);
2103 glColorMask(1,1,1,1);
2104 for (int i=nbFrames-1 ; i>=0 ; i--) {
2105 const float v = itg(i);
2106 hverts(vtx, v);
2107 glClear(GL_COLOR_BUFFER_BIT);
2108 glColor4f(1-v, 1-v, 1-v, 1);
2109 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2110 hw.flip(screenBounds);
2111 }
2112
Mathias Agopiana6546e52010-10-14 12:33:07 -07002113 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002114 v_stretch vverts(hw_w, hw_h);
2115 glEnable(GL_BLEND);
2116 glBlendFunc(GL_ONE, GL_ONE);
2117 for (int i=nbFrames-1 ; i>=0 ; i--) {
2118 float x, y, w, h;
2119 const float vr = itr(i);
2120 const float vg = itg(i);
2121 const float vb = itb(i);
2122
2123 // clear screen
2124 glColorMask(1,1,1,1);
2125 glClear(GL_COLOR_BUFFER_BIT);
2126 glEnable(GL_TEXTURE_2D);
2127
2128 // draw the red plane
2129 vverts(vtx, vr);
2130 glColorMask(1,0,0,1);
2131 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2132
2133 // draw the green plane
2134 vverts(vtx, vg);
2135 glColorMask(0,1,0,1);
2136 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2137
2138 // draw the blue plane
2139 vverts(vtx, vb);
2140 glColorMask(0,0,1,1);
2141 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2142
2143 hw.flip(screenBounds);
2144 }
2145
2146 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002147 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002148 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002149 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002150 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002151 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59119e62010-10-11 12:37:43 -07002152
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002153 return NO_ERROR;
2154}
2155
2156// ---------------------------------------------------------------------------
2157
Mathias Agopianabd671a2010-10-14 14:54:06 -07002158status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002159{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002160 ATRACE_CALL();
2161
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002162 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2163 if (!hw.canDraw()) {
2164 // we're already off
2165 return NO_ERROR;
2166 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002167
2168 // turn off hwc while we're doing the animation
2169 hw.getHwComposer().disable();
2170 // and make sure to turn it back on (if needed) next time we compose
2171 invalidateHwcGeometry();
2172
Mathias Agopianabd671a2010-10-14 14:54:06 -07002173 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2174 electronBeamOffAnimationImplLocked();
2175 }
2176
2177 // always clear the whole screen at the end of the animation
2178 glClearColor(0,0,0,1);
2179 glDisable(GL_SCISSOR_TEST);
2180 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002181 hw.flip( Region(hw.bounds()) );
2182
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002183 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002184}
2185
2186status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2187{
Mathias Agopian59119e62010-10-11 12:37:43 -07002188 class MessageTurnElectronBeamOff : public MessageBase {
2189 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002190 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002191 status_t result;
2192 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002193 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2194 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002195 }
2196 status_t getResult() const {
2197 return result;
2198 }
2199 virtual bool handler() {
2200 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002201 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002202 return true;
2203 }
2204 };
2205
Mathias Agopianabd671a2010-10-14 14:54:06 -07002206 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002207 status_t res = postMessageSync(msg);
2208 if (res == NO_ERROR) {
2209 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002210
2211 // work-around: when the power-manager calls us we activate the
2212 // animation. eventually, the "on" animation will be called
2213 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002214 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002215 }
2216 return res;
2217}
2218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002220
Mathias Agopianabd671a2010-10-14 14:54:06 -07002221status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002222{
2223 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2224 if (hw.canDraw()) {
2225 // we're already on
2226 return NO_ERROR;
2227 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002228 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2229 electronBeamOnAnimationImplLocked();
2230 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002231
2232 // make sure to redraw the whole screen when the animation is done
2233 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002234 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002235
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002236 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002237}
2238
2239status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2240{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002241 class MessageTurnElectronBeamOn : public MessageBase {
2242 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002243 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002244 status_t result;
2245 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002246 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2247 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002248 }
2249 status_t getResult() const {
2250 return result;
2251 }
2252 virtual bool handler() {
2253 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002254 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002255 return true;
2256 }
2257 };
2258
Mathias Agopianabd671a2010-10-14 14:54:06 -07002259 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002260 return NO_ERROR;
2261}
2262
2263// ---------------------------------------------------------------------------
2264
Mathias Agopian74c40c02010-09-29 13:02:36 -07002265status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2266 sp<IMemoryHeap>* heap,
2267 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002268 uint32_t sw, uint32_t sh,
2269 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002270{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002271 ATRACE_CALL();
2272
Mathias Agopian74c40c02010-09-29 13:02:36 -07002273 status_t result = PERMISSION_DENIED;
2274
2275 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002276 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002277 return BAD_VALUE;
2278
2279 if (!GLExtensions::getInstance().haveFramebufferObject())
2280 return INVALID_OPERATION;
2281
2282 // get screen geometry
2283 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2284 const uint32_t hw_w = hw.getWidth();
2285 const uint32_t hw_h = hw.getHeight();
2286
2287 if ((sw > hw_w) || (sh > hw_h))
2288 return BAD_VALUE;
2289
2290 sw = (!sw) ? hw_w : sw;
2291 sh = (!sh) ? hw_h : sh;
2292 const size_t size = sw * sh * 4;
2293
Steve Block9d453682011-12-20 16:23:08 +00002294 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002295 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002296
Mathias Agopian74c40c02010-09-29 13:02:36 -07002297 // make sure to clear all GL error flags
2298 while ( glGetError() != GL_NO_ERROR ) ;
2299
2300 // create a FBO
2301 GLuint name, tname;
2302 glGenRenderbuffersOES(1, &tname);
2303 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2304 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002305
Mathias Agopian74c40c02010-09-29 13:02:36 -07002306 glGenFramebuffersOES(1, &name);
2307 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2308 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2309 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2310
2311 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002312
Mathias Agopian74c40c02010-09-29 13:02:36 -07002313 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2314
2315 // invert everything, b/c glReadPixel() below will invert the FB
2316 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002317 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002318 glMatrixMode(GL_PROJECTION);
2319 glPushMatrix();
2320 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002321 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 glMatrixMode(GL_MODELVIEW);
2323
2324 // redraw the screen entirely...
2325 glClearColor(0,0,0,1);
2326 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002327
Jamie Gennis9575f602011-10-07 14:51:16 -07002328 const LayerVector& layers(mDrawingState.layersSortedByZ);
2329 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002330 for (size_t i=0 ; i<count ; ++i) {
2331 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002332 const uint32_t flags = layer->drawingState().flags;
2333 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2334 const uint32_t z = layer->drawingState().z;
2335 if (z >= minLayerZ && z <= maxLayerZ) {
2336 layer->drawForSreenShot();
2337 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002338 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002339 }
2340
2341 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002342 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 glScissor(0, 0, sw, sh);
2344
2345 // check for errors and return screen capture
2346 if (glGetError() != GL_NO_ERROR) {
2347 // error while rendering
2348 result = INVALID_OPERATION;
2349 } else {
2350 // allocate shared memory large enough to hold the
2351 // screen capture
2352 sp<MemoryHeapBase> base(
2353 new MemoryHeapBase(size, 0, "screen-capture") );
2354 void* const ptr = base->getBase();
2355 if (ptr) {
2356 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002357 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002358 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2359 if (glGetError() == GL_NO_ERROR) {
2360 *heap = base;
2361 *w = sw;
2362 *h = sh;
2363 *f = PIXEL_FORMAT_RGBA_8888;
2364 result = NO_ERROR;
2365 }
2366 } else {
2367 result = NO_MEMORY;
2368 }
2369 }
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002370 glDisable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002371 glViewport(0, 0, hw_w, hw_h);
2372 glMatrixMode(GL_PROJECTION);
2373 glPopMatrix();
2374 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 } else {
2376 result = BAD_VALUE;
2377 }
2378
2379 // release FBO resources
2380 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2381 glDeleteRenderbuffersOES(1, &tname);
2382 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002383
2384 hw.compositionComplete();
2385
Steve Block9d453682011-12-20 16:23:08 +00002386 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002387
Mathias Agopian74c40c02010-09-29 13:02:36 -07002388 return result;
2389}
2390
2391
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002392status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2393 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002394 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002395 uint32_t sw, uint32_t sh,
2396 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002397{
2398 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002399 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002400 return BAD_VALUE;
2401
2402 if (!GLExtensions::getInstance().haveFramebufferObject())
2403 return INVALID_OPERATION;
2404
2405 class MessageCaptureScreen : public MessageBase {
2406 SurfaceFlinger* flinger;
2407 DisplayID dpy;
2408 sp<IMemoryHeap>* heap;
2409 uint32_t* w;
2410 uint32_t* h;
2411 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002412 uint32_t sw;
2413 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002414 uint32_t minLayerZ;
2415 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002416 status_t result;
2417 public:
2418 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002419 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002420 uint32_t sw, uint32_t sh,
2421 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002422 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002423 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2424 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2425 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002426 {
2427 }
2428 status_t getResult() const {
2429 return result;
2430 }
2431 virtual bool handler() {
2432 Mutex::Autolock _l(flinger->mStateLock);
2433
2434 // if we have secure windows, never allow the screen capture
2435 if (flinger->mSecureFrameBuffer)
2436 return true;
2437
Mathias Agopian74c40c02010-09-29 13:02:36 -07002438 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002439 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002441 return true;
2442 }
2443 };
2444
2445 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002446 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447 status_t res = postMessageSync(msg);
2448 if (res == NO_ERROR) {
2449 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2450 }
2451 return res;
2452}
2453
2454// ---------------------------------------------------------------------------
2455
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002456sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2457{
2458 sp<Layer> result;
2459 Mutex::Autolock _l(mStateLock);
2460 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2461 return result;
2462}
2463
2464// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002465
Mathias Agopian96f08192010-06-02 23:28:45 -07002466Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002467 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002468{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469}
2470
Mathias Agopian96f08192010-06-02 23:28:45 -07002471Client::~Client()
2472{
Mathias Agopian96f08192010-06-02 23:28:45 -07002473 const size_t count = mLayers.size();
2474 for (size_t i=0 ; i<count ; i++) {
2475 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2476 if (layer != 0) {
2477 mFlinger->removeLayer(layer);
2478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 }
2480}
2481
Mathias Agopian96f08192010-06-02 23:28:45 -07002482status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002483 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002485
Mathias Agopian4f113742011-05-03 16:21:41 -07002486size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002487{
Mathias Agopian4f113742011-05-03 16:21:41 -07002488 Mutex::Autolock _l(mLock);
2489 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002490 mLayers.add(name, layer);
2491 return name;
2492}
2493
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002494void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002495{
Mathias Agopian4f113742011-05-03 16:21:41 -07002496 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002497 // we do a linear search here, because this doesn't happen often
2498 const size_t count = mLayers.size();
2499 for (size_t i=0 ; i<count ; i++) {
2500 if (mLayers.valueAt(i) == layer) {
2501 mLayers.removeItemsAt(i, 1);
2502 break;
2503 }
2504 }
2505}
Mathias Agopian4f113742011-05-03 16:21:41 -07002506sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2507{
2508 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002509 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002510 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002511 if (layer != 0) {
2512 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002513 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002514 }
2515 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516}
2517
Mathias Agopiana67932f2011-04-20 14:20:59 -07002518
2519status_t Client::onTransact(
2520 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2521{
2522 // these must be checked
2523 IPCThreadState* ipc = IPCThreadState::self();
2524 const int pid = ipc->getCallingPid();
2525 const int uid = ipc->getCallingUid();
2526 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002527 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002528 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002529 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002530 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002531 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002532 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2533 return PERMISSION_DENIED;
2534 }
2535 }
2536 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002537}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002538
2539
Mathias Agopian96f08192010-06-02 23:28:45 -07002540sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002541 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002542 const String8& name,
2543 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544 uint32_t flags)
2545{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002546 /*
2547 * createSurface must be called from the GL thread so that it can
2548 * have access to the GL context.
2549 */
2550
2551 class MessageCreateSurface : public MessageBase {
2552 sp<ISurface> result;
2553 SurfaceFlinger* flinger;
2554 ISurfaceComposerClient::surface_data_t* params;
2555 Client* client;
2556 const String8& name;
2557 DisplayID display;
2558 uint32_t w, h;
2559 PixelFormat format;
2560 uint32_t flags;
2561 public:
2562 MessageCreateSurface(SurfaceFlinger* flinger,
2563 ISurfaceComposerClient::surface_data_t* params,
2564 const String8& name, Client* client,
2565 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2566 uint32_t flags)
2567 : flinger(flinger), params(params), client(client), name(name),
2568 display(display), w(w), h(h), format(format), flags(flags)
2569 {
2570 }
2571 sp<ISurface> getResult() const { return result; }
2572 virtual bool handler() {
2573 result = flinger->createSurface(params, name, client,
2574 display, w, h, format, flags);
2575 return true;
2576 }
2577 };
2578
2579 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2580 params, name, this, display, w, h, format, flags);
2581 mFlinger->postMessageSync(msg);
2582 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583}
Mathias Agopian96f08192010-06-02 23:28:45 -07002584status_t Client::destroySurface(SurfaceID sid) {
2585 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002587
2588// ---------------------------------------------------------------------------
2589
Jamie Gennis9a78c902011-01-12 18:30:40 -08002590GraphicBufferAlloc::GraphicBufferAlloc() {}
2591
2592GraphicBufferAlloc::~GraphicBufferAlloc() {}
2593
2594sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002595 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002596 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2597 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002598 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002599 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002600 if (err == NO_MEMORY) {
2601 GraphicBuffer::dumpAllocationsToSystemLog();
2602 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002603 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002604 "failed (%s), handle=%p",
2605 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002606 return 0;
2607 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002608 return graphicBuffer;
2609}
2610
Jamie Gennis9a78c902011-01-12 18:30:40 -08002611// ---------------------------------------------------------------------------
2612
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002613GraphicPlane::GraphicPlane()
2614 : mHw(0)
2615{
2616}
2617
2618GraphicPlane::~GraphicPlane() {
2619 delete mHw;
2620}
2621
2622bool GraphicPlane::initialized() const {
2623 return mHw ? true : false;
2624}
2625
Mathias Agopian2b92d892010-02-08 15:49:35 -08002626int GraphicPlane::getWidth() const {
2627 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628}
2629
Mathias Agopian2b92d892010-02-08 15:49:35 -08002630int GraphicPlane::getHeight() const {
2631 return mHeight;
2632}
2633
2634void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2635{
2636 mHw = hw;
2637
2638 // initialize the display orientation transform.
2639 // it's a constant that should come from the display driver.
2640 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2641 char property[PROPERTY_VALUE_MAX];
2642 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2643 //displayOrientation
2644 switch (atoi(property)) {
2645 case 90:
2646 displayOrientation = ISurfaceComposer::eOrientation90;
2647 break;
2648 case 270:
2649 displayOrientation = ISurfaceComposer::eOrientation270;
2650 break;
2651 }
2652 }
2653
2654 const float w = hw->getWidth();
2655 const float h = hw->getHeight();
2656 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2657 &mDisplayTransform);
2658 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2659 mDisplayWidth = h;
2660 mDisplayHeight = w;
2661 } else {
2662 mDisplayWidth = w;
2663 mDisplayHeight = h;
2664 }
2665
2666 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002667}
2668
2669status_t GraphicPlane::orientationToTransfrom(
2670 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002671{
2672 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673 switch (orientation) {
2674 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002675 flags = Transform::ROT_0;
2676 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002678 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002679 break;
2680 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002681 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002682 break;
2683 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002684 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002685 break;
2686 default:
2687 return BAD_VALUE;
2688 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002689 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690 return NO_ERROR;
2691}
2692
2693status_t GraphicPlane::setOrientation(int orientation)
2694{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002695 // If the rotation can be handled in hardware, this is where
2696 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002697
2698 const DisplayHardware& hw(displayHardware());
2699 const float w = mDisplayWidth;
2700 const float h = mDisplayHeight;
2701 mWidth = int(w);
2702 mHeight = int(h);
2703
2704 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002705 GraphicPlane::orientationToTransfrom(orientation, w, h,
2706 &orientationTransform);
2707 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2708 mWidth = int(h);
2709 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002711
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002712 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002713 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002714 return NO_ERROR;
2715}
2716
2717const DisplayHardware& GraphicPlane::displayHardware() const {
2718 return *mHw;
2719}
2720
Mathias Agopian59119e62010-10-11 12:37:43 -07002721DisplayHardware& GraphicPlane::editDisplayHardware() {
2722 return *mHw;
2723}
2724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725const Transform& GraphicPlane::transform() const {
2726 return mGlobalTransform;
2727}
2728
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002729EGLDisplay GraphicPlane::getEGLDisplay() const {
2730 return mHw->getEGLDisplay();
2731}
2732
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733// ---------------------------------------------------------------------------
2734
2735}; // namespace android