blob: a6d4147c1952d1bc6e149eb29cf7693fea63b2f6 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070053#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070057#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopiana67932f2011-04-20 14:20:59 -070059#include <private/surfaceflinger/SharedBufferStack.h>
60
Mathias Agopiana1ecca92009-05-21 19:21:59 -070061/* ideally AID_GRAPHICS would be in a semi-public header
62 * or there would be a way to map a user/group name to its id
63 */
64#ifndef AID_GRAPHICS
65#define AID_GRAPHICS 1003
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mConsoleSignals(0),
100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
107 LOGI("SurfaceFlinger is starting");
108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 property_get("debug.sf.showbackground", value, "0");
116 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
123
Mathias Agopian78fd5012010-04-20 14:51:04 -0700124 LOGI_IF(mDebugRegion, "showupdates enabled");
125 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
129SurfaceFlinger::~SurfaceFlinger()
130{
131 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132}
133
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700134sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700136 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian7e27f052010-05-28 14:22:23 -0700139sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140{
Mathias Agopian96f08192010-06-02 23:28:45 -0700141 sp<ISurfaceComposerClient> bclient;
142 sp<Client> client(new Client(this));
143 status_t err = client->initCheck();
144 if (err == NO_ERROR) {
145 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 return bclient;
148}
149
Jamie Gennis9a78c902011-01-12 18:30:40 -0800150sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
151{
152 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
153 return gba;
154}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
157{
158 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
159 const GraphicPlane& plane(mGraphicPlanes[dpy]);
160 return plane;
161}
162
163GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
164{
165 return const_cast<GraphicPlane&>(
166 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
167}
168
169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700173 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700175
176 // wait patiently for the window manager death
177 const String16 name("window");
178 sp<IBinder> window(defaultServiceManager()->getService(name));
179 if (window != 0) {
180 window->linkToDeath(this);
181 }
182
183 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700184 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700187void SurfaceFlinger::binderDied(const wp<IBinder>& who)
188{
189 // the window manager died on us. prepare its eulogy.
190
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700191 // reset screen orientation
192 setOrientation(0, eOrientationDefault, 0);
193
194 // restart the boot-animation
195 property_set("ctl.start", "bootanim");
196}
197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198void SurfaceFlinger::onFirstRef()
199{
200 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
201
202 // Wait for the main thread to be done with its initialization
203 mReadyToRunBarrier.wait();
204}
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{
212 LOGI( "SurfaceFlinger's main thread ready to run. "
213 "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");
228 LOGE_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());
231 LOGE_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);
263 glEnable(GL_SCISSOR_TEST);
264 glShadeModel(GL_FLAT);
265 glDisable(GL_DITHER);
266 glDisable(GL_CULL_FACE);
267
268 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
269 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glGenTextures(1, &mWormholeTexName);
272 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
279
280 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
281 glGenTextures(1, &mProtectedTexName);
282 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
288 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290 glViewport(0, 0, w, h);
291 glMatrixMode(GL_PROJECTION);
292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700293 // put the origin in the left-bottom corner
294 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 mReadyToRunBarrier.open();
297
298 /*
299 * We're now ready to accept clients...
300 */
301
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700302 // start boot animation
303 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 return NO_ERROR;
306}
307
308// ----------------------------------------------------------------------------
309#if 0
310#pragma mark -
311#pragma mark Events Handler
312#endif
313
314void SurfaceFlinger::waitForEvent()
315{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700316 while (true) {
317 nsecs_t timeout = -1;
Mathias Agopianbb641242010-05-18 17:06:55 -0700318 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700319 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700320 switch (msg->what) {
321 case MessageQueue::INVALIDATE:
322 // invalidate message, just return to the main loop
323 return;
324 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 }
327}
328
329void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700330 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331}
332
Jamie Gennis582270d2011-08-17 18:19:00 -0700333bool SurfaceFlinger::authenticateSurfaceTexture(
334 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800335 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700336 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800337
338 // Check the visible layer list for the ISurface
339 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
340 size_t count = currentLayers.size();
341 for (size_t i=0 ; i<count ; i++) {
342 const sp<LayerBase>& layer(currentLayers[i]);
343 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700344 if (lbc != NULL) {
345 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
346 if (lbcBinder == surfaceTextureBinder) {
347 return true;
348 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800349 }
350 }
351
352 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700353 // SurfaceTexture gets destroyed before all the clients are done using it,
354 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800355 // will instead fail later on when the client tries to use the surface,
356 // which should be reported as "surface XYZ returned an -ENODEV". The
357 // purgatorized layers are no less authentic than the visible ones, so this
358 // should not cause any harm.
359 size_t purgatorySize = mLayerPurgatory.size();
360 for (size_t i=0 ; i<purgatorySize ; i++) {
361 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
362 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700363 if (lbc != NULL) {
364 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
365 if (lbcBinder == surfaceTextureBinder) {
366 return true;
367 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800368 }
369 }
370
371 return false;
372}
373
Mathias Agopianbb641242010-05-18 17:06:55 -0700374status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
375 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376{
Mathias Agopianbb641242010-05-18 17:06:55 -0700377 return mEventQueue.postMessage(msg, reltime, flags);
378}
379
380status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
381 nsecs_t reltime, uint32_t flags)
382{
383 status_t res = mEventQueue.postMessage(msg, reltime, flags);
384 if (res == NO_ERROR) {
385 msg->wait();
386 }
387 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388}
389
390// ----------------------------------------------------------------------------
391#if 0
392#pragma mark -
393#pragma mark Main loop
394#endif
395
396bool SurfaceFlinger::threadLoop()
397{
398 waitForEvent();
399
400 // check for transactions
401 if (UNLIKELY(mConsoleSignals)) {
402 handleConsoleEvents();
403 }
404
Mathias Agopian698c0872011-06-28 19:09:31 -0700405 // if we're in a global transaction, don't do anything.
406 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
407 uint32_t transactionFlags = peekTransactionFlags(mask);
408 if (UNLIKELY(transactionFlags)) {
409 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 }
411
412 // post surfaces (if needed)
413 handlePageFlip();
414
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700415 if (mDirtyRegion.isEmpty()) {
416 // nothing new to do.
417 return true;
418 }
419
Mathias Agopiana350ff92010-08-10 17:14:02 -0700420 if (UNLIKELY(mHwWorkListDirty)) {
421 // build the h/w work list
422 handleWorkList();
423 }
424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700426 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700428
429 const int index = hw.getCurrentBufferIndex();
430 GraphicLog& logger(GraphicLog::getInstance());
431
432 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 handleRepaint();
434
Mathias Agopian74faca22009-09-17 16:18:16 -0700435 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700436 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700437 hw.compositionComplete();
438
Mathias Agopian35b48d12010-09-13 22:57:58 -0700439 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700440 postFramebuffer();
441
Mathias Agopian35b48d12010-09-13 22:57:58 -0700442 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 } else {
444 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800445 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 usleep(16667); // 60 fps period
447 }
448 return true;
449}
450
451void SurfaceFlinger::postFramebuffer()
452{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700453 // this should never happen. we do the flip anyways so we don't
454 // risk to cause a deadlock with hwc
455 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700456 const DisplayHardware& hw(graphicPlane(0).displayHardware());
457 const nsecs_t now = systemTime();
458 mDebugInSwapBuffers = now;
459 hw.flip(mSwapRegion);
460 mLastSwapBufferTime = systemTime() - now;
461 mDebugInSwapBuffers = 0;
462 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463}
464
465void SurfaceFlinger::handleConsoleEvents()
466{
467 // something to do with the console
468 const DisplayHardware& hw = graphicPlane(0).displayHardware();
469
470 int what = android_atomic_and(0, &mConsoleSignals);
471 if (what & eConsoleAcquired) {
472 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700473 // this is a temporary work-around, eventually this should be called
474 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700475 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 }
477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700479 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 }
482 }
483
484 mDirtyRegion.set(hw.bounds());
485}
486
487void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
488{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700489 Mutex::Autolock _l(mStateLock);
490 const nsecs_t now = systemTime();
491 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492
Mathias Agopianca4d3602011-05-19 15:38:14 -0700493 // Here we're guaranteed that some transaction flags are set
494 // so we can call handleTransactionLocked() unconditionally.
495 // We call getTransactionFlags(), which will also clear the flags,
496 // with mStateLock held to guarantee that mCurrentState won't change
497 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700498
Mathias Agopianca4d3602011-05-19 15:38:14 -0700499 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
500 transactionFlags = getTransactionFlags(mask);
501 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700502
Mathias Agopianca4d3602011-05-19 15:38:14 -0700503 mLastTransactionTime = systemTime() - now;
504 mDebugInTransaction = 0;
505 invalidateHwcGeometry();
506 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700507}
508
Mathias Agopianca4d3602011-05-19 15:38:14 -0700509void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700510{
511 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 const size_t count = currentLayers.size();
513
514 /*
515 * Traversal of the children
516 * (perform the transaction for each of them if needed)
517 */
518
519 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
520 if (layersNeedTransaction) {
521 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700522 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
524 if (!trFlags) continue;
525
526 const uint32_t flags = layer->doTransaction(0);
527 if (flags & Layer::eVisibleRegion)
528 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 }
530 }
531
532 /*
533 * Perform our own transaction if needed
534 */
535
536 if (transactionFlags & eTransactionNeeded) {
537 if (mCurrentState.orientation != mDrawingState.orientation) {
538 // the orientation has changed, recompute all visible regions
539 // and invalidate everything.
540
541 const int dpy = 0;
542 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700543 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 GraphicPlane& plane(graphicPlane(dpy));
545 plane.setOrientation(orientation);
546
547 // update the shared control block
548 const DisplayHardware& hw(plane.displayHardware());
549 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
550 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800551 dcblk->w = plane.getWidth();
552 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800553
554 mVisibleRegionsDirty = true;
555 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
557
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700558 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
559 // layers have been added
560 mVisibleRegionsDirty = true;
561 }
562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 // some layers might have been removed, so
564 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700565 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700566 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700568 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700569 const size_t count = previousLayers.size();
570 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700571 const sp<LayerBase>& layer(previousLayers[i]);
572 if (currentLayers.indexOf( layer ) < 0) {
573 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700574 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700575 }
576 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
579
580 commitTransaction();
581}
582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800584 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585{
586 const GraphicPlane& plane(graphicPlane(0));
587 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700588 const DisplayHardware& hw(plane.displayHardware());
589 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590
591 Region aboveOpaqueLayers;
592 Region aboveCoveredLayers;
593 Region dirty;
594
595 bool secureFrameBuffer = false;
596
597 size_t i = currentLayers.size();
598 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 layer->validateVisibility(planeTransform);
601
602 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700603 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604
Mathias Agopianab028732010-03-16 16:41:46 -0700605 /*
606 * opaqueRegion: area of a surface that is fully opaque.
607 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700609
610 /*
611 * visibleRegion: area of a surface that is visible on screen
612 * and not fully transparent. This is essentially the layer's
613 * footprint minus the opaque regions above it.
614 * Areas covered by a translucent surface are considered visible.
615 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700617
618 /*
619 * coveredRegion: area of a surface that is covered by all
620 * visible regions above it (which includes the translucent areas).
621 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700623
624
625 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700626 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700627 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700628 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700630 visibleRegion.andSelf(screenRegion);
631 if (!visibleRegion.isEmpty()) {
632 // Remove the transparent area from the visible region
633 if (translucent) {
634 visibleRegion.subtractSelf(layer->transparentRegionScreen);
635 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636
Mathias Agopianab028732010-03-16 16:41:46 -0700637 // compute the opaque region
638 const int32_t layerOrientation = layer->getOrientation();
639 if (s.alpha==255 && !translucent &&
640 ((layerOrientation & Transform::ROT_INVALID) == false)) {
641 // the opaque region is the layer's footprint
642 opaqueRegion = visibleRegion;
643 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644 }
645 }
646
Mathias Agopianab028732010-03-16 16:41:46 -0700647 // Clip the covered region to the visible region
648 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
649
650 // Update aboveCoveredLayers for next (lower) layer
651 aboveCoveredLayers.orSelf(visibleRegion);
652
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 // subtract the opaque region covered by the layers above us
654 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
656 // compute this layer's dirty region
657 if (layer->contentDirty) {
658 // we need to invalidate the whole region
659 dirty = visibleRegion;
660 // as well, as the old visible region
661 dirty.orSelf(layer->visibleRegionScreen);
662 layer->contentDirty = false;
663 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700664 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700665 * the exposed region consists of two components:
666 * 1) what's VISIBLE now and was COVERED before
667 * 2) what's EXPOSED now less what was EXPOSED before
668 *
669 * note that (1) is conservative, we start with the whole
670 * visible region but only keep what used to be covered by
671 * something -- which mean it may have been exposed.
672 *
673 * (2) handles areas that were not covered by anything but got
674 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700675 */
Mathias Agopianab028732010-03-16 16:41:46 -0700676 const Region newExposed = visibleRegion - coveredRegion;
677 const Region oldVisibleRegion = layer->visibleRegionScreen;
678 const Region oldCoveredRegion = layer->coveredRegionScreen;
679 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
680 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 }
682 dirty.subtractSelf(aboveOpaqueLayers);
683
684 // accumulate to the screen dirty region
685 dirtyRegion.orSelf(dirty);
686
Mathias Agopianab028732010-03-16 16:41:46 -0700687 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 // Store the visible region is screen space
691 layer->setVisibleRegion(visibleRegion);
692 layer->setCoveredRegion(coveredRegion);
693
Mathias Agopian97011222009-07-28 10:57:27 -0700694 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695 if (layer->isSecure() && !visibleRegion.isEmpty()) {
696 secureFrameBuffer = true;
697 }
698 }
699
Mathias Agopian97011222009-07-28 10:57:27 -0700700 // invalidate the areas where a layer was removed
701 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
702 mDirtyRegionRemovedLayer.clear();
703
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 mSecureFrameBuffer = secureFrameBuffer;
705 opaqueRegion = aboveOpaqueLayers;
706}
707
708
709void SurfaceFlinger::commitTransaction()
710{
711 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700712 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700713 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714}
715
716void SurfaceFlinger::handlePageFlip()
717{
718 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800719 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 visibleRegions |= lockPageFlip(currentLayers);
721
722 const DisplayHardware& hw = graphicPlane(0).displayHardware();
723 const Region screenRegion(hw.bounds());
724 if (visibleRegions) {
725 Region opaqueRegion;
726 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700727
728 /*
729 * rebuild the visible layer list
730 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800731 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700732 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700733 mVisibleLayersSortedByZ.setCapacity(count);
734 for (size_t i=0 ; i<count ; i++) {
735 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
736 mVisibleLayersSortedByZ.add(currentLayers[i]);
737 }
738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 mWormholeRegion = screenRegion.subtract(opaqueRegion);
740 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800741 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
743
744 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700745
746 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 mDirtyRegion.andSelf(screenRegion);
748}
749
Mathias Agopianad456f92011-01-13 17:53:01 -0800750void SurfaceFlinger::invalidateHwcGeometry()
751{
752 mHwWorkListDirty = true;
753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
756{
757 bool recomputeVisibleRegions = false;
758 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700759 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700761 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 layer->lockPageFlip(recomputeVisibleRegions);
763 }
764 return recomputeVisibleRegions;
765}
766
767void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
768{
769 const GraphicPlane& plane(graphicPlane(0));
770 const Transform& planeTransform(plane.transform());
771 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700772 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700774 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 layer->unlockPageFlip(planeTransform, mDirtyRegion);
776 }
777}
778
Mathias Agopiana350ff92010-08-10 17:14:02 -0700779void SurfaceFlinger::handleWorkList()
780{
781 mHwWorkListDirty = false;
782 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
783 if (hwc.initCheck() == NO_ERROR) {
784 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
785 const size_t count = currentLayers.size();
786 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700787 hwc_layer_t* const cur(hwc.getLayers());
788 for (size_t i=0 ; cur && i<count ; i++) {
789 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700790 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700791 cur[i].compositionType = HWC_FRAMEBUFFER;
792 cur[i].flags |= HWC_SKIP_LAYER;
793 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700794 }
795 }
796}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700797
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798void SurfaceFlinger::handleRepaint()
799{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700800 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700801 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802
803 if (UNLIKELY(mDebugRegion)) {
804 debugFlashRegions();
805 }
806
Mathias Agopianb8a55602009-06-26 19:06:36 -0700807 // set the frame buffer
808 const DisplayHardware& hw(graphicPlane(0).displayHardware());
809 glMatrixMode(GL_MODELVIEW);
810 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811
812 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700813 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
814 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700815 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700816 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
817 // takes a rectangle, we must make sure to update that whole
818 // rectangle in that case
819 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700820 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700821 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700822 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700824 // in the BUFFER_PRESERVED case, obviously, we can update only
825 // what's needed and nothing more.
826 // NOTE: this is NOT a common case, as preserving the backbuffer
827 // is costly and usually involves copying the whole update back.
828 }
829 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700830 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700831 // We need to redraw the rectangle that will be updated
832 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700833 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700834 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700835 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 } else {
837 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700839 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 }
841 }
842
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700843 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844 composeSurfaces(mDirtyRegion);
845
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700846 // update the swap region and clear the dirty region
847 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 mDirtyRegion.clear();
849}
850
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700851void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700853 const DisplayHardware& hw(graphicPlane(0).displayHardware());
854 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700855 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700856 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700857 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700858 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700859
Mathias Agopianf384cc32011-09-08 18:31:55 -0700860 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
861 size_t count = layers.size();
862
863 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700864 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
865 hwc.getNumLayers(), count);
866
867 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700868 if (hwc.initCheck() == NO_ERROR) {
869 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
870 }
Mathias Agopian45721772010-08-12 15:03:26 -0700871
872 /*
873 * update the per-frame h/w composer data for each layer
874 * and build the transparent region of the FB
875 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700876 for (size_t i=0 ; i<count ; i++) {
877 const sp<LayerBase>& layer(layers[i]);
878 layer->setPerFrameData(&cur[i]);
879 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700880 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700881 status_t err = hwc.prepare();
882 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700883
Mathias Agopianf384cc32011-09-08 18:31:55 -0700884 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700885 // what's happening here is tricky.
886 // we want to clear all the layers with the CLEAR_FB flags
887 // that are opaque.
888 // however, since some GPU are efficient at preserving
889 // the backbuffer, we want to take advantage of that so we do the
890 // clear only in the dirty region (other areas will be preserved
891 // on those GPUs).
892 // NOTE: on non backbuffer preserving GPU, the dirty region
893 // has already been expanded as needed, so the code is correct
894 // there too.
895 //
896 // However, the content of the framebuffer cannot be trusted when
897 // we switch to/from FB/OVERLAY, in which case we need to
898 // expand the dirty region to those areas too.
899 //
900 // Note also that there is a special case when switching from
901 // "no layers in FB" to "some layers in FB", where we need to redraw
902 // the entire FB, since some areas might contain uninitialized
903 // data.
904 //
905 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700906 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700907 // that is, we can't erase anything outside the dirty region.
908
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700909 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700910
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700911 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
912 transparent.set(hw.getBounds());
913 dirtyInOut = transparent;
914 } else {
915 for (size_t i=0 ; i<count ; i++) {
916 const sp<LayerBase>& layer(layers[i]);
917 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
918 transparent.orSelf(layer->visibleRegionScreen);
919 }
920 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
921 if (isOverlay != layer->isOverlay()) {
922 // we transitioned to/from overlay, so add this layer
923 // to the dirty region so the framebuffer can be either
924 // cleared or redrawn.
925 dirtyInOut.orSelf(layer->visibleRegionScreen);
926 }
927 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800928 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700929 // don't erase stuff outside the dirty region
930 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700931 }
932
933 /*
934 * clear the area of the FB that need to be transparent
935 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700936 if (!transparent.isEmpty()) {
937 glClearColor(0,0,0,0);
938 Region::const_iterator it = transparent.begin();
939 Region::const_iterator const end = transparent.end();
940 const int32_t height = hw.getHeight();
941 while (it != end) {
942 const Rect& r(*it++);
943 const GLint sy = height - (r.top + r.height());
944 glScissor(r.left, sy, r.width(), r.height());
945 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800946 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700947 }
948 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700949}
Mathias Agopian45721772010-08-12 15:03:26 -0700950
Mathias Agopianf384cc32011-09-08 18:31:55 -0700951void SurfaceFlinger::composeSurfaces(const Region& dirty)
952{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700953 const DisplayHardware& hw(graphicPlane(0).displayHardware());
954 HWComposer& hwc(hw.getHwComposer());
955
956 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
957 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700958 // should never happen unless the window manager has a bug
959 // draw something...
960 drawWormhole();
961 }
962
Mathias Agopian45721772010-08-12 15:03:26 -0700963 /*
964 * and then, render the layers targeted at the framebuffer
965 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700966 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700967 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
968 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700969 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700970 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700971 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700972 }
973 const sp<LayerBase>& layer(layers[i]);
974 const Region clip(dirty.intersect(layer->visibleRegionScreen));
975 if (!clip.isEmpty()) {
976 layer->draw(clip);
977 }
978 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979}
980
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981void SurfaceFlinger::debugFlashRegions()
982{
Mathias Agopian0a917752010-06-14 21:20:00 -0700983 const DisplayHardware& hw(graphicPlane(0).displayHardware());
984 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700985 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700986 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700987 return;
988 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700989
Mathias Agopian0a917752010-06-14 21:20:00 -0700990 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
991 (flags & DisplayHardware::BUFFER_PRESERVED))) {
992 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
993 mDirtyRegion.bounds() : hw.bounds());
994 composeSurfaces(repaint);
995 }
996
Mathias Agopianc492e672011-10-18 14:49:27 -0700997 glDisable(GL_TEXTURE_EXTERNAL_OES);
998 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 glDisable(GL_SCISSOR_TEST);
1001
Mathias Agopian0926f502009-05-04 14:17:04 -07001002 static int toggle = 0;
1003 toggle = 1 - toggle;
1004 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001005 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001006 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001007 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001008 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
Mathias Agopian20f68782009-05-11 00:03:41 -07001010 Region::const_iterator it = mDirtyRegion.begin();
1011 Region::const_iterator const end = mDirtyRegion.end();
1012 while (it != end) {
1013 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001015 { r.left, height - r.top },
1016 { r.left, height - r.bottom },
1017 { r.right, height - r.bottom },
1018 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 };
1020 glVertexPointer(2, GL_FLOAT, 0, vertices);
1021 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1022 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001023
Mathias Agopian0656a682011-09-20 17:22:44 -07001024 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025
1026 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001027 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028
1029 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030}
1031
1032void SurfaceFlinger::drawWormhole() const
1033{
1034 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1035 if (region.isEmpty())
1036 return;
1037
1038 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1039 const int32_t width = hw.getWidth();
1040 const int32_t height = hw.getHeight();
1041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001043 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001044 Region::const_iterator it = region.begin();
1045 Region::const_iterator const end = region.end();
1046 while (it != end) {
1047 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 const GLint sy = height - (r.top + r.height());
1049 glScissor(r.left, sy, r.width(), r.height());
1050 glClear(GL_COLOR_BUFFER_BIT);
1051 }
1052 } else {
1053 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1054 { width, height }, { 0, height } };
1055 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 glVertexPointer(2, GL_SHORT, 0, vertices);
1058 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1059 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001060
1061 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 glEnable(GL_TEXTURE_2D);
1063 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1064 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1065 glMatrixMode(GL_TEXTURE);
1066 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001067
1068 glDisable(GL_BLEND);
1069
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001071 Region::const_iterator it = region.begin();
1072 Region::const_iterator const end = region.end();
1073 while (it != end) {
1074 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 const GLint sy = height - (r.top + r.height());
1076 glScissor(r.left, sy, r.width(), r.height());
1077 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1078 }
1079 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001080 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001081 glLoadIdentity();
1082 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 }
1084}
1085
1086void SurfaceFlinger::debugShowFPS() const
1087{
1088 static int mFrameCount;
1089 static int mLastFrameCount = 0;
1090 static nsecs_t mLastFpsTime = 0;
1091 static float mFps = 0;
1092 mFrameCount++;
1093 nsecs_t now = systemTime();
1094 nsecs_t diff = now - mLastFpsTime;
1095 if (diff > ms2ns(250)) {
1096 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1097 mLastFpsTime = now;
1098 mLastFrameCount = mFrameCount;
1099 }
1100 // XXX: mFPS has the value we want
1101 }
1102
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001103status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104{
1105 Mutex::Autolock _l(mStateLock);
1106 addLayer_l(layer);
1107 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1108 return NO_ERROR;
1109}
1110
Mathias Agopian96f08192010-06-02 23:28:45 -07001111status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1112{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001113 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001114 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1115}
1116
1117ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1118 const sp<LayerBaseClient>& lbc)
1119{
Mathias Agopian96f08192010-06-02 23:28:45 -07001120 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001121 size_t name = client->attachLayer(lbc);
1122
1123 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001124
1125 // add this layer to the current state list
1126 addLayer_l(lbc);
1127
Mathias Agopian4f113742011-05-03 16:21:41 -07001128 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001129}
1130
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001131status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132{
1133 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001134 status_t err = purgatorizeLayer_l(layer);
1135 if (err == NO_ERROR)
1136 setTransactionFlags(eTransactionNeeded);
1137 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138}
1139
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001140status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001142 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1143 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001144 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1147 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001148 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 return NO_ERROR;
1150 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001151 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152}
1153
Mathias Agopian9a112062009-04-17 19:36:26 -07001154status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1155{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001156 // First add the layer to the purgatory list, which makes sure it won't
1157 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001158 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001159 if (err >= 0) {
1160 mLayerPurgatory.add(layerBase);
1161 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001162
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001163 layerBase->onRemoved();
1164
Mathias Agopian3d579642009-06-04 18:46:21 -07001165 // it's possible that we don't find a layer, because it might
1166 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001167 // from the user because there is a race between Client::destroySurface(),
1168 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001169 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1170}
1171
Mathias Agopian96f08192010-06-02 23:28:45 -07001172status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173{
Mathias Agopian96f08192010-06-02 23:28:45 -07001174 layer->forceVisibilityTransaction();
1175 setTransactionFlags(eTraversalNeeded);
1176 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177}
1178
Mathias Agopiandea20b12011-05-03 17:04:02 -07001179uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1180{
1181 return android_atomic_release_load(&mTransactionFlags);
1182}
1183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1185{
1186 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1187}
1188
Mathias Agopianbb641242010-05-18 17:06:55 -07001189uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190{
1191 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1192 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001193 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 }
1195 return old;
1196}
1197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001199void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001200 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001201 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001202
Jamie Gennis28378392011-10-12 17:39:00 -07001203 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001204 if (mCurrentState.orientation != orientation) {
1205 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1206 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001207 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001208 } else if (orientation != eOrientationUnchanged) {
1209 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1210 orientation);
1211 }
1212 }
1213
Mathias Agopian698c0872011-06-28 19:09:31 -07001214 const size_t count = state.size();
1215 for (size_t i=0 ; i<count ; i++) {
1216 const ComposerState& s(state[i]);
1217 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001218 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001219 }
Jamie Gennis28378392011-10-12 17:39:00 -07001220 if (transactionFlags) {
1221 setTransactionFlags(transactionFlags);
Mathias Agopian698c0872011-06-28 19:09:31 -07001222 }
1223
Jamie Gennis28378392011-10-12 17:39:00 -07001224 // if this is a synchronous transaction, wait for it to take effect before
1225 // returning.
1226 if (flags & eSynchronous) {
1227 mTransationPending = true;
1228 }
1229 while (mTransationPending) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001230 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1231 if (CC_UNLIKELY(err != NO_ERROR)) {
1232 // just in case something goes wrong in SF, return to the
1233 // called after a few seconds.
1234 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
Jamie Gennis28378392011-10-12 17:39:00 -07001235 mTransationPending = false;
Mathias Agopian698c0872011-06-28 19:09:31 -07001236 break;
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 }
1239}
1240
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001241int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001242 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243{
1244 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1245 return BAD_VALUE;
1246
1247 Mutex::Autolock _l(mStateLock);
1248 if (mCurrentState.orientation != orientation) {
1249 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001250 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251 mCurrentState.orientation = orientation;
1252 setTransactionFlags(eTransactionNeeded);
1253 mTransactionCV.wait(mStateLock);
1254 } else {
1255 orientation = BAD_VALUE;
1256 }
1257 }
1258 return orientation;
1259}
1260
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001261sp<ISurface> SurfaceFlinger::createSurface(
1262 ISurfaceComposerClient::surface_data_t* params,
1263 const String8& name,
1264 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1266 uint32_t flags)
1267{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001268 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001269 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001270
1271 if (int32_t(w|h) < 0) {
1272 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1273 int(w), int(h));
1274 return surfaceHandle;
1275 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001278 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 switch (flags & eFXSurfaceMask) {
1280 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001281 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1282 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 break;
1284 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001285 // for now we treat Blur as Dim, until we can implement it
1286 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001288 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001290 case eFXSurfaceScreenshot:
1291 layer = createScreenshotSurface(client, d, w, h, flags);
1292 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 }
1294
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001295 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001296 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001297 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001298 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001301 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001303 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001304 if (normalLayer != 0) {
1305 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001306 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001307 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001308 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001309
Mathias Agopian96f08192010-06-02 23:28:45 -07001310 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 }
1312
1313 return surfaceHandle;
1314}
1315
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001316sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001317 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001318 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001319 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320{
1321 // initialize the surfaces
1322 switch (format) { // TODO: take h/w into account
1323 case PIXEL_FORMAT_TRANSPARENT:
1324 case PIXEL_FORMAT_TRANSLUCENT:
1325 format = PIXEL_FORMAT_RGBA_8888;
1326 break;
1327 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001328#ifdef NO_RGBX_8888
1329 format = PIXEL_FORMAT_RGB_565;
1330#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001331 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001332#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 break;
1334 }
1335
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001336#ifdef NO_RGBX_8888
1337 if (format == PIXEL_FORMAT_RGBX_8888)
1338 format = PIXEL_FORMAT_RGBA_8888;
1339#endif
1340
Mathias Agopian96f08192010-06-02 23:28:45 -07001341 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001342 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001343 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001345 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 }
1347 return layer;
1348}
1349
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001350sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001351 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001352 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353{
Mathias Agopian96f08192010-06-02 23:28:45 -07001354 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001355 return layer;
1356}
1357
1358sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1359 const sp<Client>& client, DisplayID display,
1360 uint32_t w, uint32_t h, uint32_t flags)
1361{
1362 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
1363 status_t err = layer->capture();
1364 if (err != NO_ERROR) {
1365 layer.clear();
1366 LOGW("createScreenshotSurface failed (%s)", strerror(-err));
1367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 return layer;
1369}
1370
Mathias Agopian96f08192010-06-02 23:28:45 -07001371status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372{
Mathias Agopian9a112062009-04-17 19:36:26 -07001373 /*
1374 * called by the window manager, when a surface should be marked for
1375 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001376 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001377 * The surface is removed from the current and drawing lists, but placed
1378 * in the purgatory queue, so it's not destroyed right-away (we need
1379 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001380 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001381
Mathias Agopian48d819a2009-09-10 19:41:18 -07001382 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001383 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001384 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001385 if (layer != 0) {
1386 err = purgatorizeLayer_l(layer);
1387 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001388 setTransactionFlags(eTransactionNeeded);
1389 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001390 }
1391 return err;
1392}
1393
Mathias Agopianca4d3602011-05-19 15:38:14 -07001394status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001395{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001396 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001397 status_t err = NO_ERROR;
1398 sp<LayerBaseClient> l(layer.promote());
1399 if (l != NULL) {
1400 Mutex::Autolock _l(mStateLock);
1401 err = removeLayer_l(l);
1402 if (err == NAME_NOT_FOUND) {
1403 // The surface wasn't in the current list, which means it was
1404 // removed already, which means it is in the purgatory,
1405 // and need to be removed from there.
1406 ssize_t idx = mLayerPurgatory.remove(l);
1407 LOGE_IF(idx < 0,
1408 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001409 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001410 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1411 "error removing layer=%p (%s)", l.get(), strerror(-err));
1412 }
1413 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414}
1415
Mathias Agopian698c0872011-06-28 19:09:31 -07001416uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001417 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001418 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001421 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1422 if (layer != 0) {
1423 const uint32_t what = s.what;
1424 if (what & ePositionChanged) {
1425 if (layer->setPosition(s.x, s.y))
1426 flags |= eTraversalNeeded;
1427 }
1428 if (what & eLayerChanged) {
1429 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1430 if (layer->setLayer(s.z)) {
1431 mCurrentState.layersSortedByZ.removeAt(idx);
1432 mCurrentState.layersSortedByZ.add(layer);
1433 // we need traversal (state changed)
1434 // AND transaction (list changed)
1435 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 }
1437 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001438 if (what & eSizeChanged) {
1439 if (layer->setSize(s.w, s.h)) {
1440 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001441 }
1442 }
1443 if (what & eAlphaChanged) {
1444 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1445 flags |= eTraversalNeeded;
1446 }
1447 if (what & eMatrixChanged) {
1448 if (layer->setMatrix(s.matrix))
1449 flags |= eTraversalNeeded;
1450 }
1451 if (what & eTransparentRegionChanged) {
1452 if (layer->setTransparentRegionHint(s.transparentRegion))
1453 flags |= eTraversalNeeded;
1454 }
1455 if (what & eVisibilityChanged) {
1456 if (layer->setFlags(s.flags, s.mask))
1457 flags |= eTraversalNeeded;
1458 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001460 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463void SurfaceFlinger::screenReleased(int dpy)
1464{
1465 // this may be called by a signal handler, we can't do too much in here
1466 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1467 signalEvent();
1468}
1469
1470void SurfaceFlinger::screenAcquired(int dpy)
1471{
1472 // this may be called by a signal handler, we can't do too much in here
1473 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1474 signalEvent();
1475}
1476
1477status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1478{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001479 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 char buffer[SIZE];
1481 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001482
1483 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 snprintf(buffer, SIZE, "Permission Denial: "
1485 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1486 IPCThreadState::self()->getCallingPid(),
1487 IPCThreadState::self()->getCallingUid());
1488 result.append(buffer);
1489 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001490
1491 // figure out if we're stuck somewhere
1492 const nsecs_t now = systemTime();
1493 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1494 const nsecs_t inTransaction(mDebugInTransaction);
1495 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1496 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1497
1498 // Try to get the main lock, but don't insist if we can't
1499 // (this would indicate SF is stuck, but we want to be able to
1500 // print something in dumpsys).
1501 int retry = 3;
1502 while (mStateLock.tryLock()<0 && --retry>=0) {
1503 usleep(1000000);
1504 }
1505 const bool locked(retry >= 0);
1506 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001507 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001508 "SurfaceFlinger appears to be unresponsive, "
1509 "dumping anyways (no locks held)\n");
1510 result.append(buffer);
1511 }
1512
Mathias Agopian48b888a2011-01-19 16:15:53 -08001513 /*
1514 * Dump the visible layer list
1515 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1517 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001518 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1519 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001521 const sp<LayerBase>& layer(currentLayers[i]);
1522 layer->dump(result, buffer, SIZE);
1523 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 s.transparentRegion.dump(result, "transparentRegion");
1525 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1526 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1527 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001528
Mathias Agopian48b888a2011-01-19 16:15:53 -08001529 /*
1530 * Dump the layers in the purgatory
1531 */
1532
1533 const size_t purgatorySize = mLayerPurgatory.size();
1534 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1535 result.append(buffer);
1536 for (size_t i=0 ; i<purgatorySize ; i++) {
1537 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1538 layer->shortDump(result, buffer, SIZE);
1539 }
1540
1541 /*
1542 * Dump SurfaceFlinger global state
1543 */
1544
Mathias Agopianad701862011-07-14 18:01:49 -07001545 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001546 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001547
1548 const GLExtensions& extensions(GLExtensions::getInstance());
1549 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1550 extensions.getVendor(),
1551 extensions.getRenderer(),
1552 extensions.getVersion());
1553 result.append(buffer);
1554 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1555 result.append(buffer);
1556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 mWormholeRegion.dump(result, "WormholeRegion");
1558 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1559 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001560 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 mCurrentState.orientation, hw.canDraw());
1562 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001563 snprintf(buffer, SIZE,
1564 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001565 " last transaction time : %f us\n"
1566 " refresh-rate : %f fps\n"
1567 " x-dpi : %f\n"
1568 " y-dpi : %f\n",
1569 mLastSwapBufferTime/1000.0,
1570 mLastTransactionTime/1000.0,
1571 hw.getRefreshRate(),
1572 hw.getDpiX(),
1573 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001574 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001575
Mathias Agopian9795c422009-08-26 16:36:26 -07001576 if (inSwapBuffersDuration || !locked) {
1577 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1578 inSwapBuffersDuration/1000.0);
1579 result.append(buffer);
1580 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001581
Mathias Agopian9795c422009-08-26 16:36:26 -07001582 if (inTransactionDuration || !locked) {
1583 snprintf(buffer, SIZE, " transaction time: %f us\n",
1584 inTransactionDuration/1000.0);
1585 result.append(buffer);
1586 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001587
Mathias Agopian48b888a2011-01-19 16:15:53 -08001588 /*
1589 * Dump HWComposer state
1590 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001591 HWComposer& hwc(hw.getHwComposer());
1592 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1593 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001594 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001595 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001596 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001597
Mathias Agopian48b888a2011-01-19 16:15:53 -08001598 /*
1599 * Dump gralloc state
1600 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001601 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001602 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001603 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001604
1605 if (locked) {
1606 mStateLock.unlock();
1607 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 }
1609 write(fd, result.string(), result.size());
1610 return NO_ERROR;
1611}
1612
1613status_t SurfaceFlinger::onTransact(
1614 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1615{
1616 switch (code) {
1617 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001618 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001621 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001622 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 {
1624 // codes that require permission check
1625 IPCThreadState* ipc = IPCThreadState::self();
1626 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001627 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001628 if ((uid != AID_GRAPHICS) &&
1629 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001630 LOGE("Permission Denial: "
1631 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1632 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001634 break;
1635 }
1636 case CAPTURE_SCREEN:
1637 {
1638 // codes that require permission check
1639 IPCThreadState* ipc = IPCThreadState::self();
1640 const int pid = ipc->getCallingPid();
1641 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001642 if ((uid != AID_GRAPHICS) &&
1643 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001644 LOGE("Permission Denial: "
1645 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1646 return PERMISSION_DENIED;
1647 }
1648 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 }
1650 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1653 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001654 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001655 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001656 IPCThreadState* ipc = IPCThreadState::self();
1657 const int pid = ipc->getCallingPid();
1658 const int uid = ipc->getCallingUid();
1659 LOGE("Permission Denial: "
1660 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 return PERMISSION_DENIED;
1662 }
1663 int n;
1664 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001665 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001666 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 return NO_ERROR;
1668 case 1002: // SHOW_UPDATES
1669 n = data.readInt32();
1670 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001671 invalidateHwcGeometry();
1672 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 return NO_ERROR;
1674 case 1003: // SHOW_BACKGROUND
1675 n = data.readInt32();
1676 mDebugBackground = n ? 1 : 0;
1677 return NO_ERROR;
1678 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001679 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001680 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001682 case 1005:{ // force transaction
1683 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1684 return NO_ERROR;
1685 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001686 case 1006:{ // enable/disable GraphicLog
1687 int enabled = data.readInt32();
1688 GraphicLog::getInstance().setEnabled(enabled);
1689 return NO_ERROR;
1690 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001691 case 1008: // toggle use of hw composer
1692 n = data.readInt32();
1693 mDebugDisableHWC = n ? 1 : 0;
1694 invalidateHwcGeometry();
1695 repaintEverything();
1696 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001697 case 1009: // toggle use of transform hint
1698 n = data.readInt32();
1699 mDebugDisableTransformHint = n ? 1 : 0;
1700 invalidateHwcGeometry();
1701 repaintEverything();
1702 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001704 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 reply->writeInt32(0);
1706 reply->writeInt32(mDebugRegion);
1707 reply->writeInt32(mDebugBackground);
1708 return NO_ERROR;
1709 case 1013: {
1710 Mutex::Autolock _l(mStateLock);
1711 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1712 reply->writeInt32(hw.getPageFlipCount());
1713 }
1714 return NO_ERROR;
1715 }
1716 }
1717 return err;
1718}
1719
Mathias Agopian53331da2011-08-22 21:44:41 -07001720void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001721 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001722 const Rect bounds(hw.getBounds());
1723 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001724 signalEvent();
1725}
1726
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001727void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1728 Mutex::Autolock _l(mInvalidateLock);
1729 mInvalidateRegion = reg;
1730}
1731
1732Region SurfaceFlinger::getAndClearInvalidateRegion() {
1733 Mutex::Autolock _l(mInvalidateLock);
1734 Region reg(mInvalidateRegion);
1735 mInvalidateRegion.clear();
1736 return reg;
1737}
1738
Mathias Agopian59119e62010-10-11 12:37:43 -07001739// ---------------------------------------------------------------------------
1740
Mathias Agopian118d0242011-10-13 16:02:48 -07001741status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1742 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1743{
1744 Mutex::Autolock _l(mStateLock);
1745 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1746}
1747
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001748status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1749 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001750{
Mathias Agopian59119e62010-10-11 12:37:43 -07001751 if (!GLExtensions::getInstance().haveFramebufferObject())
1752 return INVALID_OPERATION;
1753
1754 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001755 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001756 const uint32_t hw_w = hw.getWidth();
1757 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001758 GLfloat u = 1;
1759 GLfloat v = 1;
1760
1761 // make sure to clear all GL error flags
1762 while ( glGetError() != GL_NO_ERROR ) ;
1763
1764 // create a FBO
1765 GLuint name, tname;
1766 glGenTextures(1, &tname);
1767 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001768 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1769 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001770 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001771 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001772 GLint tw = (2 << (31 - clz(hw_w)));
1773 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001774 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1775 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001776 u = GLfloat(hw_w) / tw;
1777 v = GLfloat(hw_h) / th;
1778 }
1779 glGenFramebuffersOES(1, &name);
1780 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001781 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1782 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001783
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001784 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001785 glDisable(GL_TEXTURE_EXTERNAL_OES);
1786 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001787 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001788 glClearColor(0,0,0,1);
1789 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001790 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001791 glMatrixMode(GL_MODELVIEW);
1792 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001793 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1794 const size_t count = layers.size();
1795 for (size_t i=0 ; i<count ; ++i) {
1796 const sp<LayerBase>& layer(layers[i]);
1797 layer->drawForSreenShot();
1798 }
1799
Mathias Agopian118d0242011-10-13 16:02:48 -07001800 hw.compositionComplete();
1801
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001802 // back to main framebuffer
1803 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1804 glDisable(GL_SCISSOR_TEST);
1805 glDeleteFramebuffersOES(1, &name);
1806
1807 *textureName = tname;
1808 *uOut = u;
1809 *vOut = v;
1810 return NO_ERROR;
1811}
1812
1813// ---------------------------------------------------------------------------
1814
1815status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1816{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001817 // get screen geometry
1818 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1819 const uint32_t hw_w = hw.getWidth();
1820 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001821 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822
1823 GLfloat u, v;
1824 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001825 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001826 if (result != NO_ERROR) {
1827 return result;
1828 }
1829
1830 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001831 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001832 glBindTexture(GL_TEXTURE_2D, tname);
1833 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1834 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1835 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1836 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1837 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1838 glVertexPointer(2, GL_FLOAT, 0, vtx);
1839
Mathias Agopianffcf4652011-07-07 17:30:31 -07001840 /*
1841 * Texture coordinate mapping
1842 *
1843 * u
1844 * 1 +----------+---+
1845 * | | | | image is inverted
1846 * | V | | w.r.t. the texture
1847 * 1-v +----------+ | coordinates
1848 * | |
1849 * | |
1850 * | |
1851 * 0 +--------------+
1852 * 0 1
1853 *
1854 */
1855
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001856 class s_curve_interpolator {
1857 const float nbFrames, s, v;
1858 public:
1859 s_curve_interpolator(int nbFrames, float s)
1860 : nbFrames(1.0f / (nbFrames-1)), s(s),
1861 v(1.0f + expf(-s + 0.5f*s)) {
1862 }
1863 float operator()(int f) {
1864 const float x = f * nbFrames;
1865 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1866 }
1867 };
1868
1869 class v_stretch {
1870 const GLfloat hw_w, hw_h;
1871 public:
1872 v_stretch(uint32_t hw_w, uint32_t hw_h)
1873 : hw_w(hw_w), hw_h(hw_h) {
1874 }
1875 void operator()(GLfloat* vtx, float v) {
1876 const GLfloat w = hw_w + (hw_w * v);
1877 const GLfloat h = hw_h - (hw_h * v);
1878 const GLfloat x = (hw_w - w) * 0.5f;
1879 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001880 vtx[0] = x; vtx[1] = y;
1881 vtx[2] = x; vtx[3] = y + h;
1882 vtx[4] = x + w; vtx[5] = y + h;
1883 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001884 }
1885 };
1886
1887 class h_stretch {
1888 const GLfloat hw_w, hw_h;
1889 public:
1890 h_stretch(uint32_t hw_w, uint32_t hw_h)
1891 : hw_w(hw_w), hw_h(hw_h) {
1892 }
1893 void operator()(GLfloat* vtx, float v) {
1894 const GLfloat w = hw_w - (hw_w * v);
1895 const GLfloat h = 1.0f;
1896 const GLfloat x = (hw_w - w) * 0.5f;
1897 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001898 vtx[0] = x; vtx[1] = y;
1899 vtx[2] = x; vtx[3] = y + h;
1900 vtx[4] = x + w; vtx[5] = y + h;
1901 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001902 }
1903 };
1904
1905 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001906 char value[PROPERTY_VALUE_MAX];
1907 property_get("debug.sf.electron_frames", value, "24");
1908 int nbFrames = (atoi(value) + 1) >> 1;
1909 if (nbFrames <= 0) // just in case
1910 nbFrames = 24;
1911
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001912 s_curve_interpolator itr(nbFrames, 7.5f);
1913 s_curve_interpolator itg(nbFrames, 8.0f);
1914 s_curve_interpolator itb(nbFrames, 8.5f);
1915
1916 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001917
1918 glMatrixMode(GL_TEXTURE);
1919 glLoadIdentity();
1920 glMatrixMode(GL_MODELVIEW);
1921 glLoadIdentity();
1922
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001923 glEnable(GL_BLEND);
1924 glBlendFunc(GL_ONE, GL_ONE);
1925 for (int i=0 ; i<nbFrames ; i++) {
1926 float x, y, w, h;
1927 const float vr = itr(i);
1928 const float vg = itg(i);
1929 const float vb = itb(i);
1930
1931 // clear screen
1932 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001933 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001934 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001935
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001936 // draw the red plane
1937 vverts(vtx, vr);
1938 glColorMask(1,0,0,1);
1939 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001940
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001941 // draw the green plane
1942 vverts(vtx, vg);
1943 glColorMask(0,1,0,1);
1944 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001945
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001946 // draw the blue plane
1947 vverts(vtx, vb);
1948 glColorMask(0,0,1,1);
1949 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001950
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001951 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001952 glDisable(GL_TEXTURE_2D);
1953 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001954 glColor4f(vg, vg, vg, 1);
1955 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1956 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001957 }
1958
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001959 h_stretch hverts(hw_w, hw_h);
1960 glDisable(GL_BLEND);
1961 glDisable(GL_TEXTURE_2D);
1962 glColorMask(1,1,1,1);
1963 for (int i=0 ; i<nbFrames ; i++) {
1964 const float v = itg(i);
1965 hverts(vtx, v);
1966 glClear(GL_COLOR_BUFFER_BIT);
1967 glColor4f(1-v, 1-v, 1-v, 1);
1968 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1969 hw.flip(screenBounds);
1970 }
1971
1972 glColorMask(1,1,1,1);
1973 glEnable(GL_SCISSOR_TEST);
1974 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1975 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001976 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07001977 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001978 return NO_ERROR;
1979}
1980
1981status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1982{
1983 status_t result = PERMISSION_DENIED;
1984
1985 if (!GLExtensions::getInstance().haveFramebufferObject())
1986 return INVALID_OPERATION;
1987
1988
1989 // get screen geometry
1990 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1991 const uint32_t hw_w = hw.getWidth();
1992 const uint32_t hw_h = hw.getHeight();
1993 const Region screenBounds(hw.bounds());
1994
1995 GLfloat u, v;
1996 GLuint tname;
1997 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1998 if (result != NO_ERROR) {
1999 return result;
2000 }
2001
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002002 GLfloat vtx[8];
2003 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002004 glBindTexture(GL_TEXTURE_2D, tname);
2005 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2006 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2007 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2008 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2009 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2010 glVertexPointer(2, GL_FLOAT, 0, vtx);
2011
2012 class s_curve_interpolator {
2013 const float nbFrames, s, v;
2014 public:
2015 s_curve_interpolator(int nbFrames, float s)
2016 : nbFrames(1.0f / (nbFrames-1)), s(s),
2017 v(1.0f + expf(-s + 0.5f*s)) {
2018 }
2019 float operator()(int f) {
2020 const float x = f * nbFrames;
2021 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2022 }
2023 };
2024
2025 class v_stretch {
2026 const GLfloat hw_w, hw_h;
2027 public:
2028 v_stretch(uint32_t hw_w, uint32_t hw_h)
2029 : hw_w(hw_w), hw_h(hw_h) {
2030 }
2031 void operator()(GLfloat* vtx, float v) {
2032 const GLfloat w = hw_w + (hw_w * v);
2033 const GLfloat h = hw_h - (hw_h * v);
2034 const GLfloat x = (hw_w - w) * 0.5f;
2035 const GLfloat y = (hw_h - h) * 0.5f;
2036 vtx[0] = x; vtx[1] = y;
2037 vtx[2] = x; vtx[3] = y + h;
2038 vtx[4] = x + w; vtx[5] = y + h;
2039 vtx[6] = x + w; vtx[7] = y;
2040 }
2041 };
2042
2043 class h_stretch {
2044 const GLfloat hw_w, hw_h;
2045 public:
2046 h_stretch(uint32_t hw_w, uint32_t hw_h)
2047 : hw_w(hw_w), hw_h(hw_h) {
2048 }
2049 void operator()(GLfloat* vtx, float v) {
2050 const GLfloat w = hw_w - (hw_w * v);
2051 const GLfloat h = 1.0f;
2052 const GLfloat x = (hw_w - w) * 0.5f;
2053 const GLfloat y = (hw_h - h) * 0.5f;
2054 vtx[0] = x; vtx[1] = y;
2055 vtx[2] = x; vtx[3] = y + h;
2056 vtx[4] = x + w; vtx[5] = y + h;
2057 vtx[6] = x + w; vtx[7] = y;
2058 }
2059 };
2060
Mathias Agopiana6546e52010-10-14 12:33:07 -07002061 // the full animation is 12 frames
2062 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002063 s_curve_interpolator itr(nbFrames, 7.5f);
2064 s_curve_interpolator itg(nbFrames, 8.0f);
2065 s_curve_interpolator itb(nbFrames, 8.5f);
2066
2067 h_stretch hverts(hw_w, hw_h);
2068 glDisable(GL_BLEND);
2069 glDisable(GL_TEXTURE_2D);
2070 glColorMask(1,1,1,1);
2071 for (int i=nbFrames-1 ; i>=0 ; i--) {
2072 const float v = itg(i);
2073 hverts(vtx, v);
2074 glClear(GL_COLOR_BUFFER_BIT);
2075 glColor4f(1-v, 1-v, 1-v, 1);
2076 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2077 hw.flip(screenBounds);
2078 }
2079
Mathias Agopiana6546e52010-10-14 12:33:07 -07002080 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002081 v_stretch vverts(hw_w, hw_h);
2082 glEnable(GL_BLEND);
2083 glBlendFunc(GL_ONE, GL_ONE);
2084 for (int i=nbFrames-1 ; i>=0 ; i--) {
2085 float x, y, w, h;
2086 const float vr = itr(i);
2087 const float vg = itg(i);
2088 const float vb = itb(i);
2089
2090 // clear screen
2091 glColorMask(1,1,1,1);
2092 glClear(GL_COLOR_BUFFER_BIT);
2093 glEnable(GL_TEXTURE_2D);
2094
2095 // draw the red plane
2096 vverts(vtx, vr);
2097 glColorMask(1,0,0,1);
2098 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2099
2100 // draw the green plane
2101 vverts(vtx, vg);
2102 glColorMask(0,1,0,1);
2103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2104
2105 // draw the blue plane
2106 vverts(vtx, vb);
2107 glColorMask(0,0,1,1);
2108 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2109
2110 hw.flip(screenBounds);
2111 }
2112
2113 glColorMask(1,1,1,1);
2114 glEnable(GL_SCISSOR_TEST);
2115 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002116 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002117 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002118 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002119
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002120 return NO_ERROR;
2121}
2122
2123// ---------------------------------------------------------------------------
2124
Mathias Agopianabd671a2010-10-14 14:54:06 -07002125status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002126{
2127 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2128 if (!hw.canDraw()) {
2129 // we're already off
2130 return NO_ERROR;
2131 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002132
2133 // turn off hwc while we're doing the animation
2134 hw.getHwComposer().disable();
2135 // and make sure to turn it back on (if needed) next time we compose
2136 invalidateHwcGeometry();
2137
Mathias Agopianabd671a2010-10-14 14:54:06 -07002138 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2139 electronBeamOffAnimationImplLocked();
2140 }
2141
2142 // always clear the whole screen at the end of the animation
2143 glClearColor(0,0,0,1);
2144 glDisable(GL_SCISSOR_TEST);
2145 glClear(GL_COLOR_BUFFER_BIT);
2146 glEnable(GL_SCISSOR_TEST);
2147 hw.flip( Region(hw.bounds()) );
2148
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002149 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002150}
2151
2152status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2153{
Mathias Agopian59119e62010-10-11 12:37:43 -07002154 class MessageTurnElectronBeamOff : public MessageBase {
2155 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002156 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002157 status_t result;
2158 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002159 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2160 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002161 }
2162 status_t getResult() const {
2163 return result;
2164 }
2165 virtual bool handler() {
2166 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002167 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002168 return true;
2169 }
2170 };
2171
Mathias Agopianabd671a2010-10-14 14:54:06 -07002172 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002173 status_t res = postMessageSync(msg);
2174 if (res == NO_ERROR) {
2175 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002176
2177 // work-around: when the power-manager calls us we activate the
2178 // animation. eventually, the "on" animation will be called
2179 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002180 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002181 }
2182 return res;
2183}
2184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002186
Mathias Agopianabd671a2010-10-14 14:54:06 -07002187status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002188{
2189 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2190 if (hw.canDraw()) {
2191 // we're already on
2192 return NO_ERROR;
2193 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002194 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2195 electronBeamOnAnimationImplLocked();
2196 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002197
2198 // make sure to redraw the whole screen when the animation is done
2199 mDirtyRegion.set(hw.bounds());
2200 signalEvent();
2201
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002202 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002203}
2204
2205status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2206{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 class MessageTurnElectronBeamOn : public MessageBase {
2208 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002209 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002210 status_t result;
2211 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002212 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2213 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002214 }
2215 status_t getResult() const {
2216 return result;
2217 }
2218 virtual bool handler() {
2219 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002220 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002221 return true;
2222 }
2223 };
2224
Mathias Agopianabd671a2010-10-14 14:54:06 -07002225 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 return NO_ERROR;
2227}
2228
2229// ---------------------------------------------------------------------------
2230
Mathias Agopian74c40c02010-09-29 13:02:36 -07002231status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2232 sp<IMemoryHeap>* heap,
2233 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002234 uint32_t sw, uint32_t sh,
2235 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236{
2237 status_t result = PERMISSION_DENIED;
2238
2239 // only one display supported for now
2240 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2241 return BAD_VALUE;
2242
2243 if (!GLExtensions::getInstance().haveFramebufferObject())
2244 return INVALID_OPERATION;
2245
2246 // get screen geometry
2247 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2248 const uint32_t hw_w = hw.getWidth();
2249 const uint32_t hw_h = hw.getHeight();
2250
2251 if ((sw > hw_w) || (sh > hw_h))
2252 return BAD_VALUE;
2253
2254 sw = (!sw) ? hw_w : sw;
2255 sh = (!sh) ? hw_h : sh;
2256 const size_t size = sw * sh * 4;
2257
Mathias Agopian1c71a472011-03-02 18:45:50 -08002258 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2259 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002260
Mathias Agopian74c40c02010-09-29 13:02:36 -07002261 // make sure to clear all GL error flags
2262 while ( glGetError() != GL_NO_ERROR ) ;
2263
2264 // create a FBO
2265 GLuint name, tname;
2266 glGenRenderbuffersOES(1, &tname);
2267 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2268 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2269 glGenFramebuffersOES(1, &name);
2270 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2271 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2272 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2273
2274 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002275
Mathias Agopian74c40c02010-09-29 13:02:36 -07002276 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2277
2278 // invert everything, b/c glReadPixel() below will invert the FB
2279 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002280 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002281 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002282 glMatrixMode(GL_PROJECTION);
2283 glPushMatrix();
2284 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002285 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002286 glMatrixMode(GL_MODELVIEW);
2287
2288 // redraw the screen entirely...
2289 glClearColor(0,0,0,1);
2290 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002291
Jamie Gennis9575f602011-10-07 14:51:16 -07002292 const LayerVector& layers(mDrawingState.layersSortedByZ);
2293 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002294 for (size_t i=0 ; i<count ; ++i) {
2295 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002296 const uint32_t flags = layer->drawingState().flags;
2297 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2298 const uint32_t z = layer->drawingState().z;
2299 if (z >= minLayerZ && z <= maxLayerZ) {
2300 layer->drawForSreenShot();
2301 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002302 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002303 }
2304
2305 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002306 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002307 glScissor(0, 0, sw, sh);
2308
2309 // check for errors and return screen capture
2310 if (glGetError() != GL_NO_ERROR) {
2311 // error while rendering
2312 result = INVALID_OPERATION;
2313 } else {
2314 // allocate shared memory large enough to hold the
2315 // screen capture
2316 sp<MemoryHeapBase> base(
2317 new MemoryHeapBase(size, 0, "screen-capture") );
2318 void* const ptr = base->getBase();
2319 if (ptr) {
2320 // capture the screen with glReadPixels()
2321 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2322 if (glGetError() == GL_NO_ERROR) {
2323 *heap = base;
2324 *w = sw;
2325 *h = sh;
2326 *f = PIXEL_FORMAT_RGBA_8888;
2327 result = NO_ERROR;
2328 }
2329 } else {
2330 result = NO_MEMORY;
2331 }
2332 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002333 glEnable(GL_SCISSOR_TEST);
2334 glViewport(0, 0, hw_w, hw_h);
2335 glMatrixMode(GL_PROJECTION);
2336 glPopMatrix();
2337 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002338 } else {
2339 result = BAD_VALUE;
2340 }
2341
2342 // release FBO resources
2343 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2344 glDeleteRenderbuffersOES(1, &tname);
2345 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002346
2347 hw.compositionComplete();
2348
Mathias Agopian1c71a472011-03-02 18:45:50 -08002349 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002350
Mathias Agopian74c40c02010-09-29 13:02:36 -07002351 return result;
2352}
2353
2354
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002355status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2356 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002357 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002358 uint32_t sw, uint32_t sh,
2359 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002360{
2361 // only one display supported for now
2362 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2363 return BAD_VALUE;
2364
2365 if (!GLExtensions::getInstance().haveFramebufferObject())
2366 return INVALID_OPERATION;
2367
2368 class MessageCaptureScreen : public MessageBase {
2369 SurfaceFlinger* flinger;
2370 DisplayID dpy;
2371 sp<IMemoryHeap>* heap;
2372 uint32_t* w;
2373 uint32_t* h;
2374 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002375 uint32_t sw;
2376 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002377 uint32_t minLayerZ;
2378 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002379 status_t result;
2380 public:
2381 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002382 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002383 uint32_t sw, uint32_t sh,
2384 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002385 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002386 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2387 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2388 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002389 {
2390 }
2391 status_t getResult() const {
2392 return result;
2393 }
2394 virtual bool handler() {
2395 Mutex::Autolock _l(flinger->mStateLock);
2396
2397 // if we have secure windows, never allow the screen capture
2398 if (flinger->mSecureFrameBuffer)
2399 return true;
2400
Mathias Agopian74c40c02010-09-29 13:02:36 -07002401 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002402 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002403
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002404 return true;
2405 }
2406 };
2407
2408 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002409 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002410 status_t res = postMessageSync(msg);
2411 if (res == NO_ERROR) {
2412 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2413 }
2414 return res;
2415}
2416
2417// ---------------------------------------------------------------------------
2418
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002419sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2420{
2421 sp<Layer> result;
2422 Mutex::Autolock _l(mStateLock);
2423 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2424 return result;
2425}
2426
2427// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428
Mathias Agopian96f08192010-06-02 23:28:45 -07002429Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002430 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002432}
2433
Mathias Agopian96f08192010-06-02 23:28:45 -07002434Client::~Client()
2435{
Mathias Agopian96f08192010-06-02 23:28:45 -07002436 const size_t count = mLayers.size();
2437 for (size_t i=0 ; i<count ; i++) {
2438 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2439 if (layer != 0) {
2440 mFlinger->removeLayer(layer);
2441 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002442 }
2443}
2444
Mathias Agopian96f08192010-06-02 23:28:45 -07002445status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002446 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002448
Mathias Agopian4f113742011-05-03 16:21:41 -07002449size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002450{
Mathias Agopian4f113742011-05-03 16:21:41 -07002451 Mutex::Autolock _l(mLock);
2452 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002453 mLayers.add(name, layer);
2454 return name;
2455}
2456
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002457void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002458{
Mathias Agopian4f113742011-05-03 16:21:41 -07002459 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002460 // we do a linear search here, because this doesn't happen often
2461 const size_t count = mLayers.size();
2462 for (size_t i=0 ; i<count ; i++) {
2463 if (mLayers.valueAt(i) == layer) {
2464 mLayers.removeItemsAt(i, 1);
2465 break;
2466 }
2467 }
2468}
Mathias Agopian4f113742011-05-03 16:21:41 -07002469sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2470{
2471 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002472 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002473 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002474 if (layer != 0) {
2475 lbc = layer.promote();
2476 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002477 }
2478 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479}
2480
Mathias Agopiana67932f2011-04-20 14:20:59 -07002481
2482status_t Client::onTransact(
2483 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2484{
2485 // these must be checked
2486 IPCThreadState* ipc = IPCThreadState::self();
2487 const int pid = ipc->getCallingPid();
2488 const int uid = ipc->getCallingUid();
2489 const int self_pid = getpid();
2490 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2491 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002492 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002493 {
2494 LOGE("Permission Denial: "
2495 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2496 return PERMISSION_DENIED;
2497 }
2498 }
2499 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002500}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002501
2502
Mathias Agopian96f08192010-06-02 23:28:45 -07002503sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002504 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002505 const String8& name,
2506 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507 uint32_t flags)
2508{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002509 /*
2510 * createSurface must be called from the GL thread so that it can
2511 * have access to the GL context.
2512 */
2513
2514 class MessageCreateSurface : public MessageBase {
2515 sp<ISurface> result;
2516 SurfaceFlinger* flinger;
2517 ISurfaceComposerClient::surface_data_t* params;
2518 Client* client;
2519 const String8& name;
2520 DisplayID display;
2521 uint32_t w, h;
2522 PixelFormat format;
2523 uint32_t flags;
2524 public:
2525 MessageCreateSurface(SurfaceFlinger* flinger,
2526 ISurfaceComposerClient::surface_data_t* params,
2527 const String8& name, Client* client,
2528 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2529 uint32_t flags)
2530 : flinger(flinger), params(params), client(client), name(name),
2531 display(display), w(w), h(h), format(format), flags(flags)
2532 {
2533 }
2534 sp<ISurface> getResult() const { return result; }
2535 virtual bool handler() {
2536 result = flinger->createSurface(params, name, client,
2537 display, w, h, format, flags);
2538 return true;
2539 }
2540 };
2541
2542 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2543 params, name, this, display, w, h, format, flags);
2544 mFlinger->postMessageSync(msg);
2545 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546}
Mathias Agopian96f08192010-06-02 23:28:45 -07002547status_t Client::destroySurface(SurfaceID sid) {
2548 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002549}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550
2551// ---------------------------------------------------------------------------
2552
Jamie Gennis9a78c902011-01-12 18:30:40 -08002553GraphicBufferAlloc::GraphicBufferAlloc() {}
2554
2555GraphicBufferAlloc::~GraphicBufferAlloc() {}
2556
2557sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002558 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002559 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2560 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002561 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002562 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002563 if (err == NO_MEMORY) {
2564 GraphicBuffer::dumpAllocationsToSystemLog();
2565 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002566 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2567 "failed (%s), handle=%p",
2568 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002569 return 0;
2570 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002571 return graphicBuffer;
2572}
2573
Jamie Gennis9a78c902011-01-12 18:30:40 -08002574// ---------------------------------------------------------------------------
2575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576GraphicPlane::GraphicPlane()
2577 : mHw(0)
2578{
2579}
2580
2581GraphicPlane::~GraphicPlane() {
2582 delete mHw;
2583}
2584
2585bool GraphicPlane::initialized() const {
2586 return mHw ? true : false;
2587}
2588
Mathias Agopian2b92d892010-02-08 15:49:35 -08002589int GraphicPlane::getWidth() const {
2590 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591}
2592
Mathias Agopian2b92d892010-02-08 15:49:35 -08002593int GraphicPlane::getHeight() const {
2594 return mHeight;
2595}
2596
2597void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2598{
2599 mHw = hw;
2600
2601 // initialize the display orientation transform.
2602 // it's a constant that should come from the display driver.
2603 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2604 char property[PROPERTY_VALUE_MAX];
2605 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2606 //displayOrientation
2607 switch (atoi(property)) {
2608 case 90:
2609 displayOrientation = ISurfaceComposer::eOrientation90;
2610 break;
2611 case 270:
2612 displayOrientation = ISurfaceComposer::eOrientation270;
2613 break;
2614 }
2615 }
2616
2617 const float w = hw->getWidth();
2618 const float h = hw->getHeight();
2619 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2620 &mDisplayTransform);
2621 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2622 mDisplayWidth = h;
2623 mDisplayHeight = w;
2624 } else {
2625 mDisplayWidth = w;
2626 mDisplayHeight = h;
2627 }
2628
2629 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630}
2631
2632status_t GraphicPlane::orientationToTransfrom(
2633 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002634{
2635 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002636 switch (orientation) {
2637 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002638 flags = Transform::ROT_0;
2639 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002641 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002642 break;
2643 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002644 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002645 break;
2646 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002647 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002648 break;
2649 default:
2650 return BAD_VALUE;
2651 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002652 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002653 return NO_ERROR;
2654}
2655
2656status_t GraphicPlane::setOrientation(int orientation)
2657{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658 // If the rotation can be handled in hardware, this is where
2659 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002660
2661 const DisplayHardware& hw(displayHardware());
2662 const float w = mDisplayWidth;
2663 const float h = mDisplayHeight;
2664 mWidth = int(w);
2665 mHeight = int(h);
2666
2667 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002668 GraphicPlane::orientationToTransfrom(orientation, w, h,
2669 &orientationTransform);
2670 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2671 mWidth = int(h);
2672 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002673 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002674
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002675 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002676 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 return NO_ERROR;
2678}
2679
2680const DisplayHardware& GraphicPlane::displayHardware() const {
2681 return *mHw;
2682}
2683
Mathias Agopian59119e62010-10-11 12:37:43 -07002684DisplayHardware& GraphicPlane::editDisplayHardware() {
2685 return *mHw;
2686}
2687
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688const Transform& GraphicPlane::transform() const {
2689 return mGlobalTransform;
2690}
2691
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002692EGLDisplay GraphicPlane::getEGLDisplay() const {
2693 return mHw->getEGLDisplay();
2694}
2695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696// ---------------------------------------------------------------------------
2697
2698}; // namespace android