blob: 61b5f71e0d6e7b234f078b09e76d64ef304d833d [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 Agopian45721772010-08-12 15:03:26 -0700803 hwc_layer_t* const cur(hwc.getLayers());
804 for (size_t i=0 ; cur && i<count ; i++) {
805 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700806 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700807 cur[i].compositionType = HWC_FRAMEBUFFER;
808 cur[i].flags |= HWC_SKIP_LAYER;
809 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700810 }
811 }
812}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814void SurfaceFlinger::handleRepaint()
815{
Mathias Agopian841cde52012-03-01 15:44:37 -0800816 ATRACE_CALL();
817
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700819 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820
Glenn Kasten99ed2242011-12-15 09:51:17 -0800821 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 debugFlashRegions();
823 }
824
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // set the frame buffer
826 const DisplayHardware& hw(graphicPlane(0).displayHardware());
827 glMatrixMode(GL_MODELVIEW);
828 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
830 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700831 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700832 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
833 // takes a rectangle, we must make sure to update that whole
834 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700835 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700837 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // We need to redraw the rectangle that will be updated
839 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700840 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700842 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 } else {
844 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700846 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
848 }
849
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700850 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 composeSurfaces(mDirtyRegion);
852
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853 // update the swap region and clear the dirty region
854 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.clear();
856}
857
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700858void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700860 const DisplayHardware& hw(graphicPlane(0).displayHardware());
861 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700862 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700863 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700864 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700865 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700866
Mathias Agopianf384cc32011-09-08 18:31:55 -0700867 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
868 size_t count = layers.size();
869
Steve Blocke6f43dd2012-01-06 19:20:56 +0000870 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700871 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
872 hwc.getNumLayers(), count);
873
874 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700875 if (hwc.initCheck() == NO_ERROR) {
876 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
877 }
Mathias Agopian45721772010-08-12 15:03:26 -0700878
879 /*
880 * update the per-frame h/w composer data for each layer
881 * and build the transparent region of the FB
882 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700883 for (size_t i=0 ; i<count ; i++) {
884 const sp<LayerBase>& layer(layers[i]);
885 layer->setPerFrameData(&cur[i]);
886 }
887 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000888 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700889}
Mathias Agopian45721772010-08-12 15:03:26 -0700890
Mathias Agopianf384cc32011-09-08 18:31:55 -0700891void SurfaceFlinger::composeSurfaces(const Region& dirty)
892{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700893 const DisplayHardware& hw(graphicPlane(0).displayHardware());
894 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700895 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700896
897 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700898 if (!cur || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700899 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900
Mathias Agopianb9494d52012-04-18 02:28:45 -0700901 if (hwc.getLayerCount(HWC_OVERLAY)) {
902 // when using overlays, we assume a fully transparent framebuffer
903 // NOTE: we could reduce how much we need to clear, for instance
904 // remove where there are opaque FB layers. however, on some
905 // GPUs doing a "clean slate" glClear might be more efficient.
906 // We'll revisit later if needed.
907 glClearColor(0, 0, 0, 0);
908 glClear(GL_COLOR_BUFFER_BIT);
909 } else {
910 // screen is already cleared here
911 if (!mWormholeRegion.isEmpty()) {
912 // can happen with SurfaceView
913 drawWormhole();
914 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700915 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700916
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700917 /*
918 * and then, render the layers targeted at the framebuffer
919 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700920
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700921 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
922 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700923
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700925 const sp<LayerBase>& layer(layers[i]);
926 const Region clip(dirty.intersect(layer->visibleRegionScreen));
927 if (!clip.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700928 if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
929 if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700931 // never clear the very first layer since we're
932 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700933 layer->clearWithOpenGL(clip);
934 }
935 continue;
936 }
937 // render the layer
938 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700939 }
940 }
941 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942}
943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944void SurfaceFlinger::debugFlashRegions()
945{
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 const DisplayHardware& hw(graphicPlane(0).displayHardware());
947 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700948 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700949 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700950 return;
951 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700952
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700953 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700954 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
955 mDirtyRegion.bounds() : hw.bounds());
956 composeSurfaces(repaint);
957 }
958
Mathias Agopianc492e672011-10-18 14:49:27 -0700959 glDisable(GL_TEXTURE_EXTERNAL_OES);
960 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962
Mathias Agopian0926f502009-05-04 14:17:04 -0700963 static int toggle = 0;
964 toggle = 1 - toggle;
965 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700966 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700967 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700968 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700969 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970
Mathias Agopian20f68782009-05-11 00:03:41 -0700971 Region::const_iterator it = mDirtyRegion.begin();
972 Region::const_iterator const end = mDirtyRegion.end();
973 while (it != end) {
974 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700976 { r.left, height - r.top },
977 { r.left, height - r.bottom },
978 { r.right, height - r.bottom },
979 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980 };
981 glVertexPointer(2, GL_FLOAT, 0, vertices);
982 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
983 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700984
Mathias Agopian0656a682011-09-20 17:22:44 -0700985 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986
987 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700988 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989}
990
991void SurfaceFlinger::drawWormhole() const
992{
993 const Region region(mWormholeRegion.intersect(mDirtyRegion));
994 if (region.isEmpty())
995 return;
996
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700997 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -0700998 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700999 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001000 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001001
1002 GLfloat vertices[4][2];
1003 glVertexPointer(2, GL_FLOAT, 0, vertices);
1004 Region::const_iterator it = region.begin();
1005 Region::const_iterator const end = region.end();
1006 while (it != end) {
1007 const Rect& r = *it++;
1008 vertices[0][0] = r.left;
1009 vertices[0][1] = r.top;
1010 vertices[1][0] = r.right;
1011 vertices[1][1] = r.top;
1012 vertices[2][0] = r.right;
1013 vertices[2][1] = r.bottom;
1014 vertices[3][0] = r.left;
1015 vertices[3][1] = r.bottom;
1016 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1017 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018}
1019
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001020status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021{
1022 Mutex::Autolock _l(mStateLock);
1023 addLayer_l(layer);
1024 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1025 return NO_ERROR;
1026}
1027
Mathias Agopian96f08192010-06-02 23:28:45 -07001028status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1029{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001030 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001031 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1032}
1033
1034ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1035 const sp<LayerBaseClient>& lbc)
1036{
Mathias Agopian96f08192010-06-02 23:28:45 -07001037 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001038 size_t name = client->attachLayer(lbc);
1039
1040 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001041
1042 // add this layer to the current state list
1043 addLayer_l(lbc);
1044
Mathias Agopian4f113742011-05-03 16:21:41 -07001045 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001046}
1047
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001048status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049{
1050 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001051 status_t err = purgatorizeLayer_l(layer);
1052 if (err == NO_ERROR)
1053 setTransactionFlags(eTransactionNeeded);
1054 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055}
1056
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001057status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001059 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1060 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001061 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001062 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1064 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001065 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 return NO_ERROR;
1067 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001068 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Mathias Agopian9a112062009-04-17 19:36:26 -07001071status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1072{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001073 // First add the layer to the purgatory list, which makes sure it won't
1074 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001075 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001076 if (err >= 0) {
1077 mLayerPurgatory.add(layerBase);
1078 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001079
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001080 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001081
Mathias Agopian3d579642009-06-04 18:46:21 -07001082 // it's possible that we don't find a layer, because it might
1083 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001084 // from the user because there is a race between Client::destroySurface(),
1085 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001086 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1087}
1088
Mathias Agopian96f08192010-06-02 23:28:45 -07001089status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090{
Mathias Agopian96f08192010-06-02 23:28:45 -07001091 layer->forceVisibilityTransaction();
1092 setTransactionFlags(eTraversalNeeded);
1093 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094}
1095
Mathias Agopiandea20b12011-05-03 17:04:02 -07001096uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1097{
1098 return android_atomic_release_load(&mTransactionFlags);
1099}
1100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1102{
1103 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1104}
1105
Mathias Agopianbb641242010-05-18 17:06:55 -07001106uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107{
1108 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1109 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001110 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 }
1112 return old;
1113}
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001116void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001117 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001118 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001119
Jamie Gennis28378392011-10-12 17:39:00 -07001120 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001121 if (mCurrentState.orientation != orientation) {
1122 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1123 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001124 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001125 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001126 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001127 orientation);
1128 }
1129 }
1130
Mathias Agopian698c0872011-06-28 19:09:31 -07001131 const size_t count = state.size();
1132 for (size_t i=0 ; i<count ; i++) {
1133 const ComposerState& s(state[i]);
1134 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001135 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001136 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001137
Mathias Agopian386aa982011-11-07 21:58:03 -08001138 if (transactionFlags) {
1139 // this triggers the transaction
1140 setTransactionFlags(transactionFlags);
1141
1142 // if this is a synchronous transaction, wait for it to take effect
1143 // before returning.
1144 if (flags & eSynchronous) {
1145 mTransationPending = true;
1146 }
1147 while (mTransationPending) {
1148 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1149 if (CC_UNLIKELY(err != NO_ERROR)) {
1150 // just in case something goes wrong in SF, return to the
1151 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001152 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001153 mTransationPending = false;
1154 break;
1155 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001156 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 }
1158}
1159
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001160sp<ISurface> SurfaceFlinger::createSurface(
1161 ISurfaceComposerClient::surface_data_t* params,
1162 const String8& name,
1163 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1165 uint32_t flags)
1166{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001167 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001168 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001169
1170 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001171 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001172 int(w), int(h));
1173 return surfaceHandle;
1174 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001175
Mathias Agopianff615cc2012-02-24 14:58:36 -08001176 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001177 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 switch (flags & eFXSurfaceMask) {
1179 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001180 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1181 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 break;
1183 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001184 // for now we treat Blur as Dim, until we can implement it
1185 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001187 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001189 case eFXSurfaceScreenshot:
1190 layer = createScreenshotSurface(client, d, w, h, flags);
1191 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 }
1193
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001194 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001195 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001196 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001197 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001200 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001201 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001202 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001203 if (normalLayer != 0) {
1204 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001205 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001206 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001207 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001208
Mathias Agopian96f08192010-06-02 23:28:45 -07001209 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 }
1211
1212 return surfaceHandle;
1213}
1214
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001215sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001216 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001217 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001218 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219{
1220 // initialize the surfaces
1221 switch (format) { // TODO: take h/w into account
1222 case PIXEL_FORMAT_TRANSPARENT:
1223 case PIXEL_FORMAT_TRANSLUCENT:
1224 format = PIXEL_FORMAT_RGBA_8888;
1225 break;
1226 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001227#ifdef NO_RGBX_8888
1228 format = PIXEL_FORMAT_RGB_565;
1229#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001230 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001231#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232 break;
1233 }
1234
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001235#ifdef NO_RGBX_8888
1236 if (format == PIXEL_FORMAT_RGBX_8888)
1237 format = PIXEL_FORMAT_RGBA_8888;
1238#endif
1239
Mathias Agopian96f08192010-06-02 23:28:45 -07001240 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001241 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001242 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001243 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001244 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 }
1246 return layer;
1247}
1248
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001249sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001250 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001251 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252{
Mathias Agopian96f08192010-06-02 23:28:45 -07001253 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001254 return layer;
1255}
1256
1257sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1258 const sp<Client>& client, DisplayID display,
1259 uint32_t w, uint32_t h, uint32_t flags)
1260{
1261 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 return layer;
1263}
1264
Mathias Agopian96f08192010-06-02 23:28:45 -07001265status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266{
Mathias Agopian9a112062009-04-17 19:36:26 -07001267 /*
1268 * called by the window manager, when a surface should be marked for
1269 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001270 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001271 * The surface is removed from the current and drawing lists, but placed
1272 * in the purgatory queue, so it's not destroyed right-away (we need
1273 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001274 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001275
Mathias Agopian48d819a2009-09-10 19:41:18 -07001276 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001277 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001278 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001279
Mathias Agopian48d819a2009-09-10 19:41:18 -07001280 if (layer != 0) {
1281 err = purgatorizeLayer_l(layer);
1282 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001283 setTransactionFlags(eTransactionNeeded);
1284 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001285 }
1286 return err;
1287}
1288
Mathias Agopianca4d3602011-05-19 15:38:14 -07001289status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001290{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001291 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001292 status_t err = NO_ERROR;
1293 sp<LayerBaseClient> l(layer.promote());
1294 if (l != NULL) {
1295 Mutex::Autolock _l(mStateLock);
1296 err = removeLayer_l(l);
1297 if (err == NAME_NOT_FOUND) {
1298 // The surface wasn't in the current list, which means it was
1299 // removed already, which means it is in the purgatory,
1300 // and need to be removed from there.
1301 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001302 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001303 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001304 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001305 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001306 "error removing layer=%p (%s)", l.get(), strerror(-err));
1307 }
1308 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309}
1310
Mathias Agopian698c0872011-06-28 19:09:31 -07001311uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001312 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001313 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001316 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1317 if (layer != 0) {
1318 const uint32_t what = s.what;
1319 if (what & ePositionChanged) {
1320 if (layer->setPosition(s.x, s.y))
1321 flags |= eTraversalNeeded;
1322 }
1323 if (what & eLayerChanged) {
1324 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1325 if (layer->setLayer(s.z)) {
1326 mCurrentState.layersSortedByZ.removeAt(idx);
1327 mCurrentState.layersSortedByZ.add(layer);
1328 // we need traversal (state changed)
1329 // AND transaction (list changed)
1330 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 }
1332 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001333 if (what & eSizeChanged) {
1334 if (layer->setSize(s.w, s.h)) {
1335 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001336 }
1337 }
1338 if (what & eAlphaChanged) {
1339 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1340 flags |= eTraversalNeeded;
1341 }
1342 if (what & eMatrixChanged) {
1343 if (layer->setMatrix(s.matrix))
1344 flags |= eTraversalNeeded;
1345 }
1346 if (what & eTransparentRegionChanged) {
1347 if (layer->setTransparentRegionHint(s.transparentRegion))
1348 flags |= eTraversalNeeded;
1349 }
1350 if (what & eVisibilityChanged) {
1351 if (layer->setFlags(s.flags, s.mask))
1352 flags |= eTraversalNeeded;
1353 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001354 if (what & eCropChanged) {
1355 if (layer->setCrop(s.crop))
1356 flags |= eTraversalNeeded;
1357 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001359 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360}
1361
Mathias Agopianb60314a2012-04-10 22:09:54 -07001362// ---------------------------------------------------------------------------
1363
1364void SurfaceFlinger::onScreenAcquired() {
Colin Cross8e533062012-06-07 13:17:52 -07001365 ALOGD("Screen about to return, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001366 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1367 hw.acquireScreen();
Mathias Agopian22ffb112012-04-10 21:04:02 -07001368 mEventThread->onScreenAcquired();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001369 // this is a temporary work-around, eventually this should be called
1370 // by the power-manager
1371 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
Mathias Agopian22ffb112012-04-10 21:04:02 -07001372 // from this point on, SF will process updates again
Mathias Agopian8acce202012-04-13 16:18:55 -07001373 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374}
1375
Mathias Agopianb60314a2012-04-10 22:09:54 -07001376void SurfaceFlinger::onScreenReleased() {
Colin Cross8e533062012-06-07 13:17:52 -07001377 ALOGD("About to give-up screen, flinger = %p", this);
Mathias Agopianb60314a2012-04-10 22:09:54 -07001378 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1379 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001380 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001381 hw.releaseScreen();
1382 // from this point on, SF will stop drawing
1383 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384}
1385
Colin Cross8e533062012-06-07 13:17:52 -07001386void SurfaceFlinger::unblank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001387 class MessageScreenAcquired : public MessageBase {
1388 SurfaceFlinger* flinger;
1389 public:
1390 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1391 virtual bool handler() {
1392 flinger->onScreenAcquired();
1393 return true;
1394 }
1395 };
1396 sp<MessageBase> msg = new MessageScreenAcquired(this);
1397 postMessageSync(msg);
1398}
1399
Colin Cross8e533062012-06-07 13:17:52 -07001400void SurfaceFlinger::blank() {
Mathias Agopianb60314a2012-04-10 22:09:54 -07001401 class MessageScreenReleased : public MessageBase {
1402 SurfaceFlinger* flinger;
1403 public:
1404 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1405 virtual bool handler() {
1406 flinger->onScreenReleased();
1407 return true;
1408 }
1409 };
1410 sp<MessageBase> msg = new MessageScreenReleased(this);
1411 postMessageSync(msg);
1412}
1413
1414// ---------------------------------------------------------------------------
1415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1417{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001418 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 char buffer[SIZE];
1420 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001421
1422 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 snprintf(buffer, SIZE, "Permission Denial: "
1424 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1425 IPCThreadState::self()->getCallingPid(),
1426 IPCThreadState::self()->getCallingUid());
1427 result.append(buffer);
1428 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001429 // Try to get the main lock, but don't insist if we can't
1430 // (this would indicate SF is stuck, but we want to be able to
1431 // print something in dumpsys).
1432 int retry = 3;
1433 while (mStateLock.tryLock()<0 && --retry>=0) {
1434 usleep(1000000);
1435 }
1436 const bool locked(retry >= 0);
1437 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001438 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001439 "SurfaceFlinger appears to be unresponsive, "
1440 "dumping anyways (no locks held)\n");
1441 result.append(buffer);
1442 }
1443
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001444 bool dumpAll = true;
1445 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001446 size_t numArgs = args.size();
1447 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001448 if ((index < numArgs) &&
1449 (args[index] == String16("--list"))) {
1450 index++;
1451 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001452 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001453 }
1454
1455 if ((index < numArgs) &&
1456 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001457 index++;
1458 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001459 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001460 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001461
1462 if ((index < numArgs) &&
1463 (args[index] == String16("--latency-clear"))) {
1464 index++;
1465 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001466 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001467 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001469
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001470 if (dumpAll) {
1471 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001472 }
1473
Mathias Agopian9795c422009-08-26 16:36:26 -07001474 if (locked) {
1475 mStateLock.unlock();
1476 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 }
1478 write(fd, result.string(), result.size());
1479 return NO_ERROR;
1480}
1481
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001482void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1483 String8& result, char* buffer, size_t SIZE) const
1484{
1485 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1486 const size_t count = currentLayers.size();
1487 for (size_t i=0 ; i<count ; i++) {
1488 const sp<LayerBase>& layer(currentLayers[i]);
1489 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1490 result.append(buffer);
1491 }
1492}
1493
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001494void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1495 String8& result, char* buffer, size_t SIZE) const
1496{
1497 String8 name;
1498 if (index < args.size()) {
1499 name = String8(args[index]);
1500 index++;
1501 }
1502
1503 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1504 const size_t count = currentLayers.size();
1505 for (size_t i=0 ; i<count ; i++) {
1506 const sp<LayerBase>& layer(currentLayers[i]);
1507 if (name.isEmpty()) {
1508 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1509 result.append(buffer);
1510 }
1511 if (name.isEmpty() || (name == layer->getName())) {
1512 layer->dumpStats(result, buffer, SIZE);
1513 }
1514 }
1515}
1516
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001517void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1518 String8& result, char* buffer, size_t SIZE) const
1519{
1520 String8 name;
1521 if (index < args.size()) {
1522 name = String8(args[index]);
1523 index++;
1524 }
1525
1526 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1527 const size_t count = currentLayers.size();
1528 for (size_t i=0 ; i<count ; i++) {
1529 const sp<LayerBase>& layer(currentLayers[i]);
1530 if (name.isEmpty() || (name == layer->getName())) {
1531 layer->clearStats();
1532 }
1533 }
1534}
1535
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001536void SurfaceFlinger::dumpAllLocked(
1537 String8& result, char* buffer, size_t SIZE) const
1538{
1539 // figure out if we're stuck somewhere
1540 const nsecs_t now = systemTime();
1541 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1542 const nsecs_t inTransaction(mDebugInTransaction);
1543 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1544 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1545
1546 /*
1547 * Dump the visible layer list
1548 */
1549 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1550 const size_t count = currentLayers.size();
1551 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1552 result.append(buffer);
1553 for (size_t i=0 ; i<count ; i++) {
1554 const sp<LayerBase>& layer(currentLayers[i]);
1555 layer->dump(result, buffer, SIZE);
1556 }
1557
1558 /*
1559 * Dump the layers in the purgatory
1560 */
1561
1562 const size_t purgatorySize = mLayerPurgatory.size();
1563 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1564 result.append(buffer);
1565 for (size_t i=0 ; i<purgatorySize ; i++) {
1566 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1567 layer->shortDump(result, buffer, SIZE);
1568 }
1569
1570 /*
1571 * Dump SurfaceFlinger global state
1572 */
1573
1574 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1575 result.append(buffer);
1576
1577 const GLExtensions& extensions(GLExtensions::getInstance());
1578 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1579 extensions.getVendor(),
1580 extensions.getRenderer(),
1581 extensions.getVersion());
1582 result.append(buffer);
1583
1584 snprintf(buffer, SIZE, "EGL : %s\n",
1585 eglQueryString(graphicPlane(0).getEGLDisplay(),
1586 EGL_VERSION_HW_ANDROID));
1587 result.append(buffer);
1588
1589 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1590 result.append(buffer);
1591
1592 mWormholeRegion.dump(result, "WormholeRegion");
1593 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1594 snprintf(buffer, SIZE,
1595 " orientation=%d, canDraw=%d\n",
1596 mCurrentState.orientation, hw.canDraw());
1597 result.append(buffer);
1598 snprintf(buffer, SIZE,
1599 " last eglSwapBuffers() time: %f us\n"
1600 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001601 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001602 " refresh-rate : %f fps\n"
1603 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001604 " y-dpi : %f\n"
1605 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001606 mLastSwapBufferTime/1000.0,
1607 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001608 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001609 hw.getRefreshRate(),
1610 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001611 hw.getDpiY(),
1612 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001613 result.append(buffer);
1614
1615 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1616 inSwapBuffersDuration/1000.0);
1617 result.append(buffer);
1618
1619 snprintf(buffer, SIZE, " transaction time: %f us\n",
1620 inTransactionDuration/1000.0);
1621 result.append(buffer);
1622
1623 /*
1624 * VSYNC state
1625 */
1626 mEventThread->dump(result, buffer, SIZE);
1627
1628 /*
1629 * Dump HWComposer state
1630 */
1631 HWComposer& hwc(hw.getHwComposer());
1632 snprintf(buffer, SIZE, "h/w composer state:\n");
1633 result.append(buffer);
1634 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1635 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1636 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1637 result.append(buffer);
1638 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1639
1640 /*
1641 * Dump gralloc state
1642 */
1643 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1644 alloc.dump(result);
1645 hw.dump(result);
1646}
1647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648status_t SurfaceFlinger::onTransact(
1649 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1650{
1651 switch (code) {
1652 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001653 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001656 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001657 case TURN_ELECTRON_BEAM_ON:
Colin Cross8e533062012-06-07 13:17:52 -07001658 case BLANK:
1659 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 {
1661 // codes that require permission check
1662 IPCThreadState* ipc = IPCThreadState::self();
1663 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001664 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001665 if ((uid != AID_GRAPHICS) &&
1666 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001667 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001668 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1669 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001671 break;
1672 }
1673 case CAPTURE_SCREEN:
1674 {
1675 // codes that require permission check
1676 IPCThreadState* ipc = IPCThreadState::self();
1677 const int pid = ipc->getCallingPid();
1678 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001679 if ((uid != AID_GRAPHICS) &&
1680 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001681 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001682 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1683 return PERMISSION_DENIED;
1684 }
1685 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 }
1687 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001688
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1690 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001691 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001692 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001693 IPCThreadState* ipc = IPCThreadState::self();
1694 const int pid = ipc->getCallingPid();
1695 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001696 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001697 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 return PERMISSION_DENIED;
1699 }
1700 int n;
1701 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001702 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001703 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 return NO_ERROR;
1705 case 1002: // SHOW_UPDATES
1706 n = data.readInt32();
1707 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001708 invalidateHwcGeometry();
1709 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001712 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001713 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001715 case 1005:{ // force transaction
1716 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1717 return NO_ERROR;
1718 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001719 case 1006:{ // send empty update
1720 signalRefresh();
1721 return NO_ERROR;
1722 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001723 case 1008: // toggle use of hw composer
1724 n = data.readInt32();
1725 mDebugDisableHWC = n ? 1 : 0;
1726 invalidateHwcGeometry();
1727 repaintEverything();
1728 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001729 case 1009: // toggle use of transform hint
1730 n = data.readInt32();
1731 mDebugDisableTransformHint = n ? 1 : 0;
1732 invalidateHwcGeometry();
1733 repaintEverything();
1734 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001736 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 reply->writeInt32(0);
1738 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001739 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001740 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001741 return NO_ERROR;
1742 case 1013: {
1743 Mutex::Autolock _l(mStateLock);
1744 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1745 reply->writeInt32(hw.getPageFlipCount());
1746 }
1747 return NO_ERROR;
1748 }
1749 }
1750 return err;
1751}
1752
Mathias Agopian53331da2011-08-22 21:44:41 -07001753void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001754 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001755 const Rect bounds(hw.getBounds());
1756 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001757 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001758}
1759
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001760void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1761 Mutex::Autolock _l(mInvalidateLock);
1762 mInvalidateRegion = reg;
1763}
1764
1765Region SurfaceFlinger::getAndClearInvalidateRegion() {
1766 Mutex::Autolock _l(mInvalidateLock);
1767 Region reg(mInvalidateRegion);
1768 mInvalidateRegion.clear();
1769 return reg;
1770}
1771
Mathias Agopian59119e62010-10-11 12:37:43 -07001772// ---------------------------------------------------------------------------
1773
Mathias Agopian118d0242011-10-13 16:02:48 -07001774status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1775 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1776{
1777 Mutex::Autolock _l(mStateLock);
1778 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1779}
1780
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001781status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1782 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001783{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001784 ATRACE_CALL();
1785
Mathias Agopian59119e62010-10-11 12:37:43 -07001786 if (!GLExtensions::getInstance().haveFramebufferObject())
1787 return INVALID_OPERATION;
1788
1789 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001790 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001791 const uint32_t hw_w = hw.getWidth();
1792 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001793 GLfloat u = 1;
1794 GLfloat v = 1;
1795
1796 // make sure to clear all GL error flags
1797 while ( glGetError() != GL_NO_ERROR ) ;
1798
1799 // create a FBO
1800 GLuint name, tname;
1801 glGenTextures(1, &tname);
1802 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001803 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1804 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001805 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1806 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001807 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001808 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001809 GLint tw = (2 << (31 - clz(hw_w)));
1810 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001811 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1812 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001813 u = GLfloat(hw_w) / tw;
1814 v = GLfloat(hw_h) / th;
1815 }
1816 glGenFramebuffersOES(1, &name);
1817 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1819 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001820
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001821 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001822 glDisable(GL_TEXTURE_EXTERNAL_OES);
1823 glDisable(GL_TEXTURE_2D);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001824 glClearColor(0,0,0,1);
1825 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001826 glMatrixMode(GL_MODELVIEW);
1827 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001828 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1829 const size_t count = layers.size();
1830 for (size_t i=0 ; i<count ; ++i) {
1831 const sp<LayerBase>& layer(layers[i]);
1832 layer->drawForSreenShot();
1833 }
1834
Mathias Agopian118d0242011-10-13 16:02:48 -07001835 hw.compositionComplete();
1836
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001837 // back to main framebuffer
1838 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001839 glDeleteFramebuffersOES(1, &name);
1840
1841 *textureName = tname;
1842 *uOut = u;
1843 *vOut = v;
1844 return NO_ERROR;
1845}
1846
1847// ---------------------------------------------------------------------------
1848
Mathias Agopianed9807b2012-05-18 14:18:08 -07001849class VSyncWaiter {
1850 DisplayEventReceiver::Event buffer[4];
1851 sp<Looper> looper;
1852 sp<IDisplayEventConnection> events;
1853 sp<BitTube> eventTube;
1854public:
1855 VSyncWaiter(const sp<EventThread>& eventThread) {
1856 looper = new Looper(true);
1857 events = eventThread->createEventConnection();
1858 eventTube = events->getDataChannel();
1859 looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
1860 events->requestNextVsync();
1861 }
1862
1863 void wait() {
1864 ssize_t n;
1865
1866 looper->pollOnce(-1);
1867 // we don't handle any errors here, it doesn't matter
1868 // and we don't want to take the risk to get stuck.
1869
1870 // drain the events...
1871 while ((n = DisplayEventReceiver::getEvents(
1872 eventTube, buffer, 4)) > 0) ;
1873
1874 events->requestNextVsync();
1875 }
1876};
1877
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001878status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1879{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001880 // get screen geometry
1881 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1882 const uint32_t hw_w = hw.getWidth();
1883 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001884 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001885
1886 GLfloat u, v;
1887 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001888 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001889 if (result != NO_ERROR) {
1890 return result;
1891 }
1892
1893 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001894 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001895 glBindTexture(GL_TEXTURE_2D, tname);
1896 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1897 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1898 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001899 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1900 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001901 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1902 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1903 glVertexPointer(2, GL_FLOAT, 0, vtx);
1904
Mathias Agopianffcf4652011-07-07 17:30:31 -07001905 /*
1906 * Texture coordinate mapping
1907 *
1908 * u
1909 * 1 +----------+---+
1910 * | | | | image is inverted
1911 * | V | | w.r.t. the texture
1912 * 1-v +----------+ | coordinates
1913 * | |
1914 * | |
1915 * | |
1916 * 0 +--------------+
1917 * 0 1
1918 *
1919 */
1920
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001921 class s_curve_interpolator {
1922 const float nbFrames, s, v;
1923 public:
1924 s_curve_interpolator(int nbFrames, float s)
1925 : nbFrames(1.0f / (nbFrames-1)), s(s),
1926 v(1.0f + expf(-s + 0.5f*s)) {
1927 }
1928 float operator()(int f) {
1929 const float x = f * nbFrames;
1930 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1931 }
1932 };
1933
1934 class v_stretch {
1935 const GLfloat hw_w, hw_h;
1936 public:
1937 v_stretch(uint32_t hw_w, uint32_t hw_h)
1938 : hw_w(hw_w), hw_h(hw_h) {
1939 }
1940 void operator()(GLfloat* vtx, float v) {
1941 const GLfloat w = hw_w + (hw_w * v);
1942 const GLfloat h = hw_h - (hw_h * v);
1943 const GLfloat x = (hw_w - w) * 0.5f;
1944 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001945 vtx[0] = x; vtx[1] = y;
1946 vtx[2] = x; vtx[3] = y + h;
1947 vtx[4] = x + w; vtx[5] = y + h;
1948 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001949 }
1950 };
1951
1952 class h_stretch {
1953 const GLfloat hw_w, hw_h;
1954 public:
1955 h_stretch(uint32_t hw_w, uint32_t hw_h)
1956 : hw_w(hw_w), hw_h(hw_h) {
1957 }
1958 void operator()(GLfloat* vtx, float v) {
1959 const GLfloat w = hw_w - (hw_w * v);
1960 const GLfloat h = 1.0f;
1961 const GLfloat x = (hw_w - w) * 0.5f;
1962 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001963 vtx[0] = x; vtx[1] = y;
1964 vtx[2] = x; vtx[3] = y + h;
1965 vtx[4] = x + w; vtx[5] = y + h;
1966 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001967 }
1968 };
1969
Mathias Agopianed9807b2012-05-18 14:18:08 -07001970 VSyncWaiter vsync(mEventThread);
1971
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001972 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001973 char value[PROPERTY_VALUE_MAX];
1974 property_get("debug.sf.electron_frames", value, "24");
1975 int nbFrames = (atoi(value) + 1) >> 1;
1976 if (nbFrames <= 0) // just in case
1977 nbFrames = 24;
1978
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001979 s_curve_interpolator itr(nbFrames, 7.5f);
1980 s_curve_interpolator itg(nbFrames, 8.0f);
1981 s_curve_interpolator itb(nbFrames, 8.5f);
1982
1983 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001984
1985 glMatrixMode(GL_TEXTURE);
1986 glLoadIdentity();
1987 glMatrixMode(GL_MODELVIEW);
1988 glLoadIdentity();
1989
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 glEnable(GL_BLEND);
1991 glBlendFunc(GL_ONE, GL_ONE);
1992 for (int i=0 ; i<nbFrames ; i++) {
1993 float x, y, w, h;
1994 const float vr = itr(i);
1995 const float vg = itg(i);
1996 const float vb = itb(i);
1997
Mathias Agopianed9807b2012-05-18 14:18:08 -07001998 // wait for vsync
1999 vsync.wait();
2000
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002001 // clear screen
2002 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07002003 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07002004 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07002005
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002006 // draw the red plane
2007 vverts(vtx, vr);
2008 glColorMask(1,0,0,1);
2009 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002010
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002011 // draw the green plane
2012 vverts(vtx, vg);
2013 glColorMask(0,1,0,1);
2014 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002015
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002016 // draw the blue plane
2017 vverts(vtx, vb);
2018 glColorMask(0,0,1,1);
2019 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07002020
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002021 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07002022 glDisable(GL_TEXTURE_2D);
2023 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002024 glColor4f(vg, vg, vg, 1);
2025 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2026 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07002027 }
2028
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002029 h_stretch hverts(hw_w, hw_h);
2030 glDisable(GL_BLEND);
2031 glDisable(GL_TEXTURE_2D);
2032 glColorMask(1,1,1,1);
2033 for (int i=0 ; i<nbFrames ; i++) {
2034 const float v = itg(i);
2035 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002036
2037 // wait for vsync
2038 vsync.wait();
2039
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002040 glClear(GL_COLOR_BUFFER_BIT);
2041 glColor4f(1-v, 1-v, 1-v, 1);
2042 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2043 hw.flip(screenBounds);
2044 }
2045
2046 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002047 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2048 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002049 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002050 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002051 return NO_ERROR;
2052}
2053
2054status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2055{
2056 status_t result = PERMISSION_DENIED;
2057
2058 if (!GLExtensions::getInstance().haveFramebufferObject())
2059 return INVALID_OPERATION;
2060
2061
2062 // get screen geometry
2063 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2064 const uint32_t hw_w = hw.getWidth();
2065 const uint32_t hw_h = hw.getHeight();
2066 const Region screenBounds(hw.bounds());
2067
2068 GLfloat u, v;
2069 GLuint tname;
2070 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2071 if (result != NO_ERROR) {
2072 return result;
2073 }
2074
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002075 GLfloat vtx[8];
2076 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002077 glBindTexture(GL_TEXTURE_2D, tname);
2078 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2079 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2080 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002081 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2082 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002083 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2084 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2085 glVertexPointer(2, GL_FLOAT, 0, vtx);
2086
2087 class s_curve_interpolator {
2088 const float nbFrames, s, v;
2089 public:
2090 s_curve_interpolator(int nbFrames, float s)
2091 : nbFrames(1.0f / (nbFrames-1)), s(s),
2092 v(1.0f + expf(-s + 0.5f*s)) {
2093 }
2094 float operator()(int f) {
2095 const float x = f * nbFrames;
2096 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2097 }
2098 };
2099
2100 class v_stretch {
2101 const GLfloat hw_w, hw_h;
2102 public:
2103 v_stretch(uint32_t hw_w, uint32_t hw_h)
2104 : hw_w(hw_w), hw_h(hw_h) {
2105 }
2106 void operator()(GLfloat* vtx, float v) {
2107 const GLfloat w = hw_w + (hw_w * v);
2108 const GLfloat h = hw_h - (hw_h * v);
2109 const GLfloat x = (hw_w - w) * 0.5f;
2110 const GLfloat y = (hw_h - h) * 0.5f;
2111 vtx[0] = x; vtx[1] = y;
2112 vtx[2] = x; vtx[3] = y + h;
2113 vtx[4] = x + w; vtx[5] = y + h;
2114 vtx[6] = x + w; vtx[7] = y;
2115 }
2116 };
2117
2118 class h_stretch {
2119 const GLfloat hw_w, hw_h;
2120 public:
2121 h_stretch(uint32_t hw_w, uint32_t hw_h)
2122 : hw_w(hw_w), hw_h(hw_h) {
2123 }
2124 void operator()(GLfloat* vtx, float v) {
2125 const GLfloat w = hw_w - (hw_w * v);
2126 const GLfloat h = 1.0f;
2127 const GLfloat x = (hw_w - w) * 0.5f;
2128 const GLfloat y = (hw_h - h) * 0.5f;
2129 vtx[0] = x; vtx[1] = y;
2130 vtx[2] = x; vtx[3] = y + h;
2131 vtx[4] = x + w; vtx[5] = y + h;
2132 vtx[6] = x + w; vtx[7] = y;
2133 }
2134 };
2135
Mathias Agopianed9807b2012-05-18 14:18:08 -07002136 VSyncWaiter vsync(mEventThread);
2137
Mathias Agopiana6546e52010-10-14 12:33:07 -07002138 // the full animation is 12 frames
2139 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002140 s_curve_interpolator itr(nbFrames, 7.5f);
2141 s_curve_interpolator itg(nbFrames, 8.0f);
2142 s_curve_interpolator itb(nbFrames, 8.5f);
2143
2144 h_stretch hverts(hw_w, hw_h);
2145 glDisable(GL_BLEND);
2146 glDisable(GL_TEXTURE_2D);
2147 glColorMask(1,1,1,1);
2148 for (int i=nbFrames-1 ; i>=0 ; i--) {
2149 const float v = itg(i);
2150 hverts(vtx, v);
Mathias Agopianed9807b2012-05-18 14:18:08 -07002151
2152 // wait for vsync
2153 vsync.wait();
2154
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002155 glClear(GL_COLOR_BUFFER_BIT);
2156 glColor4f(1-v, 1-v, 1-v, 1);
2157 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2158 hw.flip(screenBounds);
2159 }
2160
Mathias Agopiana6546e52010-10-14 12:33:07 -07002161 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002162 v_stretch vverts(hw_w, hw_h);
2163 glEnable(GL_BLEND);
2164 glBlendFunc(GL_ONE, GL_ONE);
2165 for (int i=nbFrames-1 ; i>=0 ; i--) {
2166 float x, y, w, h;
2167 const float vr = itr(i);
2168 const float vg = itg(i);
2169 const float vb = itb(i);
2170
Mathias Agopianed9807b2012-05-18 14:18:08 -07002171 // wait for vsync
2172 vsync.wait();
2173
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002174 // clear screen
2175 glColorMask(1,1,1,1);
2176 glClear(GL_COLOR_BUFFER_BIT);
2177 glEnable(GL_TEXTURE_2D);
2178
2179 // draw the red plane
2180 vverts(vtx, vr);
2181 glColorMask(1,0,0,1);
2182 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2183
2184 // draw the green plane
2185 vverts(vtx, vg);
2186 glColorMask(0,1,0,1);
2187 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2188
2189 // draw the blue plane
2190 vverts(vtx, vb);
2191 glColorMask(0,0,1,1);
2192 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2193
2194 hw.flip(screenBounds);
2195 }
2196
2197 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002198 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002199 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002200 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002201 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002202
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203 return NO_ERROR;
2204}
2205
2206// ---------------------------------------------------------------------------
2207
Mathias Agopianabd671a2010-10-14 14:54:06 -07002208status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002209{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002210 ATRACE_CALL();
2211
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2213 if (!hw.canDraw()) {
2214 // we're already off
2215 return NO_ERROR;
2216 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002217
2218 // turn off hwc while we're doing the animation
2219 hw.getHwComposer().disable();
2220 // and make sure to turn it back on (if needed) next time we compose
2221 invalidateHwcGeometry();
2222
Mathias Agopianabd671a2010-10-14 14:54:06 -07002223 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2224 electronBeamOffAnimationImplLocked();
2225 }
2226
2227 // always clear the whole screen at the end of the animation
2228 glClearColor(0,0,0,1);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002229 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002230 hw.flip( Region(hw.bounds()) );
2231
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002232 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002233}
2234
2235status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2236{
Mathias Agopian59119e62010-10-11 12:37:43 -07002237 class MessageTurnElectronBeamOff : public MessageBase {
2238 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002239 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002240 status_t result;
2241 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002242 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2243 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002244 }
2245 status_t getResult() const {
2246 return result;
2247 }
2248 virtual bool handler() {
2249 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002250 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002251 return true;
2252 }
2253 };
2254
Mathias Agopianabd671a2010-10-14 14:54:06 -07002255 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002256 status_t res = postMessageSync(msg);
2257 if (res == NO_ERROR) {
2258 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259
2260 // work-around: when the power-manager calls us we activate the
2261 // animation. eventually, the "on" animation will be called
2262 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002263 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002264 }
2265 return res;
2266}
2267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002268// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002269
Mathias Agopianabd671a2010-10-14 14:54:06 -07002270status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002271{
2272 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2273 if (hw.canDraw()) {
2274 // we're already on
2275 return NO_ERROR;
2276 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002277 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2278 electronBeamOnAnimationImplLocked();
2279 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002280
2281 // make sure to redraw the whole screen when the animation is done
2282 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002283 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002284
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002285 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002286}
2287
2288status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2289{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002290 class MessageTurnElectronBeamOn : public MessageBase {
2291 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002292 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002293 status_t result;
2294 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002295 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2296 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002297 }
2298 status_t getResult() const {
2299 return result;
2300 }
2301 virtual bool handler() {
2302 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002303 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002304 return true;
2305 }
2306 };
2307
Mathias Agopianabd671a2010-10-14 14:54:06 -07002308 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002309 return NO_ERROR;
2310}
2311
2312// ---------------------------------------------------------------------------
2313
Mathias Agopian74c40c02010-09-29 13:02:36 -07002314status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2315 sp<IMemoryHeap>* heap,
2316 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002317 uint32_t sw, uint32_t sh,
2318 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002320 ATRACE_CALL();
2321
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 status_t result = PERMISSION_DENIED;
2323
2324 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002325 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002326 return BAD_VALUE;
2327
2328 if (!GLExtensions::getInstance().haveFramebufferObject())
2329 return INVALID_OPERATION;
2330
2331 // get screen geometry
2332 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2333 const uint32_t hw_w = hw.getWidth();
2334 const uint32_t hw_h = hw.getHeight();
2335
2336 if ((sw > hw_w) || (sh > hw_h))
2337 return BAD_VALUE;
2338
2339 sw = (!sw) ? hw_w : sw;
2340 sh = (!sh) ? hw_h : sh;
2341 const size_t size = sw * sh * 4;
2342
Steve Block9d453682011-12-20 16:23:08 +00002343 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002344 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002345
Mathias Agopian74c40c02010-09-29 13:02:36 -07002346 // make sure to clear all GL error flags
2347 while ( glGetError() != GL_NO_ERROR ) ;
2348
2349 // create a FBO
2350 GLuint name, tname;
2351 glGenRenderbuffersOES(1, &tname);
2352 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2353 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002354
Mathias Agopian74c40c02010-09-29 13:02:36 -07002355 glGenFramebuffersOES(1, &name);
2356 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2357 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2358 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2359
2360 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002361
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2363
2364 // invert everything, b/c glReadPixel() below will invert the FB
2365 glViewport(0, 0, sw, sh);
2366 glMatrixMode(GL_PROJECTION);
2367 glPushMatrix();
2368 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002369 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002370 glMatrixMode(GL_MODELVIEW);
2371
2372 // redraw the screen entirely...
2373 glClearColor(0,0,0,1);
2374 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002375
Jamie Gennis9575f602011-10-07 14:51:16 -07002376 const LayerVector& layers(mDrawingState.layersSortedByZ);
2377 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002378 for (size_t i=0 ; i<count ; ++i) {
2379 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002380 const uint32_t flags = layer->drawingState().flags;
2381 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2382 const uint32_t z = layer->drawingState().z;
2383 if (z >= minLayerZ && z <= maxLayerZ) {
2384 layer->drawForSreenShot();
2385 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002386 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 }
2388
Mathias Agopian74c40c02010-09-29 13:02:36 -07002389 // check for errors and return screen capture
2390 if (glGetError() != GL_NO_ERROR) {
2391 // error while rendering
2392 result = INVALID_OPERATION;
2393 } else {
2394 // allocate shared memory large enough to hold the
2395 // screen capture
2396 sp<MemoryHeapBase> base(
2397 new MemoryHeapBase(size, 0, "screen-capture") );
2398 void* const ptr = base->getBase();
2399 if (ptr) {
2400 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002401 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002402 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2403 if (glGetError() == GL_NO_ERROR) {
2404 *heap = base;
2405 *w = sw;
2406 *h = sh;
2407 *f = PIXEL_FORMAT_RGBA_8888;
2408 result = NO_ERROR;
2409 }
2410 } else {
2411 result = NO_MEMORY;
2412 }
2413 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002414 glViewport(0, 0, hw_w, hw_h);
2415 glMatrixMode(GL_PROJECTION);
2416 glPopMatrix();
2417 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002418 } else {
2419 result = BAD_VALUE;
2420 }
2421
2422 // release FBO resources
2423 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2424 glDeleteRenderbuffersOES(1, &tname);
2425 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002426
2427 hw.compositionComplete();
2428
Steve Block9d453682011-12-20 16:23:08 +00002429 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002430
Mathias Agopian74c40c02010-09-29 13:02:36 -07002431 return result;
2432}
2433
2434
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002435status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2436 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002437 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002438 uint32_t sw, uint32_t sh,
2439 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002440{
2441 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002442 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002443 return BAD_VALUE;
2444
2445 if (!GLExtensions::getInstance().haveFramebufferObject())
2446 return INVALID_OPERATION;
2447
2448 class MessageCaptureScreen : public MessageBase {
2449 SurfaceFlinger* flinger;
2450 DisplayID dpy;
2451 sp<IMemoryHeap>* heap;
2452 uint32_t* w;
2453 uint32_t* h;
2454 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002455 uint32_t sw;
2456 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002457 uint32_t minLayerZ;
2458 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002459 status_t result;
2460 public:
2461 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002462 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002463 uint32_t sw, uint32_t sh,
2464 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002465 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002466 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2467 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2468 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469 {
2470 }
2471 status_t getResult() const {
2472 return result;
2473 }
2474 virtual bool handler() {
2475 Mutex::Autolock _l(flinger->mStateLock);
2476
2477 // if we have secure windows, never allow the screen capture
2478 if (flinger->mSecureFrameBuffer)
2479 return true;
2480
Mathias Agopian74c40c02010-09-29 13:02:36 -07002481 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002482 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002484 return true;
2485 }
2486 };
2487
2488 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002489 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002490 status_t res = postMessageSync(msg);
2491 if (res == NO_ERROR) {
2492 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2493 }
2494 return res;
2495}
2496
2497// ---------------------------------------------------------------------------
2498
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002499sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2500{
2501 sp<Layer> result;
2502 Mutex::Autolock _l(mStateLock);
2503 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2504 return result;
2505}
2506
2507// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508
Mathias Agopian96f08192010-06-02 23:28:45 -07002509Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002510 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512}
2513
Mathias Agopian96f08192010-06-02 23:28:45 -07002514Client::~Client()
2515{
Mathias Agopian96f08192010-06-02 23:28:45 -07002516 const size_t count = mLayers.size();
2517 for (size_t i=0 ; i<count ; i++) {
2518 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2519 if (layer != 0) {
2520 mFlinger->removeLayer(layer);
2521 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 }
2523}
2524
Mathias Agopian96f08192010-06-02 23:28:45 -07002525status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002526 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002528
Mathias Agopian4f113742011-05-03 16:21:41 -07002529size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002530{
Mathias Agopian4f113742011-05-03 16:21:41 -07002531 Mutex::Autolock _l(mLock);
2532 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002533 mLayers.add(name, layer);
2534 return name;
2535}
2536
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002537void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002538{
Mathias Agopian4f113742011-05-03 16:21:41 -07002539 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002540 // we do a linear search here, because this doesn't happen often
2541 const size_t count = mLayers.size();
2542 for (size_t i=0 ; i<count ; i++) {
2543 if (mLayers.valueAt(i) == layer) {
2544 mLayers.removeItemsAt(i, 1);
2545 break;
2546 }
2547 }
2548}
Mathias Agopian4f113742011-05-03 16:21:41 -07002549sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2550{
2551 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002552 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002553 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002554 if (layer != 0) {
2555 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002556 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002557 }
2558 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559}
2560
Mathias Agopiana67932f2011-04-20 14:20:59 -07002561
2562status_t Client::onTransact(
2563 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2564{
2565 // these must be checked
2566 IPCThreadState* ipc = IPCThreadState::self();
2567 const int pid = ipc->getCallingPid();
2568 const int uid = ipc->getCallingUid();
2569 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002570 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002571 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002572 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002573 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002574 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002575 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2576 return PERMISSION_DENIED;
2577 }
2578 }
2579 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002580}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002581
2582
Mathias Agopian96f08192010-06-02 23:28:45 -07002583sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002584 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002585 const String8& name,
2586 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002587 uint32_t flags)
2588{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002589 /*
2590 * createSurface must be called from the GL thread so that it can
2591 * have access to the GL context.
2592 */
2593
2594 class MessageCreateSurface : public MessageBase {
2595 sp<ISurface> result;
2596 SurfaceFlinger* flinger;
2597 ISurfaceComposerClient::surface_data_t* params;
2598 Client* client;
2599 const String8& name;
2600 DisplayID display;
2601 uint32_t w, h;
2602 PixelFormat format;
2603 uint32_t flags;
2604 public:
2605 MessageCreateSurface(SurfaceFlinger* flinger,
2606 ISurfaceComposerClient::surface_data_t* params,
2607 const String8& name, Client* client,
2608 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2609 uint32_t flags)
2610 : flinger(flinger), params(params), client(client), name(name),
2611 display(display), w(w), h(h), format(format), flags(flags)
2612 {
2613 }
2614 sp<ISurface> getResult() const { return result; }
2615 virtual bool handler() {
2616 result = flinger->createSurface(params, name, client,
2617 display, w, h, format, flags);
2618 return true;
2619 }
2620 };
2621
2622 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2623 params, name, this, display, w, h, format, flags);
2624 mFlinger->postMessageSync(msg);
2625 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626}
Mathias Agopian96f08192010-06-02 23:28:45 -07002627status_t Client::destroySurface(SurfaceID sid) {
2628 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630
2631// ---------------------------------------------------------------------------
2632
Jamie Gennis9a78c902011-01-12 18:30:40 -08002633GraphicBufferAlloc::GraphicBufferAlloc() {}
2634
2635GraphicBufferAlloc::~GraphicBufferAlloc() {}
2636
2637sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002638 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002639 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2640 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002641 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002642 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002643 if (err == NO_MEMORY) {
2644 GraphicBuffer::dumpAllocationsToSystemLog();
2645 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002646 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002647 "failed (%s), handle=%p",
2648 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002649 return 0;
2650 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002651 return graphicBuffer;
2652}
2653
Jamie Gennis9a78c902011-01-12 18:30:40 -08002654// ---------------------------------------------------------------------------
2655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656GraphicPlane::GraphicPlane()
2657 : mHw(0)
2658{
2659}
2660
2661GraphicPlane::~GraphicPlane() {
2662 delete mHw;
2663}
2664
2665bool GraphicPlane::initialized() const {
2666 return mHw ? true : false;
2667}
2668
Mathias Agopian2b92d892010-02-08 15:49:35 -08002669int GraphicPlane::getWidth() const {
2670 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671}
2672
Mathias Agopian2b92d892010-02-08 15:49:35 -08002673int GraphicPlane::getHeight() const {
2674 return mHeight;
2675}
2676
2677void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2678{
2679 mHw = hw;
2680
2681 // initialize the display orientation transform.
2682 // it's a constant that should come from the display driver.
2683 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2684 char property[PROPERTY_VALUE_MAX];
2685 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2686 //displayOrientation
2687 switch (atoi(property)) {
2688 case 90:
2689 displayOrientation = ISurfaceComposer::eOrientation90;
2690 break;
2691 case 270:
2692 displayOrientation = ISurfaceComposer::eOrientation270;
2693 break;
2694 }
2695 }
2696
2697 const float w = hw->getWidth();
2698 const float h = hw->getHeight();
2699 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2700 &mDisplayTransform);
2701 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2702 mDisplayWidth = h;
2703 mDisplayHeight = w;
2704 } else {
2705 mDisplayWidth = w;
2706 mDisplayHeight = h;
2707 }
2708
2709 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710}
2711
2712status_t GraphicPlane::orientationToTransfrom(
2713 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002714{
2715 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716 switch (orientation) {
2717 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002718 flags = Transform::ROT_0;
2719 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002720 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002721 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002722 break;
2723 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002724 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 break;
2726 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002727 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002728 break;
2729 default:
2730 return BAD_VALUE;
2731 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002732 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733 return NO_ERROR;
2734}
2735
2736status_t GraphicPlane::setOrientation(int orientation)
2737{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002738 // If the rotation can be handled in hardware, this is where
2739 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002740
2741 const DisplayHardware& hw(displayHardware());
2742 const float w = mDisplayWidth;
2743 const float h = mDisplayHeight;
2744 mWidth = int(w);
2745 mHeight = int(h);
2746
2747 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002748 GraphicPlane::orientationToTransfrom(orientation, w, h,
2749 &orientationTransform);
2750 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2751 mWidth = int(h);
2752 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002753 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002754
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002755 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002756 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757 return NO_ERROR;
2758}
2759
2760const DisplayHardware& GraphicPlane::displayHardware() const {
2761 return *mHw;
2762}
2763
Mathias Agopian59119e62010-10-11 12:37:43 -07002764DisplayHardware& GraphicPlane::editDisplayHardware() {
2765 return *mHw;
2766}
2767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002768const Transform& GraphicPlane::transform() const {
2769 return mGlobalTransform;
2770}
2771
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002772EGLDisplay GraphicPlane::getEGLDisplay() const {
2773 return mHw->getEGLDisplay();
2774}
2775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002776// ---------------------------------------------------------------------------
2777
2778}; // namespace android