blob: e2c7aeda1f56134e7bd831da70f197f39ce93d00 [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 Agopianed9807b2012-05-18 14:18:08 -070065#include <gui/BitTube.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070066
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080067#define EGL_VERSION_HW_ANDROID 0x3143
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#define DISPLAY_COUNT 1
70
71namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
Mathias Agopian99b49842011-06-27 16:05:52 -070074const String16 sHardwareTest("android.permission.HARDWARE_TEST");
75const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
76const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
77const String16 sDump("android.permission.DUMP");
78
79// ---------------------------------------------------------------------------
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081SurfaceFlinger::SurfaceFlinger()
82 : BnSurfaceComposer(), Thread(false),
83 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070084 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070088 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mSecureFrameBuffer(0)
100{
101 init();
102}
103
104void SurfaceFlinger::init()
105{
Steve Blocka19954a2012-01-04 20:05:49 +0000106 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 // debugging stuff...
109 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 property_get("debug.sf.showupdates", value, "0");
112 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
Colin Cross3854ed52012-03-23 14:17:18 -0700114#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115 property_get("debug.sf.ddms", value, "0");
116 mDebugDDMS = atoi(value);
117 if (mDebugDDMS) {
118 DdmConnection::start(getServiceName());
119 }
Colin Cross3854ed52012-03-23 14:17:18 -0700120#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Steve Blocka19954a2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124}
125
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800126void SurfaceFlinger::onFirstRef()
127{
128 mEventQueue.init(this);
129
130 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
131
132 // Wait for the main thread to be done with its initialization
133 mReadyToRunBarrier.wait();
134}
135
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137SurfaceFlinger::~SurfaceFlinger()
138{
139 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142void SurfaceFlinger::binderDied(const wp<IBinder>& who)
143{
144 // the window manager died on us. prepare its eulogy.
145
146 // reset screen orientation
147 Vector<ComposerState> state;
148 setTransactionState(state, eOrientationDefault, 0);
149
150 // restart the boot-animation
151 property_set("ctl.start", "bootanim");
152}
153
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700154sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700156 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157}
158
Mathias Agopian7e27f052010-05-28 14:22:23 -0700159sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Mathias Agopian96f08192010-06-02 23:28:45 -0700161 sp<ISurfaceComposerClient> bclient;
162 sp<Client> client(new Client(this));
163 status_t err = client->initCheck();
164 if (err == NO_ERROR) {
165 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return bclient;
168}
169
Jamie Gennis9a78c902011-01-12 18:30:40 -0800170sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
171{
172 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
173 return gba;
174}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
177{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000178 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 const GraphicPlane& plane(mGraphicPlanes[dpy]);
180 return plane;
181}
182
183GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
184{
185 return const_cast<GraphicPlane&>(
186 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
187}
188
189void SurfaceFlinger::bootFinished()
190{
191 const nsecs_t now = systemTime();
192 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000193 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700194 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700195
196 // wait patiently for the window manager death
197 const String16 name("window");
198 sp<IBinder> window(defaultServiceManager()->getService(name));
199 if (window != 0) {
200 window->linkToDeath(this);
201 }
202
203 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700204 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207static inline uint16_t pack565(int r, int g, int b) {
208 return (r<<11)|(g<<5)|b;
209}
210
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211status_t SurfaceFlinger::readyToRun()
212{
Steve Blocka19954a2012-01-04 20:05:49 +0000213 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 "Initializing graphics H/W...");
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 // we only support one display currently
217 int dpy = 0;
218
219 {
220 // initialize the main display
221 GraphicPlane& plane(graphicPlane(dpy));
222 DisplayHardware* const hw = new DisplayHardware(this, dpy);
223 plane.setDisplayHardware(hw);
224 }
225
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700226 // create the shared control-block
227 mServerHeap = new MemoryHeapBase(4096,
228 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000229 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700230
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700231 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000232 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700233
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700234 new(mServerCblk) surface_flinger_cblk_t;
235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 // initialize primary screen
237 // (other display should be initialized in the same manner, but
238 // asynchronously, as they could come and go. None of this is supported
239 // yet).
240 const GraphicPlane& plane(graphicPlane(dpy));
241 const DisplayHardware& hw = plane.displayHardware();
242 const uint32_t w = hw.getWidth();
243 const uint32_t h = hw.getHeight();
244 const uint32_t f = hw.getFormat();
245 hw.makeCurrent();
246
247 // initialize the shared control block
248 mServerCblk->connected |= 1<<dpy;
249 display_cblk_t* dcblk = mServerCblk->displays + dpy;
250 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800251 dcblk->w = plane.getWidth();
252 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 dcblk->format = f;
254 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
255 dcblk->xdpi = hw.getDpiX();
256 dcblk->ydpi = hw.getDpiY();
257 dcblk->fps = hw.getRefreshRate();
258 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259
260 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700262 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glShadeModel(GL_FLAT);
265 glDisable(GL_DITHER);
266 glDisable(GL_CULL_FACE);
267
268 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
269 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glGenTextures(1, &mWormholeTexName);
272 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
279
280 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
281 glGenTextures(1, &mProtectedTexName);
282 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
288 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290 glViewport(0, 0, w, h);
291 glMatrixMode(GL_PROJECTION);
292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700293 // put the origin in the left-bottom corner
294 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 -0800295
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800296
297 // start the EventThread
298 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800299 mEventQueue.setEventThread(mEventThread);
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 Agopian3e8b8532012-05-13 20:42:01 -0700803
804 HWComposer::LayerListIterator cur = hwc.begin();
805 const HWComposer::LayerListIterator end = hwc.end();
806 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
807 currentLayers[i]->setGeometry(*cur);
Mathias Agopian53331da2011-08-22 21:44:41 -0700808 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700809 cur->setSkip(true);
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700810 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700811 }
812 }
813}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700814
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815void SurfaceFlinger::handleRepaint()
816{
Mathias Agopian841cde52012-03-01 15:44:37 -0800817 ATRACE_CALL();
818
Mathias Agopianb8a55602009-06-26 19:06:36 -0700819 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700820 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821
Glenn Kasten99ed2242011-12-15 09:51:17 -0800822 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 debugFlashRegions();
824 }
825
Mathias Agopianb8a55602009-06-26 19:06:36 -0700826 // set the frame buffer
827 const DisplayHardware& hw(graphicPlane(0).displayHardware());
828 glMatrixMode(GL_MODELVIEW);
829 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700832 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
834 // takes a rectangle, we must make sure to update that whole
835 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700836 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700837 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700838 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700839 // We need to redraw the rectangle that will be updated
840 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700841 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700842 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700843 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700844 } else {
845 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700847 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 }
849 }
850
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700851 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 composeSurfaces(mDirtyRegion);
853
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700854 // update the swap region and clear the dirty region
855 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 mDirtyRegion.clear();
857}
858
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700859void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700861 const DisplayHardware& hw(graphicPlane(0).displayHardware());
862 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700863
864 HWComposer::LayerListIterator cur = hwc.begin();
865 const HWComposer::LayerListIterator end = hwc.end();
866 if (cur == end) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700867 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700868 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700869
Mathias Agopianf384cc32011-09-08 18:31:55 -0700870 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
871 size_t count = layers.size();
872
Steve Blocke6f43dd2012-01-06 19:20:56 +0000873 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700874 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
875 hwc.getNumLayers(), count);
876
877 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700878 if (hwc.initCheck() == NO_ERROR) {
879 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
880 }
Mathias Agopian45721772010-08-12 15:03:26 -0700881
882 /*
883 * update the per-frame h/w composer data for each layer
884 * and build the transparent region of the FB
885 */
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700886 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700887 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700888 layer->setPerFrameData(*cur);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700889 }
890 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000891 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700892}
Mathias Agopian45721772010-08-12 15:03:26 -0700893
Mathias Agopianf384cc32011-09-08 18:31:55 -0700894void SurfaceFlinger::composeSurfaces(const Region& dirty)
895{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700896 const DisplayHardware& hw(graphicPlane(0).displayHardware());
897 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700898 HWComposer::LayerListIterator cur = hwc.begin();
899 const HWComposer::LayerListIterator end = hwc.end();
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700900
901 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700902 if (cur==end || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700903 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700904
Mathias Agopianb9494d52012-04-18 02:28:45 -0700905 if (hwc.getLayerCount(HWC_OVERLAY)) {
906 // when using overlays, we assume a fully transparent framebuffer
907 // NOTE: we could reduce how much we need to clear, for instance
908 // remove where there are opaque FB layers. however, on some
909 // GPUs doing a "clean slate" glClear might be more efficient.
910 // We'll revisit later if needed.
911 glClearColor(0, 0, 0, 0);
912 glClear(GL_COLOR_BUFFER_BIT);
913 } else {
914 // screen is already cleared here
915 if (!mWormholeRegion.isEmpty()) {
916 // can happen with SurfaceView
917 drawWormhole();
918 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700919 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700920
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700921 /*
922 * and then, render the layers targeted at the framebuffer
923 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700924
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700925 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
926 const size_t count = layers.size();
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700927 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700928 const sp<LayerBase>& layer(layers[i]);
929 const Region clip(dirty.intersect(layer->visibleRegionScreen));
930 if (!clip.isEmpty()) {
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700931 if (cur->getCompositionType() == HWC_OVERLAY) {
932 if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700933 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700934 // never clear the very first layer since we're
935 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700936 layer->clearWithOpenGL(clip);
937 }
938 continue;
939 }
940 // render the layer
941 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700942 }
943 }
944 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945}
946
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947void SurfaceFlinger::debugFlashRegions()
948{
Mathias Agopian0a917752010-06-14 21:20:00 -0700949 const DisplayHardware& hw(graphicPlane(0).displayHardware());
950 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700951 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700952 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700953 return;
954 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700955
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700956 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700957 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
958 mDirtyRegion.bounds() : hw.bounds());
959 composeSurfaces(repaint);
960 }
961
Mathias Agopianc492e672011-10-18 14:49:27 -0700962 glDisable(GL_TEXTURE_EXTERNAL_OES);
963 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965
Mathias Agopian0926f502009-05-04 14:17:04 -0700966 static int toggle = 0;
967 toggle = 1 - toggle;
968 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700969 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700970 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700971 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700972 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973
Mathias Agopian20f68782009-05-11 00:03:41 -0700974 Region::const_iterator it = mDirtyRegion.begin();
975 Region::const_iterator const end = mDirtyRegion.end();
976 while (it != end) {
977 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700979 { r.left, height - r.top },
980 { r.left, height - r.bottom },
981 { r.right, height - r.bottom },
982 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 };
984 glVertexPointer(2, GL_FLOAT, 0, vertices);
985 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
986 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700987
Mathias Agopian0656a682011-09-20 17:22:44 -0700988 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989
990 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700991 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992}
993
994void SurfaceFlinger::drawWormhole() const
995{
996 const Region region(mWormholeRegion.intersect(mDirtyRegion));
997 if (region.isEmpty())
998 return;
999
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001000 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001001 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001002 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001003 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001004
1005 GLfloat vertices[4][2];
1006 glVertexPointer(2, GL_FLOAT, 0, vertices);
1007 Region::const_iterator it = region.begin();
1008 Region::const_iterator const end = region.end();
1009 while (it != end) {
1010 const Rect& r = *it++;
1011 vertices[0][0] = r.left;
1012 vertices[0][1] = r.top;
1013 vertices[1][0] = r.right;
1014 vertices[1][1] = r.top;
1015 vertices[2][0] = r.right;
1016 vertices[2][1] = r.bottom;
1017 vertices[3][0] = r.left;
1018 vertices[3][1] = r.bottom;
1019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1020 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021}
1022
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001023status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024{
1025 Mutex::Autolock _l(mStateLock);
1026 addLayer_l(layer);
1027 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1028 return NO_ERROR;
1029}
1030
Mathias Agopian96f08192010-06-02 23:28:45 -07001031status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1032{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001033 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001034 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1035}
1036
1037ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1038 const sp<LayerBaseClient>& lbc)
1039{
Mathias Agopian96f08192010-06-02 23:28:45 -07001040 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001041 size_t name = client->attachLayer(lbc);
1042
1043 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001044
1045 // add this layer to the current state list
1046 addLayer_l(lbc);
1047
Mathias Agopian4f113742011-05-03 16:21:41 -07001048 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001049}
1050
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001051status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052{
1053 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001054 status_t err = purgatorizeLayer_l(layer);
1055 if (err == NO_ERROR)
1056 setTransactionFlags(eTransactionNeeded);
1057 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058}
1059
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001060status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001062 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1063 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001064 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001065 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1067 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001068 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 return NO_ERROR;
1070 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001071 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
Mathias Agopian9a112062009-04-17 19:36:26 -07001074status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1075{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001076 // First add the layer to the purgatory list, which makes sure it won't
1077 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001078 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001079 if (err >= 0) {
1080 mLayerPurgatory.add(layerBase);
1081 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001082
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001083 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001084
Mathias Agopian3d579642009-06-04 18:46:21 -07001085 // it's possible that we don't find a layer, because it might
1086 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001087 // from the user because there is a race between Client::destroySurface(),
1088 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001089 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1090}
1091
Mathias Agopian96f08192010-06-02 23:28:45 -07001092status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093{
Mathias Agopian96f08192010-06-02 23:28:45 -07001094 layer->forceVisibilityTransaction();
1095 setTransactionFlags(eTraversalNeeded);
1096 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097}
1098
Mathias Agopiandea20b12011-05-03 17:04:02 -07001099uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1100{
1101 return android_atomic_release_load(&mTransactionFlags);
1102}
1103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1105{
1106 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1107}
1108
Mathias Agopianbb641242010-05-18 17:06:55 -07001109uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110{
1111 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1112 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001113 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114 }
1115 return old;
1116}
1117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001119void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001120 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001121 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001122
Jamie Gennis28378392011-10-12 17:39:00 -07001123 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001124 if (mCurrentState.orientation != orientation) {
1125 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1126 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001127 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001128 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001129 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001130 orientation);
1131 }
1132 }
1133
Mathias Agopian698c0872011-06-28 19:09:31 -07001134 const size_t count = state.size();
1135 for (size_t i=0 ; i<count ; i++) {
1136 const ComposerState& s(state[i]);
1137 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001138 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001139 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001140
Mathias Agopian386aa982011-11-07 21:58:03 -08001141 if (transactionFlags) {
1142 // this triggers the transaction
1143 setTransactionFlags(transactionFlags);
1144
1145 // if this is a synchronous transaction, wait for it to take effect
1146 // before returning.
1147 if (flags & eSynchronous) {
1148 mTransationPending = true;
1149 }
1150 while (mTransationPending) {
1151 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1152 if (CC_UNLIKELY(err != NO_ERROR)) {
1153 // just in case something goes wrong in SF, return to the
1154 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001155 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001156 mTransationPending = false;
1157 break;
1158 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001159 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 }
1161}
1162
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001163sp<ISurface> SurfaceFlinger::createSurface(
1164 ISurfaceComposerClient::surface_data_t* params,
1165 const String8& name,
1166 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1168 uint32_t flags)
1169{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001170 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001171 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001172
1173 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001174 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001175 int(w), int(h));
1176 return surfaceHandle;
1177 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001178
Mathias Agopianff615cc2012-02-24 14:58:36 -08001179 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001180 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 switch (flags & eFXSurfaceMask) {
1182 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001183 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1184 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 break;
1186 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001187 // for now we treat Blur as Dim, until we can implement it
1188 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001190 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001192 case eFXSurfaceScreenshot:
1193 layer = createScreenshotSurface(client, d, w, h, flags);
1194 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 }
1196
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001197 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001199 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001200 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001203 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001204 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001205 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001206 if (normalLayer != 0) {
1207 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001208 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001210 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001211
Mathias Agopian96f08192010-06-02 23:28:45 -07001212 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 }
1214
1215 return surfaceHandle;
1216}
1217
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001218sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001219 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001220 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001221 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222{
1223 // initialize the surfaces
1224 switch (format) { // TODO: take h/w into account
1225 case PIXEL_FORMAT_TRANSPARENT:
1226 case PIXEL_FORMAT_TRANSLUCENT:
1227 format = PIXEL_FORMAT_RGBA_8888;
1228 break;
1229 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001230#ifdef NO_RGBX_8888
1231 format = PIXEL_FORMAT_RGB_565;
1232#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001233 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001234#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 break;
1236 }
1237
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001238#ifdef NO_RGBX_8888
1239 if (format == PIXEL_FORMAT_RGBX_8888)
1240 format = PIXEL_FORMAT_RGBA_8888;
1241#endif
1242
Mathias Agopian96f08192010-06-02 23:28:45 -07001243 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001244 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001245 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001246 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001247 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 }
1249 return layer;
1250}
1251
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001252sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001253 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
Mathias Agopian96f08192010-06-02 23:28:45 -07001256 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001257 return layer;
1258}
1259
1260sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1261 const sp<Client>& client, DisplayID display,
1262 uint32_t w, uint32_t h, uint32_t flags)
1263{
1264 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 return layer;
1266}
1267
Mathias Agopian96f08192010-06-02 23:28:45 -07001268status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269{
Mathias Agopian9a112062009-04-17 19:36:26 -07001270 /*
1271 * called by the window manager, when a surface should be marked for
1272 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001273 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001274 * The surface is removed from the current and drawing lists, but placed
1275 * in the purgatory queue, so it's not destroyed right-away (we need
1276 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001277 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001278
Mathias Agopian48d819a2009-09-10 19:41:18 -07001279 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001280 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001281 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001282
Mathias Agopian48d819a2009-09-10 19:41:18 -07001283 if (layer != 0) {
1284 err = purgatorizeLayer_l(layer);
1285 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001286 setTransactionFlags(eTransactionNeeded);
1287 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001288 }
1289 return err;
1290}
1291
Mathias Agopianca4d3602011-05-19 15:38:14 -07001292status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001293{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001294 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001295 status_t err = NO_ERROR;
1296 sp<LayerBaseClient> l(layer.promote());
1297 if (l != NULL) {
1298 Mutex::Autolock _l(mStateLock);
1299 err = removeLayer_l(l);
1300 if (err == NAME_NOT_FOUND) {
1301 // The surface wasn't in the current list, which means it was
1302 // removed already, which means it is in the purgatory,
1303 // and need to be removed from there.
1304 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001305 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001306 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001307 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001308 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001309 "error removing layer=%p (%s)", l.get(), strerror(-err));
1310 }
1311 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312}
1313
Mathias Agopian698c0872011-06-28 19:09:31 -07001314uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001315 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001316 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001319 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1320 if (layer != 0) {
1321 const uint32_t what = s.what;
1322 if (what & ePositionChanged) {
1323 if (layer->setPosition(s.x, s.y))
1324 flags |= eTraversalNeeded;
1325 }
1326 if (what & eLayerChanged) {
1327 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1328 if (layer->setLayer(s.z)) {
1329 mCurrentState.layersSortedByZ.removeAt(idx);
1330 mCurrentState.layersSortedByZ.add(layer);
1331 // we need traversal (state changed)
1332 // AND transaction (list changed)
1333 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 }
1335 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001336 if (what & eSizeChanged) {
1337 if (layer->setSize(s.w, s.h)) {
1338 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001339 }
1340 }
1341 if (what & eAlphaChanged) {
1342 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1343 flags |= eTraversalNeeded;
1344 }
1345 if (what & eMatrixChanged) {
1346 if (layer->setMatrix(s.matrix))
1347 flags |= eTraversalNeeded;
1348 }
1349 if (what & eTransparentRegionChanged) {
1350 if (layer->setTransparentRegionHint(s.transparentRegion))
1351 flags |= eTraversalNeeded;
1352 }
1353 if (what & eVisibilityChanged) {
1354 if (layer->setFlags(s.flags, s.mask))
1355 flags |= eTraversalNeeded;
1356 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001357 if (what & eCropChanged) {
1358 if (layer->setCrop(s.crop))
1359 flags |= eTraversalNeeded;
1360 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001362 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363}
1364
Mathias Agopianb60314a2012-04-10 22:09:54 -07001365// ---------------------------------------------------------------------------
1366
1367void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001368 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001369 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1370 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001371 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001372 // this is a temporary work-around, eventually this should be called
1373 // by the power-manager
1374 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001375 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001376 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377}
1378
Mathias Agopianb60314a2012-04-10 22:09:54 -07001379void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001380 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001381 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1382 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001383 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001384 hw.releaseScreen();
1385 // from this point on, SF will stop drawing
1386 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387}
1388
Colin Cross8e533062012-06-07 13:17:52 -07001389void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001390 class MessageScreenAcquired : public MessageBase {
1391 SurfaceFlinger* flinger;
1392 public:
1393 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1394 virtual bool handler() {
1395 flinger->onScreenAcquired();
1396 return true;
1397 }
1398 };
1399 sp<MessageBase> msg = new MessageScreenAcquired(this);
1400 postMessageSync(msg);
1401}
1402
Colin Cross8e533062012-06-07 13:17:52 -07001403void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001404 class MessageScreenReleased : public MessageBase {
1405 SurfaceFlinger* flinger;
1406 public:
1407 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1408 virtual bool handler() {
1409 flinger->onScreenReleased();
1410 return true;
1411 }
1412 };
1413 sp<MessageBase> msg = new MessageScreenReleased(this);
1414 postMessageSync(msg);
1415}
1416
1417// ---------------------------------------------------------------------------
1418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1420{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001421 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 char buffer[SIZE];
1423 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001424
1425 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 snprintf(buffer, SIZE, "Permission Denial: "
1427 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1428 IPCThreadState::self()->getCallingPid(),
1429 IPCThreadState::self()->getCallingUid());
1430 result.append(buffer);
1431 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001432 // Try to get the main lock, but don't insist if we can't
1433 // (this would indicate SF is stuck, but we want to be able to
1434 // print something in dumpsys).
1435 int retry = 3;
1436 while (mStateLock.tryLock()<0 && --retry>=0) {
1437 usleep(1000000);
1438 }
1439 const bool locked(retry >= 0);
1440 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001441 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001442 "SurfaceFlinger appears to be unresponsive, "
1443 "dumping anyways (no locks held)\n");
1444 result.append(buffer);
1445 }
1446
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001447 bool dumpAll = true;
1448 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001449 size_t numArgs = args.size();
1450 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001451 if ((index < numArgs) &&
1452 (args[index] == String16("--list"))) {
1453 index++;
1454 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001455 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001456 }
1457
1458 if ((index < numArgs) &&
1459 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001460 index++;
1461 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001462 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001463 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001464
1465 if ((index < numArgs) &&
1466 (args[index] == String16("--latency-clear"))) {
1467 index++;
1468 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001469 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001470 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001472
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001473 if (dumpAll) {
1474 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001475 }
1476
Mathias Agopian9795c422009-08-26 16:36:26 -07001477 if (locked) {
1478 mStateLock.unlock();
1479 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 }
1481 write(fd, result.string(), result.size());
1482 return NO_ERROR;
1483}
1484
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001485void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1486 String8& result, char* buffer, size_t SIZE) const
1487{
1488 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1489 const size_t count = currentLayers.size();
1490 for (size_t i=0 ; i<count ; i++) {
1491 const sp<LayerBase>& layer(currentLayers[i]);
1492 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1493 result.append(buffer);
1494 }
1495}
1496
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001497void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1498 String8& result, char* buffer, size_t SIZE) const
1499{
1500 String8 name;
1501 if (index < args.size()) {
1502 name = String8(args[index]);
1503 index++;
1504 }
1505
1506 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1507 const size_t count = currentLayers.size();
1508 for (size_t i=0 ; i<count ; i++) {
1509 const sp<LayerBase>& layer(currentLayers[i]);
1510 if (name.isEmpty()) {
1511 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1512 result.append(buffer);
1513 }
1514 if (name.isEmpty() || (name == layer->getName())) {
1515 layer->dumpStats(result, buffer, SIZE);
1516 }
1517 }
1518}
1519
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001520void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1521 String8& result, char* buffer, size_t SIZE) const
1522{
1523 String8 name;
1524 if (index < args.size()) {
1525 name = String8(args[index]);
1526 index++;
1527 }
1528
1529 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1530 const size_t count = currentLayers.size();
1531 for (size_t i=0 ; i<count ; i++) {
1532 const sp<LayerBase>& layer(currentLayers[i]);
1533 if (name.isEmpty() || (name == layer->getName())) {
1534 layer->clearStats();
1535 }
1536 }
1537}
1538
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001539void SurfaceFlinger::dumpAllLocked(
1540 String8& result, char* buffer, size_t SIZE) const
1541{
1542 // figure out if we're stuck somewhere
1543 const nsecs_t now = systemTime();
1544 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1545 const nsecs_t inTransaction(mDebugInTransaction);
1546 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1547 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1548
1549 /*
1550 * Dump the visible layer list
1551 */
1552 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1553 const size_t count = currentLayers.size();
1554 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1555 result.append(buffer);
1556 for (size_t i=0 ; i<count ; i++) {
1557 const sp<LayerBase>& layer(currentLayers[i]);
1558 layer->dump(result, buffer, SIZE);
1559 }
1560
1561 /*
1562 * Dump the layers in the purgatory
1563 */
1564
1565 const size_t purgatorySize = mLayerPurgatory.size();
1566 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1567 result.append(buffer);
1568 for (size_t i=0 ; i<purgatorySize ; i++) {
1569 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1570 layer->shortDump(result, buffer, SIZE);
1571 }
1572
1573 /*
1574 * Dump SurfaceFlinger global state
1575 */
1576
1577 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1578 result.append(buffer);
1579
1580 const GLExtensions& extensions(GLExtensions::getInstance());
1581 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1582 extensions.getVendor(),
1583 extensions.getRenderer(),
1584 extensions.getVersion());
1585 result.append(buffer);
1586
1587 snprintf(buffer, SIZE, "EGL : %s\n",
1588 eglQueryString(graphicPlane(0).getEGLDisplay(),
1589 EGL_VERSION_HW_ANDROID));
1590 result.append(buffer);
1591
1592 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1593 result.append(buffer);
1594
1595 mWormholeRegion.dump(result, "WormholeRegion");
1596 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1597 snprintf(buffer, SIZE,
1598 " orientation=%d, canDraw=%d\n",
1599 mCurrentState.orientation, hw.canDraw());
1600 result.append(buffer);
1601 snprintf(buffer, SIZE,
1602 " last eglSwapBuffers() time: %f us\n"
1603 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001604 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001605 " refresh-rate : %f fps\n"
1606 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001607 " y-dpi : %f\n"
1608 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001609 mLastSwapBufferTime/1000.0,
1610 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001611 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001612 hw.getRefreshRate(),
1613 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001614 hw.getDpiY(),
1615 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001616 result.append(buffer);
1617
1618 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1619 inSwapBuffersDuration/1000.0);
1620 result.append(buffer);
1621
1622 snprintf(buffer, SIZE, " transaction time: %f us\n",
1623 inTransactionDuration/1000.0);
1624 result.append(buffer);
1625
1626 /*
1627 * VSYNC state
1628 */
1629 mEventThread->dump(result, buffer, SIZE);
1630
1631 /*
1632 * Dump HWComposer state
1633 */
1634 HWComposer& hwc(hw.getHwComposer());
1635 snprintf(buffer, SIZE, "h/w composer state:\n");
1636 result.append(buffer);
1637 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1638 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1639 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1640 result.append(buffer);
1641 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1642
1643 /*
1644 * Dump gralloc state
1645 */
1646 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1647 alloc.dump(result);
1648 hw.dump(result);
1649}
1650
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651status_t SurfaceFlinger::onTransact(
1652 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1653{
1654 switch (code) {
1655 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001656 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001659 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001660 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001661 case BLANK:
1662 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 {
1664 // codes that require permission check
1665 IPCThreadState* ipc = IPCThreadState::self();
1666 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001667 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001668 if ((uid != AID_GRAPHICS) &&
1669 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001670 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001671 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1672 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001674 break;
1675 }
1676 case CAPTURE_SCREEN:
1677 {
1678 // codes that require permission check
1679 IPCThreadState* ipc = IPCThreadState::self();
1680 const int pid = ipc->getCallingPid();
1681 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001682 if ((uid != AID_GRAPHICS) &&
1683 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001684 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001685 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1686 return PERMISSION_DENIED;
1687 }
1688 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 }
1690 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1693 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001694 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001695 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001696 IPCThreadState* ipc = IPCThreadState::self();
1697 const int pid = ipc->getCallingPid();
1698 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001699 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001700 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 return PERMISSION_DENIED;
1702 }
1703 int n;
1704 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001705 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001706 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 return NO_ERROR;
1708 case 1002: // SHOW_UPDATES
1709 n = data.readInt32();
1710 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001711 invalidateHwcGeometry();
1712 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001715 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001716 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001718 case 1005:{ // force transaction
1719 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1720 return NO_ERROR;
1721 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001722 case 1006:{ // send empty update
1723 signalRefresh();
1724 return NO_ERROR;
1725 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001726 case 1008: // toggle use of hw composer
1727 n = data.readInt32();
1728 mDebugDisableHWC = n ? 1 : 0;
1729 invalidateHwcGeometry();
1730 repaintEverything();
1731 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001732 case 1009: // toggle use of transform hint
1733 n = data.readInt32();
1734 mDebugDisableTransformHint = n ? 1 : 0;
1735 invalidateHwcGeometry();
1736 repaintEverything();
1737 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001739 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 reply->writeInt32(0);
1741 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001742 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001743 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 return NO_ERROR;
1745 case 1013: {
1746 Mutex::Autolock _l(mStateLock);
1747 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1748 reply->writeInt32(hw.getPageFlipCount());
1749 }
1750 return NO_ERROR;
1751 }
1752 }
1753 return err;
1754}
1755
Mathias Agopian53331da2011-08-22 21:44:41 -07001756void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001757 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001758 const Rect bounds(hw.getBounds());
1759 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001760 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001761}
1762
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001763void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1764 Mutex::Autolock _l(mInvalidateLock);
1765 mInvalidateRegion = reg;
1766}
1767
1768Region SurfaceFlinger::getAndClearInvalidateRegion() {
1769 Mutex::Autolock _l(mInvalidateLock);
1770 Region reg(mInvalidateRegion);
1771 mInvalidateRegion.clear();
1772 return reg;
1773}
1774
Mathias Agopian59119e62010-10-11 12:37:43 -07001775// ---------------------------------------------------------------------------
1776
Mathias Agopian118d0242011-10-13 16:02:48 -07001777status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1778 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1779{
1780 Mutex::Autolock _l(mStateLock);
1781 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1782}
1783
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001784status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1785 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001786{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001787 ATRACE_CALL();
1788
Mathias Agopian59119e62010-10-11 12:37:43 -07001789 if (!GLExtensions::getInstance().haveFramebufferObject())
1790 return INVALID_OPERATION;
1791
1792 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001793 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001794 const uint32_t hw_w = hw.getWidth();
1795 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001796 GLfloat u = 1;
1797 GLfloat v = 1;
1798
1799 // make sure to clear all GL error flags
1800 while ( glGetError() != GL_NO_ERROR ) ;
1801
1802 // create a FBO
1803 GLuint name, tname;
1804 glGenTextures(1, &tname);
1805 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001806 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1807 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001808 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1809 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001810 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001811 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001812 GLint tw = (2 << (31 - clz(hw_w)));
1813 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001814 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1815 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001816 u = GLfloat(hw_w) / tw;
1817 v = GLfloat(hw_h) / th;
1818 }
1819 glGenFramebuffersOES(1, &name);
1820 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001821 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1822 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001823
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001824 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001825 glDisable(GL_TEXTURE_EXTERNAL_OES);
1826 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001827 glClearColor(0,0,0,1);
1828 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001829 glMatrixMode(GL_MODELVIEW);
1830 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001831 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1832 const size_t count = layers.size();
1833 for (size_t i=0 ; i<count ; ++i) {
1834 const sp<LayerBase>& layer(layers[i]);
1835 layer->drawForSreenShot();
1836 }
1837
Mathias Agopian118d0242011-10-13 16:02:48 -07001838 hw.compositionComplete();
1839
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001840 // back to main framebuffer
1841 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 glDeleteFramebuffersOES(1, &name);
1843
1844 *textureName = tname;
1845 *uOut = u;
1846 *vOut = v;
1847 return NO_ERROR;
1848}
1849
1850// ---------------------------------------------------------------------------
1851
Mathias Agopianed9807b2012-05-18 14:18:08 -07001852class VSyncWaiter {
1853 DisplayEventReceiver::Event buffer[4];
1854 sp<Looper> looper;
1855 sp<IDisplayEventConnection> events;
1856 sp<BitTube> eventTube;
1857public:
1858 VSyncWaiter(const sp<EventThread>& eventThread) {
1859 looper = new Looper(true);
1860 events = eventThread->createEventConnection();
1861 eventTube = events->getDataChannel();
1862 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1863 events->requestNextVsync();
1864 }
1865
1866 void wait() {
1867 ssize_t n;
1868
1869 looper->pollOnce(-1);
1870 // we don't handle any errors here, it doesn't matter
1871 // and we don't want to take the risk to get stuck.
1872
1873 // drain the events...
1874 while ((n = DisplayEventReceiver::getEvents(
1875 eventTube, buffer, 4)) > 0) ;
1876
1877 events->requestNextVsync();
1878 }
1879};
1880
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001881status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1882{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001883 // get screen geometry
1884 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1885 const uint32_t hw_w = hw.getWidth();
1886 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001887 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001888
1889 GLfloat u, v;
1890 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001891 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001892 if (result != NO_ERROR) {
1893 return result;
1894 }
1895
1896 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001897 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001898 glBindTexture(GL_TEXTURE_2D, tname);
1899 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1900 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1901 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001902 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1903 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001904 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1905 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1906 glVertexPointer(2, GL_FLOAT, 0, vtx);
1907
Mathias Agopianffcf4652011-07-07 17:30:31 -07001908 /*
1909 * Texture coordinate mapping
1910 *
1911 * u
1912 * 1 +----------+---+
1913 * | | | | image is inverted
1914 * | V | | w.r.t. the texture
1915 * 1-v +----------+ | coordinates
1916 * | |
1917 * | |
1918 * | |
1919 * 0 +--------------+
1920 * 0 1
1921 *
1922 */
1923
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001924 class s_curve_interpolator {
1925 const float nbFrames, s, v;
1926 public:
1927 s_curve_interpolator(int nbFrames, float s)
1928 : nbFrames(1.0f / (nbFrames-1)), s(s),
1929 v(1.0f + expf(-s + 0.5f*s)) {
1930 }
1931 float operator()(int f) {
1932 const float x = f * nbFrames;
1933 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1934 }
1935 };
1936
1937 class v_stretch {
1938 const GLfloat hw_w, hw_h;
1939 public:
1940 v_stretch(uint32_t hw_w, uint32_t hw_h)
1941 : hw_w(hw_w), hw_h(hw_h) {
1942 }
1943 void operator()(GLfloat* vtx, float v) {
1944 const GLfloat w = hw_w + (hw_w * v);
1945 const GLfloat h = hw_h - (hw_h * v);
1946 const GLfloat x = (hw_w - w) * 0.5f;
1947 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001948 vtx[0] = x; vtx[1] = y;
1949 vtx[2] = x; vtx[3] = y + h;
1950 vtx[4] = x + w; vtx[5] = y + h;
1951 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001952 }
1953 };
1954
1955 class h_stretch {
1956 const GLfloat hw_w, hw_h;
1957 public:
1958 h_stretch(uint32_t hw_w, uint32_t hw_h)
1959 : hw_w(hw_w), hw_h(hw_h) {
1960 }
1961 void operator()(GLfloat* vtx, float v) {
1962 const GLfloat w = hw_w - (hw_w * v);
1963 const GLfloat h = 1.0f;
1964 const GLfloat x = (hw_w - w) * 0.5f;
1965 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001966 vtx[0] = x; vtx[1] = y;
1967 vtx[2] = x; vtx[3] = y + h;
1968 vtx[4] = x + w; vtx[5] = y + h;
1969 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001970 }
1971 };
1972
Mathias Agopianed9807b2012-05-18 14:18:08 -07001973 VSyncWaiter vsync(mEventThread);
1974
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001975 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001976 char value[PROPERTY_VALUE_MAX];
1977 property_get("debug.sf.electron_frames", value, "24");
1978 int nbFrames = (atoi(value) + 1) >> 1;
1979 if (nbFrames <= 0) // just in case
1980 nbFrames = 24;
1981
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001982 s_curve_interpolator itr(nbFrames, 7.5f);
1983 s_curve_interpolator itg(nbFrames, 8.0f);
1984 s_curve_interpolator itb(nbFrames, 8.5f);
1985
1986 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001987
1988 glMatrixMode(GL_TEXTURE);
1989 glLoadIdentity();
1990 glMatrixMode(GL_MODELVIEW);
1991 glLoadIdentity();
1992
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001993 glEnable(GL_BLEND);
1994 glBlendFunc(GL_ONE, GL_ONE);
1995 for (int i=0 ; i<nbFrames ; i++) {
1996 float x, y, w, h;
1997 const float vr = itr(i);
1998 const float vg = itg(i);
1999 const float vb = itb(i);
2000
Mathias Agopianed9807b2012-05-18 14:18:08 -07002001 // wait for vsync
2002 vsync.wait();
2003
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 // clear screen
2005 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002006 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002007 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002008
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 // draw the red plane
2010 vverts(vtx, vr);
2011 glColorMask(1,0,0,1);
2012 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002013
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002014 // draw the green plane
2015 vverts(vtx, vg);
2016 glColorMask(0,1,0,1);
2017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002018
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002019 // draw the blue plane
2020 vverts(vtx, vb);
2021 glColorMask(0,0,1,1);
2022 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002023
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002024 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002025 glDisable(GL_TEXTURE_2D);
2026 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002027 glColor4f(vg, vg, vg, 1);
2028 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2029 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002030 }
2031
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002032 h_stretch hverts(hw_w, hw_h);
2033 glDisable(GL_BLEND);
2034 glDisable(GL_TEXTURE_2D);
2035 glColorMask(1,1,1,1);
2036 for (int i=0 ; i<nbFrames ; i++) {
2037 const float v = itg(i);
2038 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002039
2040 // wait for vsync
2041 vsync.wait();
2042
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002043 glClear(GL_COLOR_BUFFER_BIT);
2044 glColor4f(1-v, 1-v, 1-v, 1);
2045 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2046 hw.flip(screenBounds);
2047 }
2048
2049 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002050 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2051 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002052 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002053 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002054 return NO_ERROR;
2055}
2056
2057status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2058{
2059 status_t result = PERMISSION_DENIED;
2060
2061 if (!GLExtensions::getInstance().haveFramebufferObject())
2062 return INVALID_OPERATION;
2063
2064
2065 // get screen geometry
2066 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2067 const uint32_t hw_w = hw.getWidth();
2068 const uint32_t hw_h = hw.getHeight();
2069 const Region screenBounds(hw.bounds());
2070
2071 GLfloat u, v;
2072 GLuint tname;
2073 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2074 if (result != NO_ERROR) {
2075 return result;
2076 }
2077
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002078 GLfloat vtx[8];
2079 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002080 glBindTexture(GL_TEXTURE_2D, tname);
2081 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2082 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2083 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002084 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2085 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002086 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2087 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2088 glVertexPointer(2, GL_FLOAT, 0, vtx);
2089
2090 class s_curve_interpolator {
2091 const float nbFrames, s, v;
2092 public:
2093 s_curve_interpolator(int nbFrames, float s)
2094 : nbFrames(1.0f / (nbFrames-1)), s(s),
2095 v(1.0f + expf(-s + 0.5f*s)) {
2096 }
2097 float operator()(int f) {
2098 const float x = f * nbFrames;
2099 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2100 }
2101 };
2102
2103 class v_stretch {
2104 const GLfloat hw_w, hw_h;
2105 public:
2106 v_stretch(uint32_t hw_w, uint32_t hw_h)
2107 : hw_w(hw_w), hw_h(hw_h) {
2108 }
2109 void operator()(GLfloat* vtx, float v) {
2110 const GLfloat w = hw_w + (hw_w * v);
2111 const GLfloat h = hw_h - (hw_h * v);
2112 const GLfloat x = (hw_w - w) * 0.5f;
2113 const GLfloat y = (hw_h - h) * 0.5f;
2114 vtx[0] = x; vtx[1] = y;
2115 vtx[2] = x; vtx[3] = y + h;
2116 vtx[4] = x + w; vtx[5] = y + h;
2117 vtx[6] = x + w; vtx[7] = y;
2118 }
2119 };
2120
2121 class h_stretch {
2122 const GLfloat hw_w, hw_h;
2123 public:
2124 h_stretch(uint32_t hw_w, uint32_t hw_h)
2125 : hw_w(hw_w), hw_h(hw_h) {
2126 }
2127 void operator()(GLfloat* vtx, float v) {
2128 const GLfloat w = hw_w - (hw_w * v);
2129 const GLfloat h = 1.0f;
2130 const GLfloat x = (hw_w - w) * 0.5f;
2131 const GLfloat y = (hw_h - h) * 0.5f;
2132 vtx[0] = x; vtx[1] = y;
2133 vtx[2] = x; vtx[3] = y + h;
2134 vtx[4] = x + w; vtx[5] = y + h;
2135 vtx[6] = x + w; vtx[7] = y;
2136 }
2137 };
2138
Mathias Agopianed9807b2012-05-18 14:18:08 -07002139 VSyncWaiter vsync(mEventThread);
2140
Mathias Agopiana6546e52010-10-14 12:33:07 -07002141 // the full animation is 12 frames
2142 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002143 s_curve_interpolator itr(nbFrames, 7.5f);
2144 s_curve_interpolator itg(nbFrames, 8.0f);
2145 s_curve_interpolator itb(nbFrames, 8.5f);
2146
2147 h_stretch hverts(hw_w, hw_h);
2148 glDisable(GL_BLEND);
2149 glDisable(GL_TEXTURE_2D);
2150 glColorMask(1,1,1,1);
2151 for (int i=nbFrames-1 ; i>=0 ; i--) {
2152 const float v = itg(i);
2153 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002154
2155 // wait for vsync
2156 vsync.wait();
2157
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002158 glClear(GL_COLOR_BUFFER_BIT);
2159 glColor4f(1-v, 1-v, 1-v, 1);
2160 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2161 hw.flip(screenBounds);
2162 }
2163
Mathias Agopiana6546e52010-10-14 12:33:07 -07002164 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002165 v_stretch vverts(hw_w, hw_h);
2166 glEnable(GL_BLEND);
2167 glBlendFunc(GL_ONE, GL_ONE);
2168 for (int i=nbFrames-1 ; i>=0 ; i--) {
2169 float x, y, w, h;
2170 const float vr = itr(i);
2171 const float vg = itg(i);
2172 const float vb = itb(i);
2173
Mathias Agopianed9807b2012-05-18 14:18:08 -07002174 // wait for vsync
2175 vsync.wait();
2176
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002177 // clear screen
2178 glColorMask(1,1,1,1);
2179 glClear(GL_COLOR_BUFFER_BIT);
2180 glEnable(GL_TEXTURE_2D);
2181
2182 // draw the red plane
2183 vverts(vtx, vr);
2184 glColorMask(1,0,0,1);
2185 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2186
2187 // draw the green plane
2188 vverts(vtx, vg);
2189 glColorMask(0,1,0,1);
2190 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2191
2192 // draw the blue plane
2193 vverts(vtx, vb);
2194 glColorMask(0,0,1,1);
2195 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2196
2197 hw.flip(screenBounds);
2198 }
2199
2200 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002201 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002202 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002203 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002204 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002205
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002206 return NO_ERROR;
2207}
2208
2209// ---------------------------------------------------------------------------
2210
Mathias Agopianabd671a2010-10-14 14:54:06 -07002211status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002213 ATRACE_CALL();
2214
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002215 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2216 if (!hw.canDraw()) {
2217 // we're already off
2218 return NO_ERROR;
2219 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002220
2221 // turn off hwc while we're doing the animation
2222 hw.getHwComposer().disable();
2223 // and make sure to turn it back on (if needed) next time we compose
2224 invalidateHwcGeometry();
2225
Mathias Agopianabd671a2010-10-14 14:54:06 -07002226 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2227 electronBeamOffAnimationImplLocked();
2228 }
2229
2230 // always clear the whole screen at the end of the animation
2231 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002232 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002233 hw.flip( Region(hw.bounds()) );
2234
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002235 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002236}
2237
2238status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2239{
Mathias Agopian59119e62010-10-11 12:37:43 -07002240 class MessageTurnElectronBeamOff : public MessageBase {
2241 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002242 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002243 status_t result;
2244 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002245 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2246 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002247 }
2248 status_t getResult() const {
2249 return result;
2250 }
2251 virtual bool handler() {
2252 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002253 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002254 return true;
2255 }
2256 };
2257
Mathias Agopianabd671a2010-10-14 14:54:06 -07002258 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002259 status_t res = postMessageSync(msg);
2260 if (res == NO_ERROR) {
2261 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002262
2263 // work-around: when the power-manager calls us we activate the
2264 // animation. eventually, the "on" animation will be called
2265 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002266 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002267 }
2268 return res;
2269}
2270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002271// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002272
Mathias Agopianabd671a2010-10-14 14:54:06 -07002273status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002274{
2275 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2276 if (hw.canDraw()) {
2277 // we're already on
2278 return NO_ERROR;
2279 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002280 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2281 electronBeamOnAnimationImplLocked();
2282 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002283
2284 // make sure to redraw the whole screen when the animation is done
2285 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002286 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002287
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002288 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002289}
2290
2291status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2292{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002293 class MessageTurnElectronBeamOn : public MessageBase {
2294 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002295 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002296 status_t result;
2297 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002298 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2299 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002300 }
2301 status_t getResult() const {
2302 return result;
2303 }
2304 virtual bool handler() {
2305 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002306 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002307 return true;
2308 }
2309 };
2310
Mathias Agopianabd671a2010-10-14 14:54:06 -07002311 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002312 return NO_ERROR;
2313}
2314
2315// ---------------------------------------------------------------------------
2316
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2318 sp<IMemoryHeap>* heap,
2319 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002320 uint32_t sw, uint32_t sh,
2321 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002323 ATRACE_CALL();
2324
Mathias Agopian74c40c02010-09-29 13:02:36 -07002325 status_t result = PERMISSION_DENIED;
2326
2327 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002328 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002329 return BAD_VALUE;
2330
2331 if (!GLExtensions::getInstance().haveFramebufferObject())
2332 return INVALID_OPERATION;
2333
2334 // get screen geometry
2335 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2336 const uint32_t hw_w = hw.getWidth();
2337 const uint32_t hw_h = hw.getHeight();
2338
2339 if ((sw > hw_w) || (sh > hw_h))
2340 return BAD_VALUE;
2341
2342 sw = (!sw) ? hw_w : sw;
2343 sh = (!sh) ? hw_h : sh;
2344 const size_t size = sw * sh * 4;
2345
Steve Block9d453682011-12-20 16:23:08 +00002346 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002347 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002348
Mathias Agopian74c40c02010-09-29 13:02:36 -07002349 // make sure to clear all GL error flags
2350 while ( glGetError() != GL_NO_ERROR ) ;
2351
2352 // create a FBO
2353 GLuint name, tname;
2354 glGenRenderbuffersOES(1, &tname);
2355 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2356 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002357
Mathias Agopian74c40c02010-09-29 13:02:36 -07002358 glGenFramebuffersOES(1, &name);
2359 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2360 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2361 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2362
2363 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002364
Mathias Agopian74c40c02010-09-29 13:02:36 -07002365 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2366
2367 // invert everything, b/c glReadPixel() below will invert the FB
2368 glViewport(0, 0, sw, sh);
2369 glMatrixMode(GL_PROJECTION);
2370 glPushMatrix();
2371 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002372 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002373 glMatrixMode(GL_MODELVIEW);
2374
2375 // redraw the screen entirely...
2376 glClearColor(0,0,0,1);
2377 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002378
Jamie Gennis9575f602011-10-07 14:51:16 -07002379 const LayerVector& layers(mDrawingState.layersSortedByZ);
2380 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002381 for (size_t i=0 ; i<count ; ++i) {
2382 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002383 const uint32_t flags = layer->drawingState().flags;
2384 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2385 const uint32_t z = layer->drawingState().z;
2386 if (z >= minLayerZ && z <= maxLayerZ) {
2387 layer->drawForSreenShot();
2388 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002389 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002390 }
2391
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 // check for errors and return screen capture
2393 if (glGetError() != GL_NO_ERROR) {
2394 // error while rendering
2395 result = INVALID_OPERATION;
2396 } else {
2397 // allocate shared memory large enough to hold the
2398 // screen capture
2399 sp<MemoryHeapBase> base(
2400 new MemoryHeapBase(size, 0, "screen-capture") );
2401 void* const ptr = base->getBase();
2402 if (ptr) {
2403 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002404 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002405 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2406 if (glGetError() == GL_NO_ERROR) {
2407 *heap = base;
2408 *w = sw;
2409 *h = sh;
2410 *f = PIXEL_FORMAT_RGBA_8888;
2411 result = NO_ERROR;
2412 }
2413 } else {
2414 result = NO_MEMORY;
2415 }
2416 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002417 glViewport(0, 0, hw_w, hw_h);
2418 glMatrixMode(GL_PROJECTION);
2419 glPopMatrix();
2420 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002421 } else {
2422 result = BAD_VALUE;
2423 }
2424
2425 // release FBO resources
2426 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2427 glDeleteRenderbuffersOES(1, &tname);
2428 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002429
2430 hw.compositionComplete();
2431
Steve Block9d453682011-12-20 16:23:08 +00002432 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002433
Mathias Agopian74c40c02010-09-29 13:02:36 -07002434 return result;
2435}
2436
2437
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002438status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2439 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002440 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002441 uint32_t sw, uint32_t sh,
2442 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443{
2444 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002445 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002446 return BAD_VALUE;
2447
2448 if (!GLExtensions::getInstance().haveFramebufferObject())
2449 return INVALID_OPERATION;
2450
2451 class MessageCaptureScreen : public MessageBase {
2452 SurfaceFlinger* flinger;
2453 DisplayID dpy;
2454 sp<IMemoryHeap>* heap;
2455 uint32_t* w;
2456 uint32_t* h;
2457 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002458 uint32_t sw;
2459 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002460 uint32_t minLayerZ;
2461 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002462 status_t result;
2463 public:
2464 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002465 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002466 uint32_t sw, uint32_t sh,
2467 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002468 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002469 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2470 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2471 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002472 {
2473 }
2474 status_t getResult() const {
2475 return result;
2476 }
2477 virtual bool handler() {
2478 Mutex::Autolock _l(flinger->mStateLock);
2479
2480 // if we have secure windows, never allow the screen capture
2481 if (flinger->mSecureFrameBuffer)
2482 return true;
2483
Mathias Agopian74c40c02010-09-29 13:02:36 -07002484 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002485 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002486
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002487 return true;
2488 }
2489 };
2490
2491 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002492 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002493 status_t res = postMessageSync(msg);
2494 if (res == NO_ERROR) {
2495 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2496 }
2497 return res;
2498}
2499
2500// ---------------------------------------------------------------------------
2501
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002502sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2503{
2504 sp<Layer> result;
2505 Mutex::Autolock _l(mStateLock);
2506 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2507 return result;
2508}
2509
2510// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511
Mathias Agopian96f08192010-06-02 23:28:45 -07002512Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002513 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515}
2516
Mathias Agopian96f08192010-06-02 23:28:45 -07002517Client::~Client()
2518{
Mathias Agopian96f08192010-06-02 23:28:45 -07002519 const size_t count = mLayers.size();
2520 for (size_t i=0 ; i<count ; i++) {
2521 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2522 if (layer != 0) {
2523 mFlinger->removeLayer(layer);
2524 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002525 }
2526}
2527
Mathias Agopian96f08192010-06-02 23:28:45 -07002528status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002529 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002531
Mathias Agopian4f113742011-05-03 16:21:41 -07002532size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002533{
Mathias Agopian4f113742011-05-03 16:21:41 -07002534 Mutex::Autolock _l(mLock);
2535 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002536 mLayers.add(name, layer);
2537 return name;
2538}
2539
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002540void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002541{
Mathias Agopian4f113742011-05-03 16:21:41 -07002542 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002543 // we do a linear search here, because this doesn't happen often
2544 const size_t count = mLayers.size();
2545 for (size_t i=0 ; i<count ; i++) {
2546 if (mLayers.valueAt(i) == layer) {
2547 mLayers.removeItemsAt(i, 1);
2548 break;
2549 }
2550 }
2551}
Mathias Agopian4f113742011-05-03 16:21:41 -07002552sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2553{
2554 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002555 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002556 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002557 if (layer != 0) {
2558 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002559 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002560 }
2561 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562}
2563
Mathias Agopiana67932f2011-04-20 14:20:59 -07002564
2565status_t Client::onTransact(
2566 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2567{
2568 // these must be checked
2569 IPCThreadState* ipc = IPCThreadState::self();
2570 const int pid = ipc->getCallingPid();
2571 const int uid = ipc->getCallingUid();
2572 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002573 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002574 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002575 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002576 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002577 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002578 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2579 return PERMISSION_DENIED;
2580 }
2581 }
2582 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002583}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002584
2585
Mathias Agopian96f08192010-06-02 23:28:45 -07002586sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002587 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002588 const String8& name,
2589 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 uint32_t flags)
2591{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002592 /*
2593 * createSurface must be called from the GL thread so that it can
2594 * have access to the GL context.
2595 */
2596
2597 class MessageCreateSurface : public MessageBase {
2598 sp<ISurface> result;
2599 SurfaceFlinger* flinger;
2600 ISurfaceComposerClient::surface_data_t* params;
2601 Client* client;
2602 const String8& name;
2603 DisplayID display;
2604 uint32_t w, h;
2605 PixelFormat format;
2606 uint32_t flags;
2607 public:
2608 MessageCreateSurface(SurfaceFlinger* flinger,
2609 ISurfaceComposerClient::surface_data_t* params,
2610 const String8& name, Client* client,
2611 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2612 uint32_t flags)
2613 : flinger(flinger), params(params), client(client), name(name),
2614 display(display), w(w), h(h), format(format), flags(flags)
2615 {
2616 }
2617 sp<ISurface> getResult() const { return result; }
2618 virtual bool handler() {
2619 result = flinger->createSurface(params, name, client,
2620 display, w, h, format, flags);
2621 return true;
2622 }
2623 };
2624
2625 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2626 params, name, this, display, w, h, format, flags);
2627 mFlinger->postMessageSync(msg);
2628 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629}
Mathias Agopian96f08192010-06-02 23:28:45 -07002630status_t Client::destroySurface(SurfaceID sid) {
2631 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633
2634// ---------------------------------------------------------------------------
2635
Jamie Gennis9a78c902011-01-12 18:30:40 -08002636GraphicBufferAlloc::GraphicBufferAlloc() {}
2637
2638GraphicBufferAlloc::~GraphicBufferAlloc() {}
2639
2640sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002641 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002642 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2643 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002644 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002645 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002646 if (err == NO_MEMORY) {
2647 GraphicBuffer::dumpAllocationsToSystemLog();
2648 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002649 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002650 "failed (%s), handle=%p",
2651 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002652 return 0;
2653 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002654 return graphicBuffer;
2655}
2656
Jamie Gennis9a78c902011-01-12 18:30:40 -08002657// ---------------------------------------------------------------------------
2658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002659GraphicPlane::GraphicPlane()
2660 : mHw(0)
2661{
2662}
2663
2664GraphicPlane::~GraphicPlane() {
2665 delete mHw;
2666}
2667
2668bool GraphicPlane::initialized() const {
2669 return mHw ? true : false;
2670}
2671
Mathias Agopian2b92d892010-02-08 15:49:35 -08002672int GraphicPlane::getWidth() const {
2673 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002674}
2675
Mathias Agopian2b92d892010-02-08 15:49:35 -08002676int GraphicPlane::getHeight() const {
2677 return mHeight;
2678}
2679
2680void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2681{
2682 mHw = hw;
2683
2684 // initialize the display orientation transform.
2685 // it's a constant that should come from the display driver.
2686 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2687 char property[PROPERTY_VALUE_MAX];
2688 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2689 //displayOrientation
2690 switch (atoi(property)) {
2691 case 90:
2692 displayOrientation = ISurfaceComposer::eOrientation90;
2693 break;
2694 case 270:
2695 displayOrientation = ISurfaceComposer::eOrientation270;
2696 break;
2697 }
2698 }
2699
2700 const float w = hw->getWidth();
2701 const float h = hw->getHeight();
2702 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2703 &mDisplayTransform);
2704 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2705 mDisplayWidth = h;
2706 mDisplayHeight = w;
2707 } else {
2708 mDisplayWidth = w;
2709 mDisplayHeight = h;
2710 }
2711
2712 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002713}
2714
2715status_t GraphicPlane::orientationToTransfrom(
2716 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002717{
2718 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002719 switch (orientation) {
2720 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002721 flags = Transform::ROT_0;
2722 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002723 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002724 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 break;
2726 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002727 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002728 break;
2729 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002730 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002731 break;
2732 default:
2733 return BAD_VALUE;
2734 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002735 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736 return NO_ERROR;
2737}
2738
2739status_t GraphicPlane::setOrientation(int orientation)
2740{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002741 // If the rotation can be handled in hardware, this is where
2742 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002743
2744 const DisplayHardware& hw(displayHardware());
2745 const float w = mDisplayWidth;
2746 const float h = mDisplayHeight;
2747 mWidth = int(w);
2748 mHeight = int(h);
2749
2750 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002751 GraphicPlane::orientationToTransfrom(orientation, w, h,
2752 &orientationTransform);
2753 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2754 mWidth = int(h);
2755 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002756 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002757
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002758 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002759 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760 return NO_ERROR;
2761}
2762
2763const DisplayHardware& GraphicPlane::displayHardware() const {
2764 return *mHw;
2765}
2766
Mathias Agopian59119e62010-10-11 12:37:43 -07002767DisplayHardware& GraphicPlane::editDisplayHardware() {
2768 return *mHw;
2769}
2770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002771const Transform& GraphicPlane::transform() const {
2772 return mGlobalTransform;
2773}
2774
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002775EGLDisplay GraphicPlane::getEGLDisplay() const {
2776 return mHw->getEGLDisplay();
2777}
2778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002779// ---------------------------------------------------------------------------
2780
2781}; // namespace android