blob: 35a7fd51b0d4ea6b7f7f2ce93bb71dba679ba392 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080026#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/IDisplayEventConnection.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include <utils/String8.h>
42#include <utils/String16.h>
43#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3330b202009-10-05 17:07:12 -070046#include <ui/GraphicBufferAllocator.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <GLES/gl.h>
50
51#include "clz.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080052#include "DdmConnection.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080053#include "EventThread.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070054#include "GLExtensions.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070057#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
60#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070061#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Glenn Kasten1db13d72011-12-19 13:55:34 -080063#include <private/android_filesystem_config.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include <private/gui/SharedBufferStack.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070065
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080066#define EGL_VERSION_HW_ANDROID 0x3143
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070091 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070092 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070093 mDebugInSwapBuffers(0),
94 mLastSwapBufferTime(0),
95 mDebugInTransaction(0),
96 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070097 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mSecureFrameBuffer(0)
99{
100 init();
101}
102
103void SurfaceFlinger::init()
104{
Steve Blocka19954a2012-01-04 20:05:49 +0000105 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
107 // debugging stuff...
108 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 property_get("debug.sf.showupdates", value, "0");
111 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700112
Colin Cross3854ed52012-03-23 14:17:18 -0700113#ifdef DDMS_DEBUGGING
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114 property_get("debug.sf.ddms", value, "0");
115 mDebugDDMS = atoi(value);
116 if (mDebugDDMS) {
117 DdmConnection::start(getServiceName());
118 }
Colin Cross3854ed52012-03-23 14:17:18 -0700119#endif
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
Steve Blocka19954a2012-01-04 20:05:49 +0000121 ALOGI_IF(mDebugRegion, "showupdates enabled");
Steve Blocka19954a2012-01-04 20:05:49 +0000122 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800125void SurfaceFlinger::onFirstRef()
126{
127 mEventQueue.init(this);
128
129 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
130
131 // Wait for the main thread to be done with its initialization
132 mReadyToRunBarrier.wait();
133}
134
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136SurfaceFlinger::~SurfaceFlinger()
137{
138 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141void SurfaceFlinger::binderDied(const wp<IBinder>& who)
142{
143 // the window manager died on us. prepare its eulogy.
144
145 // reset screen orientation
146 Vector<ComposerState> state;
147 setTransactionState(state, eOrientationDefault, 0);
148
149 // restart the boot-animation
150 property_set("ctl.start", "bootanim");
151}
152
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700153sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700155 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian7e27f052010-05-28 14:22:23 -0700158sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Mathias Agopian96f08192010-06-02 23:28:45 -0700160 sp<ISurfaceComposerClient> bclient;
161 sp<Client> client(new Client(this));
162 status_t err = client->initCheck();
163 if (err == NO_ERROR) {
164 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 return bclient;
167}
168
Jamie Gennis9a78c902011-01-12 18:30:40 -0800169sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
170{
171 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
172 return gba;
173}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
176{
Steve Blocke6f43dd2012-01-06 19:20:56 +0000177 ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 const GraphicPlane& plane(mGraphicPlanes[dpy]);
179 return plane;
180}
181
182GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
183{
184 return const_cast<GraphicPlane&>(
185 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
186}
187
188void SurfaceFlinger::bootFinished()
189{
190 const nsecs_t now = systemTime();
191 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000192 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700193 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700194
195 // wait patiently for the window manager death
196 const String16 name("window");
197 sp<IBinder> window(defaultServiceManager()->getService(name));
198 if (window != 0) {
199 window->linkToDeath(this);
200 }
201
202 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700203 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206static inline uint16_t pack565(int r, int g, int b) {
207 return (r<<11)|(g<<5)|b;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210status_t SurfaceFlinger::readyToRun()
211{
Steve Blocka19954a2012-01-04 20:05:49 +0000212 ALOGI( "SurfaceFlinger's main thread ready to run. "
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 "Initializing graphics H/W...");
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // we only support one display currently
216 int dpy = 0;
217
218 {
219 // initialize the main display
220 GraphicPlane& plane(graphicPlane(dpy));
221 DisplayHardware* const hw = new DisplayHardware(this, dpy);
222 plane.setDisplayHardware(hw);
223 }
224
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 // create the shared control-block
226 mServerHeap = new MemoryHeapBase(4096,
227 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
Steve Blocke6f43dd2012-01-06 19:20:56 +0000228 ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 new(mServerCblk) surface_flinger_cblk_t;
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 // initialize primary screen
236 // (other display should be initialized in the same manner, but
237 // asynchronously, as they could come and go. None of this is supported
238 // yet).
239 const GraphicPlane& plane(graphicPlane(dpy));
240 const DisplayHardware& hw = plane.displayHardware();
241 const uint32_t w = hw.getWidth();
242 const uint32_t h = hw.getHeight();
243 const uint32_t f = hw.getFormat();
244 hw.makeCurrent();
245
246 // initialize the shared control block
247 mServerCblk->connected |= 1<<dpy;
248 display_cblk_t* dcblk = mServerCblk->displays + dpy;
249 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800250 dcblk->w = plane.getWidth();
251 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 dcblk->format = f;
253 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
254 dcblk->xdpi = hw.getDpiX();
255 dcblk->ydpi = hw.getDpiY();
256 dcblk->fps = hw.getRefreshRate();
257 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700261 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glEnableClientState(GL_VERTEX_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 glShadeModel(GL_FLAT);
264 glDisable(GL_DITHER);
265 glDisable(GL_CULL_FACE);
266
267 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
268 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700269 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 glGenTextures(1, &mWormholeTexName);
271 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
272 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
276 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700277 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
278
279 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
280 glGenTextures(1, &mProtectedTexName);
281 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
287 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
289 glViewport(0, 0, w, h);
290 glMatrixMode(GL_PROJECTION);
291 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700292 // put the origin in the left-bottom corner
293 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800295
296 // start the EventThread
297 mEventThread = new EventThread(this);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800298 mEventQueue.setEventThread(mEventThread);
Mathias Agopianf6de1c02012-02-05 02:15:28 -0800299 hw.startSleepManagement();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
301 /*
302 * We're now ready to accept clients...
303 */
304
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800305 mReadyToRunBarrier.open();
306
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700307 // start boot animation
308 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return NO_ERROR;
311}
312
313// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800314
Jamie Gennis582270d2011-08-17 18:19:00 -0700315bool SurfaceFlinger::authenticateSurfaceTexture(
316 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800317 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700318 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800319
320 // Check the visible layer list for the ISurface
321 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
322 size_t count = currentLayers.size();
323 for (size_t i=0 ; i<count ; i++) {
324 const sp<LayerBase>& layer(currentLayers[i]);
325 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700326 if (lbc != NULL) {
327 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
328 if (lbcBinder == surfaceTextureBinder) {
329 return true;
330 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800331 }
332 }
333
334 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700335 // SurfaceTexture gets destroyed before all the clients are done using it,
336 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800337 // will instead fail later on when the client tries to use the surface,
338 // which should be reported as "surface XYZ returned an -ENODEV". The
339 // purgatorized layers are no less authentic than the visible ones, so this
340 // should not cause any harm.
341 size_t purgatorySize = mLayerPurgatory.size();
342 for (size_t i=0 ; i<purgatorySize ; i++) {
343 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
344 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700345 if (lbc != NULL) {
346 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
347 if (lbcBinder == surfaceTextureBinder) {
348 return true;
349 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800350 }
351 }
352
353 return false;
354}
355
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800356// ----------------------------------------------------------------------------
357
358sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800359 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700360}
361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800363
364void SurfaceFlinger::waitForEvent() {
365 mEventQueue.waitMessage();
366}
367
368void SurfaceFlinger::signalTransaction() {
369 mEventQueue.invalidate();
370}
371
372void SurfaceFlinger::signalLayerUpdate() {
373 mEventQueue.invalidate();
374}
375
376void SurfaceFlinger::signalRefresh() {
377 mEventQueue.refresh();
378}
379
380status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
381 nsecs_t reltime, uint32_t flags) {
382 return mEventQueue.postMessage(msg, reltime);
383}
384
385status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
386 nsecs_t reltime, uint32_t flags) {
387 status_t res = mEventQueue.postMessage(msg, reltime);
388 if (res == NO_ERROR) {
389 msg->wait();
390 }
391 return res;
392}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393
394bool SurfaceFlinger::threadLoop()
395{
396 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 return true;
398}
399
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800400void SurfaceFlinger::onMessageReceived(int32_t what)
401{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800402 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800403 switch (what) {
Mathias Agopian69a655c2012-04-11 20:43:19 -0700404 case MessageQueue::REFRESH: {
405// case MessageQueue::INVALIDATE: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800406 // if we're in a global transaction, don't do anything.
407 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
408 uint32_t transactionFlags = peekTransactionFlags(mask);
409 if (CC_UNLIKELY(transactionFlags)) {
410 handleTransaction(transactionFlags);
411 }
412
413 // post surfaces (if needed)
414 handlePageFlip();
415
Mathias Agopian69a655c2012-04-11 20:43:19 -0700416// signalRefresh();
417//
418// } break;
419//
420// case MessageQueue::REFRESH: {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800421
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800422 handleRefresh();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800423
Mathias Agopian303d5382012-02-05 01:49:16 -0800424 const DisplayHardware& hw(graphicPlane(0).displayHardware());
425
Mathias Agopian69a655c2012-04-11 20:43:19 -0700426// if (mDirtyRegion.isEmpty()) {
427// return;
428// }
429
Mathias Agopianb048cef2012-02-04 15:44:04 -0800430 if (CC_UNLIKELY(mHwWorkListDirty)) {
431 // build the h/w work list
432 handleWorkList();
433 }
Mathias Agopian303d5382012-02-05 01:49:16 -0800434
Mathias Agopianb048cef2012-02-04 15:44:04 -0800435 if (CC_LIKELY(hw.canDraw())) {
436 // repaint the framebuffer (if needed)
437 handleRepaint();
438 // inform the h/w that we're done compositing
439 hw.compositionComplete();
440 postFramebuffer();
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800441 } else {
Mathias Agopianb048cef2012-02-04 15:44:04 -0800442 // pretend we did the post
Mathias Agopianc9ca7012012-02-03 17:22:09 -0800443 hw.compositionComplete();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800444 }
Mathias Agopianb048cef2012-02-04 15:44:04 -0800445
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800446 } break;
447 }
448}
449
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450void SurfaceFlinger::postFramebuffer()
451{
Mathias Agopian841cde52012-03-01 15:44:37 -0800452 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800453 // mSwapRegion can be empty here is some cases, for instance if a hidden
454 // or fully transparent window is updating.
455 // in that case, we need to flip anyways to not risk a deadlock with
456 // h/w composer.
457
Mathias Agopiana44b0412011-10-16 18:46:35 -0700458 const DisplayHardware& hw(graphicPlane(0).displayHardware());
459 const nsecs_t now = systemTime();
460 mDebugInSwapBuffers = now;
461 hw.flip(mSwapRegion);
Jamie Gennise8696a42012-01-15 18:54:57 -0800462
463 size_t numLayers = mVisibleLayersSortedByZ.size();
464 for (size_t i = 0; i < numLayers; i++) {
465 mVisibleLayersSortedByZ[i]->onLayerDisplayed();
466 }
467
Mathias Agopiana44b0412011-10-16 18:46:35 -0700468 mLastSwapBufferTime = systemTime() - now;
469 mDebugInSwapBuffers = 0;
470 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
474{
Mathias Agopian841cde52012-03-01 15:44:37 -0800475 ATRACE_CALL();
476
Mathias Agopianca4d3602011-05-19 15:38:14 -0700477 Mutex::Autolock _l(mStateLock);
478 const nsecs_t now = systemTime();
479 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
Mathias Agopianca4d3602011-05-19 15:38:14 -0700481 // Here we're guaranteed that some transaction flags are set
482 // so we can call handleTransactionLocked() unconditionally.
483 // We call getTransactionFlags(), which will also clear the flags,
484 // with mStateLock held to guarantee that mCurrentState won't change
485 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700486
Mathias Agopianca4d3602011-05-19 15:38:14 -0700487 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
488 transactionFlags = getTransactionFlags(mask);
489 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700490
Mathias Agopianca4d3602011-05-19 15:38:14 -0700491 mLastTransactionTime = systemTime() - now;
492 mDebugInTransaction = 0;
493 invalidateHwcGeometry();
494 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700495}
496
Mathias Agopianca4d3602011-05-19 15:38:14 -0700497void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700498{
499 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 const size_t count = currentLayers.size();
501
502 /*
503 * Traversal of the children
504 * (perform the transaction for each of them if needed)
505 */
506
507 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
508 if (layersNeedTransaction) {
509 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
512 if (!trFlags) continue;
513
514 const uint32_t flags = layer->doTransaction(0);
515 if (flags & Layer::eVisibleRegion)
516 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 }
518 }
519
520 /*
521 * Perform our own transaction if needed
522 */
523
524 if (transactionFlags & eTransactionNeeded) {
525 if (mCurrentState.orientation != mDrawingState.orientation) {
526 // the orientation has changed, recompute all visible regions
527 // and invalidate everything.
528
529 const int dpy = 0;
530 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700531 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532 GraphicPlane& plane(graphicPlane(dpy));
533 plane.setOrientation(orientation);
534
535 // update the shared control block
536 const DisplayHardware& hw(plane.displayHardware());
537 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
538 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800539 dcblk->w = plane.getWidth();
540 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541
542 mVisibleRegionsDirty = true;
543 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 }
545
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700546 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
547 // layers have been added
548 mVisibleRegionsDirty = true;
549 }
550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 // some layers might have been removed, so
552 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700553 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700554 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700556 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700557 const size_t count = previousLayers.size();
558 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700559 const sp<LayerBase>& layer(previousLayers[i]);
560 if (currentLayers.indexOf( layer ) < 0) {
561 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700562 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700563 }
564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
567
568 commitTransaction();
569}
570
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800572 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573{
Mathias Agopian841cde52012-03-01 15:44:37 -0800574 ATRACE_CALL();
575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 const GraphicPlane& plane(graphicPlane(0));
577 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700578 const DisplayHardware& hw(plane.displayHardware());
579 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580
581 Region aboveOpaqueLayers;
582 Region aboveCoveredLayers;
583 Region dirty;
584
585 bool secureFrameBuffer = false;
586
587 size_t i = currentLayers.size();
588 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 layer->validateVisibility(planeTransform);
591
592 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700593 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594
Mathias Agopianab028732010-03-16 16:41:46 -0700595 /*
596 * opaqueRegion: area of a surface that is fully opaque.
597 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700599
600 /*
601 * visibleRegion: area of a surface that is visible on screen
602 * and not fully transparent. This is essentially the layer's
603 * footprint minus the opaque regions above it.
604 * Areas covered by a translucent surface are considered visible.
605 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700607
608 /*
609 * coveredRegion: area of a surface that is covered by all
610 * visible regions above it (which includes the translucent areas).
611 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700613
614
615 // handle hidden surfaces by setting the visible region to empty
Glenn Kasten99ed2242011-12-15 09:51:17 -0800616 if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700617 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700618 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700620 visibleRegion.andSelf(screenRegion);
621 if (!visibleRegion.isEmpty()) {
622 // Remove the transparent area from the visible region
623 if (translucent) {
624 visibleRegion.subtractSelf(layer->transparentRegionScreen);
625 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopianab028732010-03-16 16:41:46 -0700627 // compute the opaque region
628 const int32_t layerOrientation = layer->getOrientation();
629 if (s.alpha==255 && !translucent &&
630 ((layerOrientation & Transform::ROT_INVALID) == false)) {
631 // the opaque region is the layer's footprint
632 opaqueRegion = visibleRegion;
633 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 }
635 }
636
Mathias Agopianab028732010-03-16 16:41:46 -0700637 // Clip the covered region to the visible region
638 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
639
640 // Update aboveCoveredLayers for next (lower) layer
641 aboveCoveredLayers.orSelf(visibleRegion);
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 // subtract the opaque region covered by the layers above us
644 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645
646 // compute this layer's dirty region
647 if (layer->contentDirty) {
648 // we need to invalidate the whole region
649 dirty = visibleRegion;
650 // as well, as the old visible region
651 dirty.orSelf(layer->visibleRegionScreen);
652 layer->contentDirty = false;
653 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700654 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700655 * the exposed region consists of two components:
656 * 1) what's VISIBLE now and was COVERED before
657 * 2) what's EXPOSED now less what was EXPOSED before
658 *
659 * note that (1) is conservative, we start with the whole
660 * visible region but only keep what used to be covered by
661 * something -- which mean it may have been exposed.
662 *
663 * (2) handles areas that were not covered by anything but got
664 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700665 */
Mathias Agopianab028732010-03-16 16:41:46 -0700666 const Region newExposed = visibleRegion - coveredRegion;
667 const Region oldVisibleRegion = layer->visibleRegionScreen;
668 const Region oldCoveredRegion = layer->coveredRegionScreen;
669 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
670 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 }
672 dirty.subtractSelf(aboveOpaqueLayers);
673
674 // accumulate to the screen dirty region
675 dirtyRegion.orSelf(dirty);
676
Mathias Agopianab028732010-03-16 16:41:46 -0700677 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700679
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 // Store the visible region is screen space
681 layer->setVisibleRegion(visibleRegion);
682 layer->setCoveredRegion(coveredRegion);
683
Mathias Agopian97011222009-07-28 10:57:27 -0700684 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 if (layer->isSecure() && !visibleRegion.isEmpty()) {
686 secureFrameBuffer = true;
687 }
688 }
689
Mathias Agopian97011222009-07-28 10:57:27 -0700690 // invalidate the areas where a layer was removed
691 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
692 mDirtyRegionRemovedLayer.clear();
693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 mSecureFrameBuffer = secureFrameBuffer;
695 opaqueRegion = aboveOpaqueLayers;
696}
697
698
699void SurfaceFlinger::commitTransaction()
700{
Jesse Hall2f4b68d2011-12-02 10:00:00 -0800701 if (!mLayersPendingRemoval.isEmpty()) {
702 // Notify removed layers now that they can't be drawn from
703 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
704 mLayersPendingRemoval[i]->onRemoved();
705 }
706 mLayersPendingRemoval.clear();
707 }
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700710 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700711 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712}
713
714void SurfaceFlinger::handlePageFlip()
715{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800716 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800717 const DisplayHardware& hw = graphicPlane(0).displayHardware();
718 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800720 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
721 const bool visibleRegions = lockPageFlip(currentLayers);
722
723 if (visibleRegions || mVisibleRegionsDirty) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 Region opaqueRegion;
725 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700726
727 /*
728 * rebuild the visible layer list
729 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800730 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700731 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.setCapacity(count);
733 for (size_t i=0 ; i<count ; i++) {
734 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
735 mVisibleLayersSortedByZ.add(currentLayers[i]);
736 }
737
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 mWormholeRegion = screenRegion.subtract(opaqueRegion);
739 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800740 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 }
742
743 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700744
745 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746 mDirtyRegion.andSelf(screenRegion);
747}
748
Mathias Agopianad456f92011-01-13 17:53:01 -0800749void SurfaceFlinger::invalidateHwcGeometry()
750{
751 mHwWorkListDirty = true;
752}
753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
755{
756 bool recomputeVisibleRegions = false;
757 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700758 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700760 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 layer->lockPageFlip(recomputeVisibleRegions);
762 }
763 return recomputeVisibleRegions;
764}
765
766void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
767{
768 const GraphicPlane& plane(graphicPlane(0));
769 const Transform& planeTransform(plane.transform());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800770 const size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700771 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700773 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 layer->unlockPageFlip(planeTransform, mDirtyRegion);
775 }
776}
777
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800778void SurfaceFlinger::handleRefresh()
779{
780 bool needInvalidate = false;
781 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
782 const size_t count = currentLayers.size();
783 for (size_t i=0 ; i<count ; i++) {
784 const sp<LayerBase>& layer(currentLayers[i]);
785 if (layer->onPreComposition()) {
786 needInvalidate = true;
787 }
788 }
789 if (needInvalidate) {
790 signalLayerUpdate();
791 }
792}
793
794
Mathias Agopiana350ff92010-08-10 17:14:02 -0700795void SurfaceFlinger::handleWorkList()
796{
797 mHwWorkListDirty = false;
798 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
799 if (hwc.initCheck() == NO_ERROR) {
800 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
801 const size_t count = currentLayers.size();
802 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700803 hwc_layer_t* const cur(hwc.getLayers());
804 for (size_t i=0 ; cur && i<count ; i++) {
805 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700806 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700807 cur[i].compositionType = HWC_FRAMEBUFFER;
808 cur[i].flags |= HWC_SKIP_LAYER;
809 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700810 }
811 }
812}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814void SurfaceFlinger::handleRepaint()
815{
Mathias Agopian841cde52012-03-01 15:44:37 -0800816 ATRACE_CALL();
817
Mathias Agopianb8a55602009-06-26 19:06:36 -0700818 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700819 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820
Glenn Kasten99ed2242011-12-15 09:51:17 -0800821 if (CC_UNLIKELY(mDebugRegion)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 debugFlashRegions();
823 }
824
Mathias Agopianb8a55602009-06-26 19:06:36 -0700825 // set the frame buffer
826 const DisplayHardware& hw(graphicPlane(0).displayHardware());
827 glMatrixMode(GL_MODELVIEW);
828 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829
830 uint32_t flags = hw.getFlags();
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700831 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700832 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
833 // takes a rectangle, we must make sure to update that whole
834 // rectangle in that case
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700835 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700837 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 // We need to redraw the rectangle that will be updated
839 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700840 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700841 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700842 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700843 } else {
844 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700846 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
848 }
849
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700850 setupHardwareComposer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 composeSurfaces(mDirtyRegion);
852
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853 // update the swap region and clear the dirty region
854 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 mDirtyRegion.clear();
856}
857
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700858void SurfaceFlinger::setupHardwareComposer()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700860 const DisplayHardware& hw(graphicPlane(0).displayHardware());
861 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700862 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700863 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700864 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700865 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700866
Mathias Agopianf384cc32011-09-08 18:31:55 -0700867 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
868 size_t count = layers.size();
869
Steve Blocke6f43dd2012-01-06 19:20:56 +0000870 ALOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700871 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
872 hwc.getNumLayers(), count);
873
874 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700875 if (hwc.initCheck() == NO_ERROR) {
876 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
877 }
Mathias Agopian45721772010-08-12 15:03:26 -0700878
879 /*
880 * update the per-frame h/w composer data for each layer
881 * and build the transparent region of the FB
882 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700883 for (size_t i=0 ; i<count ; i++) {
884 const sp<LayerBase>& layer(layers[i]);
885 layer->setPerFrameData(&cur[i]);
886 }
887 status_t err = hwc.prepare();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000888 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopianf384cc32011-09-08 18:31:55 -0700889}
Mathias Agopian45721772010-08-12 15:03:26 -0700890
Mathias Agopianf384cc32011-09-08 18:31:55 -0700891void SurfaceFlinger::composeSurfaces(const Region& dirty)
892{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700893 const DisplayHardware& hw(graphicPlane(0).displayHardware());
894 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700895 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700896
897 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopian56a2bbe2012-04-18 18:33:19 -0700898 if (!cur || fbLayerCount) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700899 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopianf384cc32011-09-08 18:31:55 -0700900
Mathias Agopianb9494d52012-04-18 02:28:45 -0700901 if (hwc.getLayerCount(HWC_OVERLAY)) {
902 // when using overlays, we assume a fully transparent framebuffer
903 // NOTE: we could reduce how much we need to clear, for instance
904 // remove where there are opaque FB layers. however, on some
905 // GPUs doing a "clean slate" glClear might be more efficient.
906 // We'll revisit later if needed.
907 glClearColor(0, 0, 0, 0);
908 glClear(GL_COLOR_BUFFER_BIT);
909 } else {
910 // screen is already cleared here
911 if (!mWormholeRegion.isEmpty()) {
912 // can happen with SurfaceView
913 drawWormhole();
914 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700915 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700916
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700917 /*
918 * and then, render the layers targeted at the framebuffer
919 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700920
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700921 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
922 const size_t count = layers.size();
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700923
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700925 const sp<LayerBase>& layer(layers[i]);
926 const Region clip(dirty.intersect(layer->visibleRegionScreen));
927 if (!clip.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700928 if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
929 if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700930 && layer->isOpaque()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -0700931 // never clear the very first layer since we're
932 // guaranteed the FB is already cleared
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700933 layer->clearWithOpenGL(clip);
934 }
935 continue;
936 }
937 // render the layer
938 layer->draw(clip);
Mathias Agopian4b2ba532012-03-29 12:23:51 -0700939 }
940 }
941 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942}
943
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944void SurfaceFlinger::debugFlashRegions()
945{
Mathias Agopian0a917752010-06-14 21:20:00 -0700946 const DisplayHardware& hw(graphicPlane(0).displayHardware());
947 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700948 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700949 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700950 return;
951 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700952
Mathias Agopiana2f4e562012-04-15 23:34:59 -0700953 if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700954 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
955 mDirtyRegion.bounds() : hw.bounds());
956 composeSurfaces(repaint);
957 }
958
Mathias Agopianc492e672011-10-18 14:49:27 -0700959 glDisable(GL_TEXTURE_EXTERNAL_OES);
960 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 glDisable(GL_SCISSOR_TEST);
963
Mathias Agopian0926f502009-05-04 14:17:04 -0700964 static int toggle = 0;
965 toggle = 1 - toggle;
966 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700967 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700968 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700969 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700970 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971
Mathias Agopian20f68782009-05-11 00:03:41 -0700972 Region::const_iterator it = mDirtyRegion.begin();
973 Region::const_iterator const end = mDirtyRegion.end();
974 while (it != end) {
975 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -0700977 { r.left, height - r.top },
978 { r.left, height - r.bottom },
979 { r.right, height - r.bottom },
980 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 };
982 glVertexPointer(2, GL_FLOAT, 0, vertices);
983 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
984 }
Mathias Agopian0a917752010-06-14 21:20:00 -0700985
Mathias Agopian0656a682011-09-20 17:22:44 -0700986 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987
988 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -0700989 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990}
991
992void SurfaceFlinger::drawWormhole() const
993{
994 const Region region(mWormholeRegion.intersect(mDirtyRegion));
995 if (region.isEmpty())
996 return;
997
Mathias Agopianf74e8e02012-04-16 03:14:05 -0700998 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -0700999 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001000 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001001 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001002
1003 GLfloat vertices[4][2];
1004 glVertexPointer(2, GL_FLOAT, 0, vertices);
1005 Region::const_iterator it = region.begin();
1006 Region::const_iterator const end = region.end();
1007 while (it != end) {
1008 const Rect& r = *it++;
1009 vertices[0][0] = r.left;
1010 vertices[0][1] = r.top;
1011 vertices[1][0] = r.right;
1012 vertices[1][1] = r.top;
1013 vertices[2][0] = r.right;
1014 vertices[2][1] = r.bottom;
1015 vertices[3][0] = r.left;
1016 vertices[3][1] = r.bottom;
1017 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019}
1020
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001021status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022{
1023 Mutex::Autolock _l(mStateLock);
1024 addLayer_l(layer);
1025 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1026 return NO_ERROR;
1027}
1028
Mathias Agopian96f08192010-06-02 23:28:45 -07001029status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1030{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001031 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001032 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1033}
1034
1035ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1036 const sp<LayerBaseClient>& lbc)
1037{
Mathias Agopian96f08192010-06-02 23:28:45 -07001038 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001039 size_t name = client->attachLayer(lbc);
1040
1041 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001042
1043 // add this layer to the current state list
1044 addLayer_l(lbc);
1045
Mathias Agopian4f113742011-05-03 16:21:41 -07001046 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001047}
1048
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001049status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050{
1051 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001052 status_t err = purgatorizeLayer_l(layer);
1053 if (err == NO_ERROR)
1054 setTransactionFlags(eTransactionNeeded);
1055 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001058status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001060 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1061 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001062 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001063 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1065 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001066 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 return NO_ERROR;
1068 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001069 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070}
1071
Mathias Agopian9a112062009-04-17 19:36:26 -07001072status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1073{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001074 // First add the layer to the purgatory list, which makes sure it won't
1075 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001076 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001077 if (err >= 0) {
1078 mLayerPurgatory.add(layerBase);
1079 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001080
Jesse Hall2f4b68d2011-12-02 10:00:00 -08001081 mLayersPendingRemoval.push(layerBase);
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001082
Mathias Agopian3d579642009-06-04 18:46:21 -07001083 // it's possible that we don't find a layer, because it might
1084 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001085 // from the user because there is a race between Client::destroySurface(),
1086 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001087 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1088}
1089
Mathias Agopian96f08192010-06-02 23:28:45 -07001090status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091{
Mathias Agopian96f08192010-06-02 23:28:45 -07001092 layer->forceVisibilityTransaction();
1093 setTransactionFlags(eTraversalNeeded);
1094 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopiandea20b12011-05-03 17:04:02 -07001097uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1098{
1099 return android_atomic_release_load(&mTransactionFlags);
1100}
1101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1103{
1104 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1105}
1106
Mathias Agopianbb641242010-05-18 17:06:55 -07001107uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108{
1109 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1110 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001111 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 }
1113 return old;
1114}
1115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001117void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001118 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001119 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001120
Jamie Gennis28378392011-10-12 17:39:00 -07001121 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001122 if (mCurrentState.orientation != orientation) {
1123 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1124 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001125 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001126 } else if (orientation != eOrientationUnchanged) {
Steve Block32397c12012-01-05 23:22:43 +00001127 ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001128 orientation);
1129 }
1130 }
1131
Mathias Agopian698c0872011-06-28 19:09:31 -07001132 const size_t count = state.size();
1133 for (size_t i=0 ; i<count ; i++) {
1134 const ComposerState& s(state[i]);
1135 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001136 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001137 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001138
Mathias Agopian386aa982011-11-07 21:58:03 -08001139 if (transactionFlags) {
1140 // this triggers the transaction
1141 setTransactionFlags(transactionFlags);
1142
1143 // if this is a synchronous transaction, wait for it to take effect
1144 // before returning.
1145 if (flags & eSynchronous) {
1146 mTransationPending = true;
1147 }
1148 while (mTransationPending) {
1149 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1150 if (CC_UNLIKELY(err != NO_ERROR)) {
1151 // just in case something goes wrong in SF, return to the
1152 // called after a few seconds.
Steve Block32397c12012-01-05 23:22:43 +00001153 ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Mathias Agopian386aa982011-11-07 21:58:03 -08001154 mTransationPending = false;
1155 break;
1156 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001157 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 }
1159}
1160
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001161sp<ISurface> SurfaceFlinger::createSurface(
1162 ISurfaceComposerClient::surface_data_t* params,
1163 const String8& name,
1164 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1166 uint32_t flags)
1167{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001168 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001169 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001170
1171 if (int32_t(w|h) < 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001172 ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001173 int(w), int(h));
1174 return surfaceHandle;
1175 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001176
Mathias Agopianff615cc2012-02-24 14:58:36 -08001177 //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001178 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 switch (flags & eFXSurfaceMask) {
1180 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001181 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1182 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 break;
1184 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001185 // for now we treat Blur as Dim, until we can implement it
1186 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001188 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001190 case eFXSurfaceScreenshot:
1191 layer = createScreenshotSurface(client, d, w, h, flags);
1192 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 }
1194
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001195 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001196 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001197 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001198 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001201 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001202 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001203 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001204 if (normalLayer != 0) {
1205 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001206 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001207 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001208 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001209
Mathias Agopian96f08192010-06-02 23:28:45 -07001210 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 }
1212
1213 return surfaceHandle;
1214}
1215
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001216sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001217 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001218 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001219 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220{
1221 // initialize the surfaces
1222 switch (format) { // TODO: take h/w into account
1223 case PIXEL_FORMAT_TRANSPARENT:
1224 case PIXEL_FORMAT_TRANSLUCENT:
1225 format = PIXEL_FORMAT_RGBA_8888;
1226 break;
1227 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001228#ifdef NO_RGBX_8888
1229 format = PIXEL_FORMAT_RGB_565;
1230#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001231 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001232#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 break;
1234 }
1235
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001236#ifdef NO_RGBX_8888
1237 if (format == PIXEL_FORMAT_RGBX_8888)
1238 format = PIXEL_FORMAT_RGBA_8888;
1239#endif
1240
Mathias Agopian96f08192010-06-02 23:28:45 -07001241 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001242 status_t err = layer->setBuffers(w, h, format, flags);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001243 if (CC_LIKELY(err != NO_ERROR)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001244 ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001245 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 }
1247 return layer;
1248}
1249
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001250sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001251 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001252 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
Mathias Agopian96f08192010-06-02 23:28:45 -07001254 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001255 return layer;
1256}
1257
1258sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1259 const sp<Client>& client, DisplayID display,
1260 uint32_t w, uint32_t h, uint32_t flags)
1261{
1262 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 return layer;
1264}
1265
Mathias Agopian96f08192010-06-02 23:28:45 -07001266status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267{
Mathias Agopian9a112062009-04-17 19:36:26 -07001268 /*
1269 * called by the window manager, when a surface should be marked for
1270 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001271 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001272 * The surface is removed from the current and drawing lists, but placed
1273 * in the purgatory queue, so it's not destroyed right-away (we need
1274 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001275 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001276
Mathias Agopian48d819a2009-09-10 19:41:18 -07001277 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001278 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001279 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Daniel Lamb2675792012-02-23 14:35:13 -08001280
Mathias Agopian48d819a2009-09-10 19:41:18 -07001281 if (layer != 0) {
1282 err = purgatorizeLayer_l(layer);
1283 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001284 setTransactionFlags(eTransactionNeeded);
1285 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001286 }
1287 return err;
1288}
1289
Mathias Agopianca4d3602011-05-19 15:38:14 -07001290status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001291{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001292 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001293 status_t err = NO_ERROR;
1294 sp<LayerBaseClient> l(layer.promote());
1295 if (l != NULL) {
1296 Mutex::Autolock _l(mStateLock);
1297 err = removeLayer_l(l);
1298 if (err == NAME_NOT_FOUND) {
1299 // The surface wasn't in the current list, which means it was
1300 // removed already, which means it is in the purgatory,
1301 // and need to be removed from there.
1302 ssize_t idx = mLayerPurgatory.remove(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001303 ALOGE_IF(idx < 0,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001304 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001305 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001306 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001307 "error removing layer=%p (%s)", l.get(), strerror(-err));
1308 }
1309 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310}
1311
Mathias Agopian698c0872011-06-28 19:09:31 -07001312uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001313 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001314 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001317 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1318 if (layer != 0) {
1319 const uint32_t what = s.what;
1320 if (what & ePositionChanged) {
1321 if (layer->setPosition(s.x, s.y))
1322 flags |= eTraversalNeeded;
1323 }
1324 if (what & eLayerChanged) {
1325 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1326 if (layer->setLayer(s.z)) {
1327 mCurrentState.layersSortedByZ.removeAt(idx);
1328 mCurrentState.layersSortedByZ.add(layer);
1329 // we need traversal (state changed)
1330 // AND transaction (list changed)
1331 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 }
1333 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001334 if (what & eSizeChanged) {
1335 if (layer->setSize(s.w, s.h)) {
1336 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001337 }
1338 }
1339 if (what & eAlphaChanged) {
1340 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1341 flags |= eTraversalNeeded;
1342 }
1343 if (what & eMatrixChanged) {
1344 if (layer->setMatrix(s.matrix))
1345 flags |= eTraversalNeeded;
1346 }
1347 if (what & eTransparentRegionChanged) {
1348 if (layer->setTransparentRegionHint(s.transparentRegion))
1349 flags |= eTraversalNeeded;
1350 }
1351 if (what & eVisibilityChanged) {
1352 if (layer->setFlags(s.flags, s.mask))
1353 flags |= eTraversalNeeded;
1354 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -07001355 if (what & eCropChanged) {
1356 if (layer->setCrop(s.crop))
1357 flags |= eTraversalNeeded;
1358 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001360 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361}
1362
Mathias Agopianb60314a2012-04-10 22:09:54 -07001363// ---------------------------------------------------------------------------
1364
1365void SurfaceFlinger::onScreenAcquired() {
1366 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() {
1377 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1378 if (hw.isScreenAcquired()) {
Mathias Agopian22ffb112012-04-10 21:04:02 -07001379 mEventThread->onScreenReleased();
Mathias Agopianb60314a2012-04-10 22:09:54 -07001380 hw.releaseScreen();
1381 // from this point on, SF will stop drawing
1382 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383}
1384
Mathias Agopianb60314a2012-04-10 22:09:54 -07001385void SurfaceFlinger::screenAcquired() {
1386 class MessageScreenAcquired : public MessageBase {
1387 SurfaceFlinger* flinger;
1388 public:
1389 MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { }
1390 virtual bool handler() {
1391 flinger->onScreenAcquired();
1392 return true;
1393 }
1394 };
1395 sp<MessageBase> msg = new MessageScreenAcquired(this);
1396 postMessageSync(msg);
1397}
1398
1399void SurfaceFlinger::screenReleased() {
1400 class MessageScreenReleased : public MessageBase {
1401 SurfaceFlinger* flinger;
1402 public:
1403 MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { }
1404 virtual bool handler() {
1405 flinger->onScreenReleased();
1406 return true;
1407 }
1408 };
1409 sp<MessageBase> msg = new MessageScreenReleased(this);
1410 postMessageSync(msg);
1411}
1412
1413// ---------------------------------------------------------------------------
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1416{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001417 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 char buffer[SIZE];
1419 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001420
1421 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 snprintf(buffer, SIZE, "Permission Denial: "
1423 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1424 IPCThreadState::self()->getCallingPid(),
1425 IPCThreadState::self()->getCallingUid());
1426 result.append(buffer);
1427 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001428 // Try to get the main lock, but don't insist if we can't
1429 // (this would indicate SF is stuck, but we want to be able to
1430 // print something in dumpsys).
1431 int retry = 3;
1432 while (mStateLock.tryLock()<0 && --retry>=0) {
1433 usleep(1000000);
1434 }
1435 const bool locked(retry >= 0);
1436 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001437 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001438 "SurfaceFlinger appears to be unresponsive, "
1439 "dumping anyways (no locks held)\n");
1440 result.append(buffer);
1441 }
1442
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001443 bool dumpAll = true;
1444 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001445 size_t numArgs = args.size();
1446 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001447 if ((index < numArgs) &&
1448 (args[index] == String16("--list"))) {
1449 index++;
1450 listLayersLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001451 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001452 }
1453
1454 if ((index < numArgs) &&
1455 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001456 index++;
1457 dumpStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001458 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001459 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001460
1461 if ((index < numArgs) &&
1462 (args[index] == String16("--latency-clear"))) {
1463 index++;
1464 clearStatsLocked(args, index, result, buffer, SIZE);
Mathias Agopian35aadd62012-03-08 22:01:51 -08001465 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001468
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001469 if (dumpAll) {
1470 dumpAllLocked(result, buffer, SIZE);
Mathias Agopian48b888a2011-01-19 16:15:53 -08001471 }
1472
Mathias Agopian9795c422009-08-26 16:36:26 -07001473 if (locked) {
1474 mStateLock.unlock();
1475 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 }
1477 write(fd, result.string(), result.size());
1478 return NO_ERROR;
1479}
1480
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001481void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
1482 String8& result, char* buffer, size_t SIZE) const
1483{
1484 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1485 const size_t count = currentLayers.size();
1486 for (size_t i=0 ; i<count ; i++) {
1487 const sp<LayerBase>& layer(currentLayers[i]);
1488 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1489 result.append(buffer);
1490 }
1491}
1492
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001493void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
1494 String8& result, char* buffer, size_t SIZE) const
1495{
1496 String8 name;
1497 if (index < args.size()) {
1498 name = String8(args[index]);
1499 index++;
1500 }
1501
1502 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1503 const size_t count = currentLayers.size();
1504 for (size_t i=0 ; i<count ; i++) {
1505 const sp<LayerBase>& layer(currentLayers[i]);
1506 if (name.isEmpty()) {
1507 snprintf(buffer, SIZE, "%s\n", layer->getName().string());
1508 result.append(buffer);
1509 }
1510 if (name.isEmpty() || (name == layer->getName())) {
1511 layer->dumpStats(result, buffer, SIZE);
1512 }
1513 }
1514}
1515
Mathias Agopian25e66fc2012-01-28 22:31:55 -08001516void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
1517 String8& result, char* buffer, size_t SIZE) const
1518{
1519 String8 name;
1520 if (index < args.size()) {
1521 name = String8(args[index]);
1522 index++;
1523 }
1524
1525 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1526 const size_t count = currentLayers.size();
1527 for (size_t i=0 ; i<count ; i++) {
1528 const sp<LayerBase>& layer(currentLayers[i]);
1529 if (name.isEmpty() || (name == layer->getName())) {
1530 layer->clearStats();
1531 }
1532 }
1533}
1534
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001535void SurfaceFlinger::dumpAllLocked(
1536 String8& result, char* buffer, size_t SIZE) const
1537{
1538 // figure out if we're stuck somewhere
1539 const nsecs_t now = systemTime();
1540 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1541 const nsecs_t inTransaction(mDebugInTransaction);
1542 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1543 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1544
1545 /*
1546 * Dump the visible layer list
1547 */
1548 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1549 const size_t count = currentLayers.size();
1550 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1551 result.append(buffer);
1552 for (size_t i=0 ; i<count ; i++) {
1553 const sp<LayerBase>& layer(currentLayers[i]);
1554 layer->dump(result, buffer, SIZE);
1555 }
1556
1557 /*
1558 * Dump the layers in the purgatory
1559 */
1560
1561 const size_t purgatorySize = mLayerPurgatory.size();
1562 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1563 result.append(buffer);
1564 for (size_t i=0 ; i<purgatorySize ; i++) {
1565 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1566 layer->shortDump(result, buffer, SIZE);
1567 }
1568
1569 /*
1570 * Dump SurfaceFlinger global state
1571 */
1572
1573 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
1574 result.append(buffer);
1575
1576 const GLExtensions& extensions(GLExtensions::getInstance());
1577 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1578 extensions.getVendor(),
1579 extensions.getRenderer(),
1580 extensions.getVersion());
1581 result.append(buffer);
1582
1583 snprintf(buffer, SIZE, "EGL : %s\n",
1584 eglQueryString(graphicPlane(0).getEGLDisplay(),
1585 EGL_VERSION_HW_ANDROID));
1586 result.append(buffer);
1587
1588 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1589 result.append(buffer);
1590
1591 mWormholeRegion.dump(result, "WormholeRegion");
1592 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1593 snprintf(buffer, SIZE,
1594 " orientation=%d, canDraw=%d\n",
1595 mCurrentState.orientation, hw.canDraw());
1596 result.append(buffer);
1597 snprintf(buffer, SIZE,
1598 " last eglSwapBuffers() time: %f us\n"
1599 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001600 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001601 " refresh-rate : %f fps\n"
1602 " x-dpi : %f\n"
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001603 " y-dpi : %f\n"
1604 " density : %f\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001605 mLastSwapBufferTime/1000.0,
1606 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08001607 mTransactionFlags,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001608 hw.getRefreshRate(),
1609 hw.getDpiX(),
Mathias Agopianb5dd9c02012-03-22 12:15:54 -07001610 hw.getDpiY(),
1611 hw.getDensity());
Mathias Agopian82d7ab62012-01-19 18:34:40 -08001612 result.append(buffer);
1613
1614 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1615 inSwapBuffersDuration/1000.0);
1616 result.append(buffer);
1617
1618 snprintf(buffer, SIZE, " transaction time: %f us\n",
1619 inTransactionDuration/1000.0);
1620 result.append(buffer);
1621
1622 /*
1623 * VSYNC state
1624 */
1625 mEventThread->dump(result, buffer, SIZE);
1626
1627 /*
1628 * Dump HWComposer state
1629 */
1630 HWComposer& hwc(hw.getHwComposer());
1631 snprintf(buffer, SIZE, "h/w composer state:\n");
1632 result.append(buffer);
1633 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1634 hwc.initCheck()==NO_ERROR ? "present" : "not present",
1635 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
1636 result.append(buffer);
1637 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
1638
1639 /*
1640 * Dump gralloc state
1641 */
1642 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
1643 alloc.dump(result);
1644 hw.dump(result);
1645}
1646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647status_t SurfaceFlinger::onTransact(
1648 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1649{
1650 switch (code) {
1651 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001652 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001655 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001656 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 {
1658 // codes that require permission check
1659 IPCThreadState* ipc = IPCThreadState::self();
1660 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001661 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001662 if ((uid != AID_GRAPHICS) &&
1663 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001664 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001665 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1666 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001668 break;
1669 }
1670 case CAPTURE_SCREEN:
1671 {
1672 // codes that require permission check
1673 IPCThreadState* ipc = IPCThreadState::self();
1674 const int pid = ipc->getCallingPid();
1675 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001676 if ((uid != AID_GRAPHICS) &&
1677 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001678 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001679 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1680 return PERMISSION_DENIED;
1681 }
1682 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
1684 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001685
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1687 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001688 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08001689 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001690 IPCThreadState* ipc = IPCThreadState::self();
1691 const int pid = ipc->getCallingPid();
1692 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00001693 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07001694 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 return PERMISSION_DENIED;
1696 }
1697 int n;
1698 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001699 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001700 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701 return NO_ERROR;
1702 case 1002: // SHOW_UPDATES
1703 n = data.readInt32();
1704 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001705 invalidateHwcGeometry();
1706 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001709 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001710 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001712 case 1005:{ // force transaction
1713 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1714 return NO_ERROR;
1715 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08001716 case 1006:{ // send empty update
1717 signalRefresh();
1718 return NO_ERROR;
1719 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001720 case 1008: // toggle use of hw composer
1721 n = data.readInt32();
1722 mDebugDisableHWC = n ? 1 : 0;
1723 invalidateHwcGeometry();
1724 repaintEverything();
1725 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001726 case 1009: // toggle use of transform hint
1727 n = data.readInt32();
1728 mDebugDisableTransformHint = n ? 1 : 0;
1729 invalidateHwcGeometry();
1730 repaintEverything();
1731 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001733 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 reply->writeInt32(0);
1735 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001736 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08001737 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 return NO_ERROR;
1739 case 1013: {
1740 Mutex::Autolock _l(mStateLock);
1741 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1742 reply->writeInt32(hw.getPageFlipCount());
1743 }
1744 return NO_ERROR;
1745 }
1746 }
1747 return err;
1748}
1749
Mathias Agopian53331da2011-08-22 21:44:41 -07001750void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001751 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001752 const Rect bounds(hw.getBounds());
1753 setInvalidateRegion(Region(bounds));
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001754 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07001755}
1756
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001757void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1758 Mutex::Autolock _l(mInvalidateLock);
1759 mInvalidateRegion = reg;
1760}
1761
1762Region SurfaceFlinger::getAndClearInvalidateRegion() {
1763 Mutex::Autolock _l(mInvalidateLock);
1764 Region reg(mInvalidateRegion);
1765 mInvalidateRegion.clear();
1766 return reg;
1767}
1768
Mathias Agopian59119e62010-10-11 12:37:43 -07001769// ---------------------------------------------------------------------------
1770
Mathias Agopian118d0242011-10-13 16:02:48 -07001771status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1772 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1773{
1774 Mutex::Autolock _l(mStateLock);
1775 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1776}
1777
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001778status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1779 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001780{
Mathias Agopian22ffb112012-04-10 21:04:02 -07001781 ATRACE_CALL();
1782
Mathias Agopian59119e62010-10-11 12:37:43 -07001783 if (!GLExtensions::getInstance().haveFramebufferObject())
1784 return INVALID_OPERATION;
1785
1786 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001787 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001788 const uint32_t hw_w = hw.getWidth();
1789 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001790 GLfloat u = 1;
1791 GLfloat v = 1;
1792
1793 // make sure to clear all GL error flags
1794 while ( glGetError() != GL_NO_ERROR ) ;
1795
1796 // create a FBO
1797 GLuint name, tname;
1798 glGenTextures(1, &tname);
1799 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001800 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1801 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001802 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1803 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001804 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001805 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001806 GLint tw = (2 << (31 - clz(hw_w)));
1807 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001808 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1809 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001810 u = GLfloat(hw_w) / tw;
1811 v = GLfloat(hw_h) / th;
1812 }
1813 glGenFramebuffersOES(1, &name);
1814 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001815 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1816 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001817
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001819 glDisable(GL_TEXTURE_EXTERNAL_OES);
1820 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001821 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822 glClearColor(0,0,0,1);
1823 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001824 glMatrixMode(GL_MODELVIEW);
1825 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001826 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1827 const size_t count = layers.size();
1828 for (size_t i=0 ; i<count ; ++i) {
1829 const sp<LayerBase>& layer(layers[i]);
1830 layer->drawForSreenShot();
1831 }
1832
Mathias Agopian118d0242011-10-13 16:02:48 -07001833 hw.compositionComplete();
1834
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001835 // back to main framebuffer
1836 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1837 glDisable(GL_SCISSOR_TEST);
1838 glDeleteFramebuffersOES(1, &name);
1839
1840 *textureName = tname;
1841 *uOut = u;
1842 *vOut = v;
1843 return NO_ERROR;
1844}
1845
1846// ---------------------------------------------------------------------------
1847
1848status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1849{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001850 // get screen geometry
1851 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1852 const uint32_t hw_w = hw.getWidth();
1853 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001854 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001855
1856 GLfloat u, v;
1857 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001858 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001859 if (result != NO_ERROR) {
1860 return result;
1861 }
1862
1863 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001864 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001865 glBindTexture(GL_TEXTURE_2D, tname);
1866 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1867 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1868 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08001869 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1870 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001871 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1872 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1873 glVertexPointer(2, GL_FLOAT, 0, vtx);
1874
Mathias Agopianffcf4652011-07-07 17:30:31 -07001875 /*
1876 * Texture coordinate mapping
1877 *
1878 * u
1879 * 1 +----------+---+
1880 * | | | | image is inverted
1881 * | V | | w.r.t. the texture
1882 * 1-v +----------+ | coordinates
1883 * | |
1884 * | |
1885 * | |
1886 * 0 +--------------+
1887 * 0 1
1888 *
1889 */
1890
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001891 class s_curve_interpolator {
1892 const float nbFrames, s, v;
1893 public:
1894 s_curve_interpolator(int nbFrames, float s)
1895 : nbFrames(1.0f / (nbFrames-1)), s(s),
1896 v(1.0f + expf(-s + 0.5f*s)) {
1897 }
1898 float operator()(int f) {
1899 const float x = f * nbFrames;
1900 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1901 }
1902 };
1903
1904 class v_stretch {
1905 const GLfloat hw_w, hw_h;
1906 public:
1907 v_stretch(uint32_t hw_w, uint32_t hw_h)
1908 : hw_w(hw_w), hw_h(hw_h) {
1909 }
1910 void operator()(GLfloat* vtx, float v) {
1911 const GLfloat w = hw_w + (hw_w * v);
1912 const GLfloat h = hw_h - (hw_h * v);
1913 const GLfloat x = (hw_w - w) * 0.5f;
1914 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001915 vtx[0] = x; vtx[1] = y;
1916 vtx[2] = x; vtx[3] = y + h;
1917 vtx[4] = x + w; vtx[5] = y + h;
1918 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001919 }
1920 };
1921
1922 class h_stretch {
1923 const GLfloat hw_w, hw_h;
1924 public:
1925 h_stretch(uint32_t hw_w, uint32_t hw_h)
1926 : hw_w(hw_w), hw_h(hw_h) {
1927 }
1928 void operator()(GLfloat* vtx, float v) {
1929 const GLfloat w = hw_w - (hw_w * v);
1930 const GLfloat h = 1.0f;
1931 const GLfloat x = (hw_w - w) * 0.5f;
1932 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001933 vtx[0] = x; vtx[1] = y;
1934 vtx[2] = x; vtx[3] = y + h;
1935 vtx[4] = x + w; vtx[5] = y + h;
1936 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001937 }
1938 };
1939
1940 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001941 char value[PROPERTY_VALUE_MAX];
1942 property_get("debug.sf.electron_frames", value, "24");
1943 int nbFrames = (atoi(value) + 1) >> 1;
1944 if (nbFrames <= 0) // just in case
1945 nbFrames = 24;
1946
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001947 s_curve_interpolator itr(nbFrames, 7.5f);
1948 s_curve_interpolator itg(nbFrames, 8.0f);
1949 s_curve_interpolator itb(nbFrames, 8.5f);
1950
1951 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001952
1953 glMatrixMode(GL_TEXTURE);
1954 glLoadIdentity();
1955 glMatrixMode(GL_MODELVIEW);
1956 glLoadIdentity();
1957
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001958 glEnable(GL_BLEND);
1959 glBlendFunc(GL_ONE, GL_ONE);
1960 for (int i=0 ; i<nbFrames ; i++) {
1961 float x, y, w, h;
1962 const float vr = itr(i);
1963 const float vg = itg(i);
1964 const float vb = itb(i);
1965
1966 // clear screen
1967 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001968 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001969 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001970
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001971 // draw the red plane
1972 vverts(vtx, vr);
1973 glColorMask(1,0,0,1);
1974 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001975
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001976 // draw the green plane
1977 vverts(vtx, vg);
1978 glColorMask(0,1,0,1);
1979 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001980
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001981 // draw the blue plane
1982 vverts(vtx, vb);
1983 glColorMask(0,0,1,1);
1984 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001985
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001986 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001987 glDisable(GL_TEXTURE_2D);
1988 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001989 glColor4f(vg, vg, vg, 1);
1990 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1991 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001992 }
1993
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001994 h_stretch hverts(hw_w, hw_h);
1995 glDisable(GL_BLEND);
1996 glDisable(GL_TEXTURE_2D);
1997 glColorMask(1,1,1,1);
1998 for (int i=0 ; i<nbFrames ; i++) {
1999 const float v = itg(i);
2000 hverts(vtx, v);
2001 glClear(GL_COLOR_BUFFER_BIT);
2002 glColor4f(1-v, 1-v, 1-v, 1);
2003 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2004 hw.flip(screenBounds);
2005 }
2006
2007 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002008 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2009 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002010 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002011 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002012 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002013 return NO_ERROR;
2014}
2015
2016status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
2017{
2018 status_t result = PERMISSION_DENIED;
2019
2020 if (!GLExtensions::getInstance().haveFramebufferObject())
2021 return INVALID_OPERATION;
2022
2023
2024 // get screen geometry
2025 const DisplayHardware& hw(graphicPlane(0).displayHardware());
2026 const uint32_t hw_w = hw.getWidth();
2027 const uint32_t hw_h = hw.getHeight();
2028 const Region screenBounds(hw.bounds());
2029
2030 GLfloat u, v;
2031 GLuint tname;
2032 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2033 if (result != NO_ERROR) {
2034 return result;
2035 }
2036
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002037 GLfloat vtx[8];
2038 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002039 glBindTexture(GL_TEXTURE_2D, tname);
2040 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2041 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2042 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Michael I. Goldb1d1c6d2012-01-13 00:36:45 -08002043 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2044 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002045 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2046 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2047 glVertexPointer(2, GL_FLOAT, 0, vtx);
2048
2049 class s_curve_interpolator {
2050 const float nbFrames, s, v;
2051 public:
2052 s_curve_interpolator(int nbFrames, float s)
2053 : nbFrames(1.0f / (nbFrames-1)), s(s),
2054 v(1.0f + expf(-s + 0.5f*s)) {
2055 }
2056 float operator()(int f) {
2057 const float x = f * nbFrames;
2058 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2059 }
2060 };
2061
2062 class v_stretch {
2063 const GLfloat hw_w, hw_h;
2064 public:
2065 v_stretch(uint32_t hw_w, uint32_t hw_h)
2066 : hw_w(hw_w), hw_h(hw_h) {
2067 }
2068 void operator()(GLfloat* vtx, float v) {
2069 const GLfloat w = hw_w + (hw_w * v);
2070 const GLfloat h = hw_h - (hw_h * v);
2071 const GLfloat x = (hw_w - w) * 0.5f;
2072 const GLfloat y = (hw_h - h) * 0.5f;
2073 vtx[0] = x; vtx[1] = y;
2074 vtx[2] = x; vtx[3] = y + h;
2075 vtx[4] = x + w; vtx[5] = y + h;
2076 vtx[6] = x + w; vtx[7] = y;
2077 }
2078 };
2079
2080 class h_stretch {
2081 const GLfloat hw_w, hw_h;
2082 public:
2083 h_stretch(uint32_t hw_w, uint32_t hw_h)
2084 : hw_w(hw_w), hw_h(hw_h) {
2085 }
2086 void operator()(GLfloat* vtx, float v) {
2087 const GLfloat w = hw_w - (hw_w * v);
2088 const GLfloat h = 1.0f;
2089 const GLfloat x = (hw_w - w) * 0.5f;
2090 const GLfloat y = (hw_h - h) * 0.5f;
2091 vtx[0] = x; vtx[1] = y;
2092 vtx[2] = x; vtx[3] = y + h;
2093 vtx[4] = x + w; vtx[5] = y + h;
2094 vtx[6] = x + w; vtx[7] = y;
2095 }
2096 };
2097
Mathias Agopiana6546e52010-10-14 12:33:07 -07002098 // the full animation is 12 frames
2099 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002100 s_curve_interpolator itr(nbFrames, 7.5f);
2101 s_curve_interpolator itg(nbFrames, 8.0f);
2102 s_curve_interpolator itb(nbFrames, 8.5f);
2103
2104 h_stretch hverts(hw_w, hw_h);
2105 glDisable(GL_BLEND);
2106 glDisable(GL_TEXTURE_2D);
2107 glColorMask(1,1,1,1);
2108 for (int i=nbFrames-1 ; i>=0 ; i--) {
2109 const float v = itg(i);
2110 hverts(vtx, v);
2111 glClear(GL_COLOR_BUFFER_BIT);
2112 glColor4f(1-v, 1-v, 1-v, 1);
2113 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2114 hw.flip(screenBounds);
2115 }
2116
Mathias Agopiana6546e52010-10-14 12:33:07 -07002117 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002118 v_stretch vverts(hw_w, hw_h);
2119 glEnable(GL_BLEND);
2120 glBlendFunc(GL_ONE, GL_ONE);
2121 for (int i=nbFrames-1 ; i>=0 ; i--) {
2122 float x, y, w, h;
2123 const float vr = itr(i);
2124 const float vg = itg(i);
2125 const float vb = itb(i);
2126
2127 // clear screen
2128 glColorMask(1,1,1,1);
2129 glClear(GL_COLOR_BUFFER_BIT);
2130 glEnable(GL_TEXTURE_2D);
2131
2132 // draw the red plane
2133 vverts(vtx, vr);
2134 glColorMask(1,0,0,1);
2135 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2136
2137 // draw the green plane
2138 vverts(vtx, vg);
2139 glColorMask(0,1,0,1);
2140 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2141
2142 // draw the blue plane
2143 vverts(vtx, vb);
2144 glColorMask(0,0,1,1);
2145 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2146
2147 hw.flip(screenBounds);
2148 }
2149
2150 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002151 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002152 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002153 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002154 glDisable(GL_BLEND);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002155 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59119e62010-10-11 12:37:43 -07002156
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002157 return NO_ERROR;
2158}
2159
2160// ---------------------------------------------------------------------------
2161
Mathias Agopianabd671a2010-10-14 14:54:06 -07002162status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002163{
Mathias Agopian22ffb112012-04-10 21:04:02 -07002164 ATRACE_CALL();
2165
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002166 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2167 if (!hw.canDraw()) {
2168 // we're already off
2169 return NO_ERROR;
2170 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002171
2172 // turn off hwc while we're doing the animation
2173 hw.getHwComposer().disable();
2174 // and make sure to turn it back on (if needed) next time we compose
2175 invalidateHwcGeometry();
2176
Mathias Agopianabd671a2010-10-14 14:54:06 -07002177 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2178 electronBeamOffAnimationImplLocked();
2179 }
2180
2181 // always clear the whole screen at the end of the animation
2182 glClearColor(0,0,0,1);
2183 glDisable(GL_SCISSOR_TEST);
2184 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002185 hw.flip( Region(hw.bounds()) );
2186
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002187 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002188}
2189
2190status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2191{
Mathias Agopian59119e62010-10-11 12:37:43 -07002192 class MessageTurnElectronBeamOff : public MessageBase {
2193 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002194 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002195 status_t result;
2196 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002197 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2198 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002199 }
2200 status_t getResult() const {
2201 return result;
2202 }
2203 virtual bool handler() {
2204 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002205 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002206 return true;
2207 }
2208 };
2209
Mathias Agopianabd671a2010-10-14 14:54:06 -07002210 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002211 status_t res = postMessageSync(msg);
2212 if (res == NO_ERROR) {
2213 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214
2215 // work-around: when the power-manager calls us we activate the
2216 // animation. eventually, the "on" animation will be called
2217 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002218 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002219 }
2220 return res;
2221}
2222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002223// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002224
Mathias Agopianabd671a2010-10-14 14:54:06 -07002225status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226{
2227 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2228 if (hw.canDraw()) {
2229 // we're already on
2230 return NO_ERROR;
2231 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002232 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2233 electronBeamOnAnimationImplLocked();
2234 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002235
2236 // make sure to redraw the whole screen when the animation is done
2237 mDirtyRegion.set(hw.bounds());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002238 signalTransaction();
Mathias Agopiana7f03732010-10-14 12:46:24 -07002239
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002240 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002241}
2242
2243status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2244{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002245 class MessageTurnElectronBeamOn : public MessageBase {
2246 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002247 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002248 status_t result;
2249 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002250 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2251 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002252 }
2253 status_t getResult() const {
2254 return result;
2255 }
2256 virtual bool handler() {
2257 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002258 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002259 return true;
2260 }
2261 };
2262
Mathias Agopianabd671a2010-10-14 14:54:06 -07002263 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002264 return NO_ERROR;
2265}
2266
2267// ---------------------------------------------------------------------------
2268
Mathias Agopian74c40c02010-09-29 13:02:36 -07002269status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2270 sp<IMemoryHeap>* heap,
2271 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002272 uint32_t sw, uint32_t sh,
2273 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002274{
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002275 ATRACE_CALL();
2276
Mathias Agopian74c40c02010-09-29 13:02:36 -07002277 status_t result = PERMISSION_DENIED;
2278
2279 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002280 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian74c40c02010-09-29 13:02:36 -07002281 return BAD_VALUE;
2282
2283 if (!GLExtensions::getInstance().haveFramebufferObject())
2284 return INVALID_OPERATION;
2285
2286 // get screen geometry
2287 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2288 const uint32_t hw_w = hw.getWidth();
2289 const uint32_t hw_h = hw.getHeight();
2290
2291 if ((sw > hw_w) || (sh > hw_h))
2292 return BAD_VALUE;
2293
2294 sw = (!sw) ? hw_w : sw;
2295 sh = (!sh) ? hw_h : sh;
2296 const size_t size = sw * sh * 4;
2297
Steve Block9d453682011-12-20 16:23:08 +00002298 //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
Mathias Agopian1c71a472011-03-02 18:45:50 -08002299 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002300
Mathias Agopian74c40c02010-09-29 13:02:36 -07002301 // make sure to clear all GL error flags
2302 while ( glGetError() != GL_NO_ERROR ) ;
2303
2304 // create a FBO
2305 GLuint name, tname;
2306 glGenRenderbuffersOES(1, &tname);
2307 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2308 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002309
Mathias Agopian74c40c02010-09-29 13:02:36 -07002310 glGenFramebuffersOES(1, &name);
2311 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2312 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2313 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2314
2315 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002316
Mathias Agopian74c40c02010-09-29 13:02:36 -07002317 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2318
2319 // invert everything, b/c glReadPixel() below will invert the FB
2320 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002321 glScissor(0, 0, sw, sh);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002322 glMatrixMode(GL_PROJECTION);
2323 glPushMatrix();
2324 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002325 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002326 glMatrixMode(GL_MODELVIEW);
2327
2328 // redraw the screen entirely...
2329 glClearColor(0,0,0,1);
2330 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002331
Jamie Gennis9575f602011-10-07 14:51:16 -07002332 const LayerVector& layers(mDrawingState.layersSortedByZ);
2333 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002334 for (size_t i=0 ; i<count ; ++i) {
2335 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002336 const uint32_t flags = layer->drawingState().flags;
2337 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2338 const uint32_t z = layer->drawingState().z;
2339 if (z >= minLayerZ && z <= maxLayerZ) {
2340 layer->drawForSreenShot();
2341 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002342 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 }
2344
2345 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002346 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002347 glScissor(0, 0, sw, sh);
2348
2349 // check for errors and return screen capture
2350 if (glGetError() != GL_NO_ERROR) {
2351 // error while rendering
2352 result = INVALID_OPERATION;
2353 } else {
2354 // allocate shared memory large enough to hold the
2355 // screen capture
2356 sp<MemoryHeapBase> base(
2357 new MemoryHeapBase(size, 0, "screen-capture") );
2358 void* const ptr = base->getBase();
2359 if (ptr) {
2360 // capture the screen with glReadPixels()
Mathias Agopianfddc28d2012-03-12 15:18:42 -04002361 ScopedTrace _t(ATRACE_TAG, "glReadPixels");
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2363 if (glGetError() == GL_NO_ERROR) {
2364 *heap = base;
2365 *w = sw;
2366 *h = sh;
2367 *f = PIXEL_FORMAT_RGBA_8888;
2368 result = NO_ERROR;
2369 }
2370 } else {
2371 result = NO_MEMORY;
2372 }
2373 }
Mathias Agopianf74e8e02012-04-16 03:14:05 -07002374 glDisable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 glViewport(0, 0, hw_w, hw_h);
2376 glMatrixMode(GL_PROJECTION);
2377 glPopMatrix();
2378 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002379 } else {
2380 result = BAD_VALUE;
2381 }
2382
2383 // release FBO resources
2384 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2385 glDeleteRenderbuffersOES(1, &tname);
2386 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002387
2388 hw.compositionComplete();
2389
Steve Block9d453682011-12-20 16:23:08 +00002390 // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002391
Mathias Agopian74c40c02010-09-29 13:02:36 -07002392 return result;
2393}
2394
2395
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002396status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2397 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002398 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002399 uint32_t sw, uint32_t sh,
2400 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002401{
2402 // only one display supported for now
Glenn Kasten99ed2242011-12-15 09:51:17 -08002403 if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404 return BAD_VALUE;
2405
2406 if (!GLExtensions::getInstance().haveFramebufferObject())
2407 return INVALID_OPERATION;
2408
2409 class MessageCaptureScreen : public MessageBase {
2410 SurfaceFlinger* flinger;
2411 DisplayID dpy;
2412 sp<IMemoryHeap>* heap;
2413 uint32_t* w;
2414 uint32_t* h;
2415 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002416 uint32_t sw;
2417 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002418 uint32_t minLayerZ;
2419 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002420 status_t result;
2421 public:
2422 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002423 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002424 uint32_t sw, uint32_t sh,
2425 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002426 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002427 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2428 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2429 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002430 {
2431 }
2432 status_t getResult() const {
2433 return result;
2434 }
2435 virtual bool handler() {
2436 Mutex::Autolock _l(flinger->mStateLock);
2437
2438 // if we have secure windows, never allow the screen capture
2439 if (flinger->mSecureFrameBuffer)
2440 return true;
2441
Mathias Agopian74c40c02010-09-29 13:02:36 -07002442 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002443 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002444
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002445 return true;
2446 }
2447 };
2448
2449 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002450 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002451 status_t res = postMessageSync(msg);
2452 if (res == NO_ERROR) {
2453 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2454 }
2455 return res;
2456}
2457
2458// ---------------------------------------------------------------------------
2459
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002460sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2461{
2462 sp<Layer> result;
2463 Mutex::Autolock _l(mStateLock);
2464 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2465 return result;
2466}
2467
2468// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469
Mathias Agopian96f08192010-06-02 23:28:45 -07002470Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002471 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473}
2474
Mathias Agopian96f08192010-06-02 23:28:45 -07002475Client::~Client()
2476{
Mathias Agopian96f08192010-06-02 23:28:45 -07002477 const size_t count = mLayers.size();
2478 for (size_t i=0 ; i<count ; i++) {
2479 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2480 if (layer != 0) {
2481 mFlinger->removeLayer(layer);
2482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 }
2484}
2485
Mathias Agopian96f08192010-06-02 23:28:45 -07002486status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002487 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002489
Mathias Agopian4f113742011-05-03 16:21:41 -07002490size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002491{
Mathias Agopian4f113742011-05-03 16:21:41 -07002492 Mutex::Autolock _l(mLock);
2493 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002494 mLayers.add(name, layer);
2495 return name;
2496}
2497
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002498void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002499{
Mathias Agopian4f113742011-05-03 16:21:41 -07002500 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002501 // we do a linear search here, because this doesn't happen often
2502 const size_t count = mLayers.size();
2503 for (size_t i=0 ; i<count ; i++) {
2504 if (mLayers.valueAt(i) == layer) {
2505 mLayers.removeItemsAt(i, 1);
2506 break;
2507 }
2508 }
2509}
Mathias Agopian4f113742011-05-03 16:21:41 -07002510sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2511{
2512 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002513 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002514 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002515 if (layer != 0) {
2516 lbc = layer.promote();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002517 ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002518 }
2519 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520}
2521
Mathias Agopiana67932f2011-04-20 14:20:59 -07002522
2523status_t Client::onTransact(
2524 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2525{
2526 // these must be checked
2527 IPCThreadState* ipc = IPCThreadState::self();
2528 const int pid = ipc->getCallingPid();
2529 const int uid = ipc->getCallingUid();
2530 const int self_pid = getpid();
Glenn Kasten99ed2242011-12-15 09:51:17 -08002531 if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07002532 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002533 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002534 {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002535 ALOGE("Permission Denial: "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002536 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2537 return PERMISSION_DENIED;
2538 }
2539 }
2540 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002541}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002542
2543
Mathias Agopian96f08192010-06-02 23:28:45 -07002544sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002545 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002546 const String8& name,
2547 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002548 uint32_t flags)
2549{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002550 /*
2551 * createSurface must be called from the GL thread so that it can
2552 * have access to the GL context.
2553 */
2554
2555 class MessageCreateSurface : public MessageBase {
2556 sp<ISurface> result;
2557 SurfaceFlinger* flinger;
2558 ISurfaceComposerClient::surface_data_t* params;
2559 Client* client;
2560 const String8& name;
2561 DisplayID display;
2562 uint32_t w, h;
2563 PixelFormat format;
2564 uint32_t flags;
2565 public:
2566 MessageCreateSurface(SurfaceFlinger* flinger,
2567 ISurfaceComposerClient::surface_data_t* params,
2568 const String8& name, Client* client,
2569 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2570 uint32_t flags)
2571 : flinger(flinger), params(params), client(client), name(name),
2572 display(display), w(w), h(h), format(format), flags(flags)
2573 {
2574 }
2575 sp<ISurface> getResult() const { return result; }
2576 virtual bool handler() {
2577 result = flinger->createSurface(params, name, client,
2578 display, w, h, format, flags);
2579 return true;
2580 }
2581 };
2582
2583 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2584 params, name, this, display, w, h, format, flags);
2585 mFlinger->postMessageSync(msg);
2586 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002587}
Mathias Agopian96f08192010-06-02 23:28:45 -07002588status_t Client::destroySurface(SurfaceID sid) {
2589 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591
2592// ---------------------------------------------------------------------------
2593
Jamie Gennis9a78c902011-01-12 18:30:40 -08002594GraphicBufferAlloc::GraphicBufferAlloc() {}
2595
2596GraphicBufferAlloc::~GraphicBufferAlloc() {}
2597
2598sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002599 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002600 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2601 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002602 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002603 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002604 if (err == NO_MEMORY) {
2605 GraphicBuffer::dumpAllocationsToSystemLog();
2606 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00002607 ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
Mathias Agopiana67932f2011-04-20 14:20:59 -07002608 "failed (%s), handle=%p",
2609 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002610 return 0;
2611 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002612 return graphicBuffer;
2613}
2614
Jamie Gennis9a78c902011-01-12 18:30:40 -08002615// ---------------------------------------------------------------------------
2616
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002617GraphicPlane::GraphicPlane()
2618 : mHw(0)
2619{
2620}
2621
2622GraphicPlane::~GraphicPlane() {
2623 delete mHw;
2624}
2625
2626bool GraphicPlane::initialized() const {
2627 return mHw ? true : false;
2628}
2629
Mathias Agopian2b92d892010-02-08 15:49:35 -08002630int GraphicPlane::getWidth() const {
2631 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632}
2633
Mathias Agopian2b92d892010-02-08 15:49:35 -08002634int GraphicPlane::getHeight() const {
2635 return mHeight;
2636}
2637
2638void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2639{
2640 mHw = hw;
2641
2642 // initialize the display orientation transform.
2643 // it's a constant that should come from the display driver.
2644 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2645 char property[PROPERTY_VALUE_MAX];
2646 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2647 //displayOrientation
2648 switch (atoi(property)) {
2649 case 90:
2650 displayOrientation = ISurfaceComposer::eOrientation90;
2651 break;
2652 case 270:
2653 displayOrientation = ISurfaceComposer::eOrientation270;
2654 break;
2655 }
2656 }
2657
2658 const float w = hw->getWidth();
2659 const float h = hw->getHeight();
2660 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2661 &mDisplayTransform);
2662 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2663 mDisplayWidth = h;
2664 mDisplayHeight = w;
2665 } else {
2666 mDisplayWidth = w;
2667 mDisplayHeight = h;
2668 }
2669
2670 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002671}
2672
2673status_t GraphicPlane::orientationToTransfrom(
2674 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002675{
2676 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 switch (orientation) {
2678 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002679 flags = Transform::ROT_0;
2680 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002681 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002682 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002683 break;
2684 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002685 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686 break;
2687 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002688 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689 break;
2690 default:
2691 return BAD_VALUE;
2692 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002693 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694 return NO_ERROR;
2695}
2696
2697status_t GraphicPlane::setOrientation(int orientation)
2698{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002699 // If the rotation can be handled in hardware, this is where
2700 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002701
2702 const DisplayHardware& hw(displayHardware());
2703 const float w = mDisplayWidth;
2704 const float h = mDisplayHeight;
2705 mWidth = int(w);
2706 mHeight = int(h);
2707
2708 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002709 GraphicPlane::orientationToTransfrom(orientation, w, h,
2710 &orientationTransform);
2711 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2712 mWidth = int(h);
2713 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002714 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002715
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002716 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002717 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002718 return NO_ERROR;
2719}
2720
2721const DisplayHardware& GraphicPlane::displayHardware() const {
2722 return *mHw;
2723}
2724
Mathias Agopian59119e62010-10-11 12:37:43 -07002725DisplayHardware& GraphicPlane::editDisplayHardware() {
2726 return *mHw;
2727}
2728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002729const Transform& GraphicPlane::transform() const {
2730 return mGlobalTransform;
2731}
2732
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002733EGLDisplay GraphicPlane::getEGLDisplay() const {
2734 return mHw->getEGLDisplay();
2735}
2736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002737// ---------------------------------------------------------------------------
2738
2739}; // namespace android