blob: f38e9481458782c1b21a31396159c77c62c1a3b6 [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
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080068#define EGL_VERSION_HW_ANDROID 0x3143
69
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#define DISPLAY_COUNT 1
71
72namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ---------------------------------------------------------------------------
74
Mathias Agopian99b49842011-06-27 16:05:52 -070075const String16 sHardwareTest("android.permission.HARDWARE_TEST");
76const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
77const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
78const String16 sDump("android.permission.DUMP");
79
80// ---------------------------------------------------------------------------
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082SurfaceFlinger::SurfaceFlinger()
83 : BnSurfaceComposer(), Thread(false),
84 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070085 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070089 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070090 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070093 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070094 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070095 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -0700100 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mConsoleSignals(0),
102 mSecureFrameBuffer(0)
103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
109 LOGI("SurfaceFlinger is starting");
110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 property_get("debug.sf.showbackground", value, "0");
118 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120 property_get("debug.sf.ddms", value, "0");
121 mDebugDDMS = atoi(value);
122 if (mDebugDDMS) {
123 DdmConnection::start(getServiceName());
124 }
125
Mathias Agopian78fd5012010-04-20 14:51:04 -0700126 LOGI_IF(mDebugRegion, "showupdates enabled");
127 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129}
130
131SurfaceFlinger::~SurfaceFlinger()
132{
133 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134}
135
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700136sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700138 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Mathias Agopian7e27f052010-05-28 14:22:23 -0700141sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142{
Mathias Agopian96f08192010-06-02 23:28:45 -0700143 sp<ISurfaceComposerClient> bclient;
144 sp<Client> client(new Client(this));
145 status_t err = client->initCheck();
146 if (err == NO_ERROR) {
147 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return bclient;
150}
151
Jamie Gennis9a78c902011-01-12 18:30:40 -0800152sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
153{
154 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
155 return gba;
156}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
159{
160 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
161 const GraphicPlane& plane(mGraphicPlanes[dpy]);
162 return plane;
163}
164
165GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
166{
167 return const_cast<GraphicPlane&>(
168 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
169}
170
171void SurfaceFlinger::bootFinished()
172{
173 const nsecs_t now = systemTime();
174 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700175 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700176 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700177
178 // wait patiently for the window manager death
179 const String16 name("window");
180 sp<IBinder> window(defaultServiceManager()->getService(name));
181 if (window != 0) {
182 window->linkToDeath(this);
183 }
184
185 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700186 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700189void SurfaceFlinger::binderDied(const wp<IBinder>& who)
190{
191 // the window manager died on us. prepare its eulogy.
192
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700193 // reset screen orientation
194 setOrientation(0, eOrientationDefault, 0);
195
196 // restart the boot-animation
197 property_set("ctl.start", "bootanim");
198}
199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200void SurfaceFlinger::onFirstRef()
201{
202 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
203
204 // Wait for the main thread to be done with its initialization
205 mReadyToRunBarrier.wait();
206}
207
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208static inline uint16_t pack565(int r, int g, int b) {
209 return (r<<11)|(g<<5)|b;
210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212status_t SurfaceFlinger::readyToRun()
213{
214 LOGI( "SurfaceFlinger's main thread ready to run. "
215 "Initializing graphics H/W...");
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 // we only support one display currently
218 int dpy = 0;
219
220 {
221 // initialize the main display
222 GraphicPlane& plane(graphicPlane(dpy));
223 DisplayHardware* const hw = new DisplayHardware(this, dpy);
224 plane.setDisplayHardware(hw);
225 }
226
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700227 // create the shared control-block
228 mServerHeap = new MemoryHeapBase(4096,
229 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
230 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700231
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700232 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
233 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700234
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700235 new(mServerCblk) surface_flinger_cblk_t;
236
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 // initialize primary screen
238 // (other display should be initialized in the same manner, but
239 // asynchronously, as they could come and go. None of this is supported
240 // yet).
241 const GraphicPlane& plane(graphicPlane(dpy));
242 const DisplayHardware& hw = plane.displayHardware();
243 const uint32_t w = hw.getWidth();
244 const uint32_t h = hw.getHeight();
245 const uint32_t f = hw.getFormat();
246 hw.makeCurrent();
247
248 // initialize the shared control block
249 mServerCblk->connected |= 1<<dpy;
250 display_cblk_t* dcblk = mServerCblk->displays + dpy;
251 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800252 dcblk->w = plane.getWidth();
253 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 dcblk->format = f;
255 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
256 dcblk->xdpi = hw.getDpiX();
257 dcblk->ydpi = hw.getDpiY();
258 dcblk->fps = hw.getRefreshRate();
259 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260
261 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700263 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 glEnableClientState(GL_VERTEX_ARRAY);
265 glEnable(GL_SCISSOR_TEST);
266 glShadeModel(GL_FLAT);
267 glDisable(GL_DITHER);
268 glDisable(GL_CULL_FACE);
269
270 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
271 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700272 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 glGenTextures(1, &mWormholeTexName);
274 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
277 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
278 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700280 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
281
282 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
283 glGenTextures(1, &mProtectedTexName);
284 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
287 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
288 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
290 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291
292 glViewport(0, 0, w, h);
293 glMatrixMode(GL_PROJECTION);
294 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700295 // put the origin in the left-bottom corner
296 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 -0800297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 mReadyToRunBarrier.open();
299
300 /*
301 * We're now ready to accept clients...
302 */
303
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700304 // start boot animation
305 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 return NO_ERROR;
308}
309
310// ----------------------------------------------------------------------------
311#if 0
312#pragma mark -
313#pragma mark Events Handler
314#endif
315
316void SurfaceFlinger::waitForEvent()
317{
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700318 while (true) {
319 nsecs_t timeout = -1;
Mathias Agopianbb641242010-05-18 17:06:55 -0700320 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700321 if (msg != 0) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700322 switch (msg->what) {
323 case MessageQueue::INVALIDATE:
324 // invalidate message, just return to the main loop
325 return;
326 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 }
329}
330
331void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700332 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333}
334
Jamie Gennis582270d2011-08-17 18:19:00 -0700335bool SurfaceFlinger::authenticateSurfaceTexture(
336 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800337 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700338 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800339
340 // Check the visible layer list for the ISurface
341 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
342 size_t count = currentLayers.size();
343 for (size_t i=0 ; i<count ; i++) {
344 const sp<LayerBase>& layer(currentLayers[i]);
345 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700346 if (lbc != NULL) {
347 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
348 if (lbcBinder == surfaceTextureBinder) {
349 return true;
350 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800351 }
352 }
353
354 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700355 // SurfaceTexture gets destroyed before all the clients are done using it,
356 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800357 // will instead fail later on when the client tries to use the surface,
358 // which should be reported as "surface XYZ returned an -ENODEV". The
359 // purgatorized layers are no less authentic than the visible ones, so this
360 // should not cause any harm.
361 size_t purgatorySize = mLayerPurgatory.size();
362 for (size_t i=0 ; i<purgatorySize ; i++) {
363 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
364 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700365 if (lbc != NULL) {
366 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
367 if (lbcBinder == surfaceTextureBinder) {
368 return true;
369 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800370 }
371 }
372
373 return false;
374}
375
Mathias Agopianbb641242010-05-18 17:06:55 -0700376status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
377 nsecs_t reltime, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378{
Mathias Agopianbb641242010-05-18 17:06:55 -0700379 return mEventQueue.postMessage(msg, reltime, flags);
380}
381
382status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
383 nsecs_t reltime, uint32_t flags)
384{
385 status_t res = mEventQueue.postMessage(msg, reltime, flags);
386 if (res == NO_ERROR) {
387 msg->wait();
388 }
389 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390}
391
392// ----------------------------------------------------------------------------
393#if 0
394#pragma mark -
395#pragma mark Main loop
396#endif
397
398bool SurfaceFlinger::threadLoop()
399{
400 waitForEvent();
401
402 // check for transactions
403 if (UNLIKELY(mConsoleSignals)) {
404 handleConsoleEvents();
405 }
406
Mathias Agopian698c0872011-06-28 19:09:31 -0700407 // if we're in a global transaction, don't do anything.
408 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
409 uint32_t transactionFlags = peekTransactionFlags(mask);
410 if (UNLIKELY(transactionFlags)) {
411 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
413
414 // post surfaces (if needed)
415 handlePageFlip();
416
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700417 if (mDirtyRegion.isEmpty()) {
418 // nothing new to do.
419 return true;
420 }
421
Mathias Agopiana350ff92010-08-10 17:14:02 -0700422 if (UNLIKELY(mHwWorkListDirty)) {
423 // build the h/w work list
424 handleWorkList();
425 }
426
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700428 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700430
431 const int index = hw.getCurrentBufferIndex();
432 GraphicLog& logger(GraphicLog::getInstance());
433
434 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 handleRepaint();
436
Mathias Agopian74faca22009-09-17 16:18:16 -0700437 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700438 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700439 hw.compositionComplete();
440
Mathias Agopian35b48d12010-09-13 22:57:58 -0700441 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700442 postFramebuffer();
443
Mathias Agopian35b48d12010-09-13 22:57:58 -0700444 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 } else {
446 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800447 hw.compositionComplete();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 usleep(16667); // 60 fps period
449 }
450 return true;
451}
452
453void SurfaceFlinger::postFramebuffer()
454{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700455 // this should never happen. we do the flip anyways so we don't
456 // risk to cause a deadlock with hwc
457 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700458 const DisplayHardware& hw(graphicPlane(0).displayHardware());
459 const nsecs_t now = systemTime();
460 mDebugInSwapBuffers = now;
461 hw.flip(mSwapRegion);
462 mLastSwapBufferTime = systemTime() - now;
463 mDebugInSwapBuffers = 0;
464 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465}
466
467void SurfaceFlinger::handleConsoleEvents()
468{
469 // something to do with the console
470 const DisplayHardware& hw = graphicPlane(0).displayHardware();
471
472 int what = android_atomic_and(0, &mConsoleSignals);
473 if (what & eConsoleAcquired) {
474 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700475 // this is a temporary work-around, eventually this should be called
476 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700477 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 }
479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700481 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 }
484 }
485
486 mDirtyRegion.set(hw.bounds());
487}
488
489void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
490{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700491 Mutex::Autolock _l(mStateLock);
492 const nsecs_t now = systemTime();
493 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494
Mathias Agopianca4d3602011-05-19 15:38:14 -0700495 // Here we're guaranteed that some transaction flags are set
496 // so we can call handleTransactionLocked() unconditionally.
497 // We call getTransactionFlags(), which will also clear the flags,
498 // with mStateLock held to guarantee that mCurrentState won't change
499 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700500
Mathias Agopianca4d3602011-05-19 15:38:14 -0700501 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
502 transactionFlags = getTransactionFlags(mask);
503 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700504
Mathias Agopianca4d3602011-05-19 15:38:14 -0700505 mLastTransactionTime = systemTime() - now;
506 mDebugInTransaction = 0;
507 invalidateHwcGeometry();
508 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700509}
510
Mathias Agopianca4d3602011-05-19 15:38:14 -0700511void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700512{
513 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 const size_t count = currentLayers.size();
515
516 /*
517 * Traversal of the children
518 * (perform the transaction for each of them if needed)
519 */
520
521 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
522 if (layersNeedTransaction) {
523 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700524 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
526 if (!trFlags) continue;
527
528 const uint32_t flags = layer->doTransaction(0);
529 if (flags & Layer::eVisibleRegion)
530 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 }
532 }
533
534 /*
535 * Perform our own transaction if needed
536 */
537
538 if (transactionFlags & eTransactionNeeded) {
539 if (mCurrentState.orientation != mDrawingState.orientation) {
540 // the orientation has changed, recompute all visible regions
541 // and invalidate everything.
542
543 const int dpy = 0;
544 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700545 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546 GraphicPlane& plane(graphicPlane(dpy));
547 plane.setOrientation(orientation);
548
549 // update the shared control block
550 const DisplayHardware& hw(plane.displayHardware());
551 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
552 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800553 dcblk->w = plane.getWidth();
554 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555
556 mVisibleRegionsDirty = true;
557 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 }
559
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
561 // layers have been added
562 mVisibleRegionsDirty = true;
563 }
564
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 // some layers might have been removed, so
566 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700567 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700568 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700570 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700571 const size_t count = previousLayers.size();
572 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700573 const sp<LayerBase>& layer(previousLayers[i]);
574 if (currentLayers.indexOf( layer ) < 0) {
575 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700576 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700577 }
578 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 }
581
582 commitTransaction();
583}
584
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800586 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800587{
588 const GraphicPlane& plane(graphicPlane(0));
589 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700590 const DisplayHardware& hw(plane.displayHardware());
591 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592
593 Region aboveOpaqueLayers;
594 Region aboveCoveredLayers;
595 Region dirty;
596
597 bool secureFrameBuffer = false;
598
599 size_t i = currentLayers.size();
600 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700601 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 layer->validateVisibility(planeTransform);
603
604 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700605 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606
Mathias Agopianab028732010-03-16 16:41:46 -0700607 /*
608 * opaqueRegion: area of a surface that is fully opaque.
609 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700611
612 /*
613 * visibleRegion: area of a surface that is visible on screen
614 * and not fully transparent. This is essentially the layer's
615 * footprint minus the opaque regions above it.
616 * Areas covered by a translucent surface are considered visible.
617 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700619
620 /*
621 * coveredRegion: area of a surface that is covered by all
622 * visible regions above it (which includes the translucent areas).
623 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700625
626
627 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700628 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700629 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700630 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700632 visibleRegion.andSelf(screenRegion);
633 if (!visibleRegion.isEmpty()) {
634 // Remove the transparent area from the visible region
635 if (translucent) {
636 visibleRegion.subtractSelf(layer->transparentRegionScreen);
637 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638
Mathias Agopianab028732010-03-16 16:41:46 -0700639 // compute the opaque region
640 const int32_t layerOrientation = layer->getOrientation();
641 if (s.alpha==255 && !translucent &&
642 ((layerOrientation & Transform::ROT_INVALID) == false)) {
643 // the opaque region is the layer's footprint
644 opaqueRegion = visibleRegion;
645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 }
647 }
648
Mathias Agopianab028732010-03-16 16:41:46 -0700649 // Clip the covered region to the visible region
650 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
651
652 // Update aboveCoveredLayers for next (lower) layer
653 aboveCoveredLayers.orSelf(visibleRegion);
654
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 // subtract the opaque region covered by the layers above us
656 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657
658 // compute this layer's dirty region
659 if (layer->contentDirty) {
660 // we need to invalidate the whole region
661 dirty = visibleRegion;
662 // as well, as the old visible region
663 dirty.orSelf(layer->visibleRegionScreen);
664 layer->contentDirty = false;
665 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700666 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700667 * the exposed region consists of two components:
668 * 1) what's VISIBLE now and was COVERED before
669 * 2) what's EXPOSED now less what was EXPOSED before
670 *
671 * note that (1) is conservative, we start with the whole
672 * visible region but only keep what used to be covered by
673 * something -- which mean it may have been exposed.
674 *
675 * (2) handles areas that were not covered by anything but got
676 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700677 */
Mathias Agopianab028732010-03-16 16:41:46 -0700678 const Region newExposed = visibleRegion - coveredRegion;
679 const Region oldVisibleRegion = layer->visibleRegionScreen;
680 const Region oldCoveredRegion = layer->coveredRegionScreen;
681 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
682 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 }
684 dirty.subtractSelf(aboveOpaqueLayers);
685
686 // accumulate to the screen dirty region
687 dirtyRegion.orSelf(dirty);
688
Mathias Agopianab028732010-03-16 16:41:46 -0700689 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700691
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 // Store the visible region is screen space
693 layer->setVisibleRegion(visibleRegion);
694 layer->setCoveredRegion(coveredRegion);
695
Mathias Agopian97011222009-07-28 10:57:27 -0700696 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 if (layer->isSecure() && !visibleRegion.isEmpty()) {
698 secureFrameBuffer = true;
699 }
700 }
701
Mathias Agopian97011222009-07-28 10:57:27 -0700702 // invalidate the areas where a layer was removed
703 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
704 mDirtyRegionRemovedLayer.clear();
705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 mSecureFrameBuffer = secureFrameBuffer;
707 opaqueRegion = aboveOpaqueLayers;
708}
709
710
711void SurfaceFlinger::commitTransaction()
712{
713 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700714 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700715 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716}
717
718void SurfaceFlinger::handlePageFlip()
719{
720 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800721 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 visibleRegions |= lockPageFlip(currentLayers);
723
724 const DisplayHardware& hw = graphicPlane(0).displayHardware();
725 const Region screenRegion(hw.bounds());
726 if (visibleRegions) {
727 Region opaqueRegion;
728 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700729
730 /*
731 * rebuild the visible layer list
732 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800733 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700734 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700735 mVisibleLayersSortedByZ.setCapacity(count);
736 for (size_t i=0 ; i<count ; i++) {
737 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
738 mVisibleLayersSortedByZ.add(currentLayers[i]);
739 }
740
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741 mWormholeRegion = screenRegion.subtract(opaqueRegion);
742 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800743 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 }
745
746 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700747
748 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 mDirtyRegion.andSelf(screenRegion);
750}
751
Mathias Agopianad456f92011-01-13 17:53:01 -0800752void SurfaceFlinger::invalidateHwcGeometry()
753{
754 mHwWorkListDirty = true;
755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
758{
759 bool recomputeVisibleRegions = false;
760 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700761 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700763 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 layer->lockPageFlip(recomputeVisibleRegions);
765 }
766 return recomputeVisibleRegions;
767}
768
769void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
770{
771 const GraphicPlane& plane(graphicPlane(0));
772 const Transform& planeTransform(plane.transform());
773 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700774 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700776 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777 layer->unlockPageFlip(planeTransform, mDirtyRegion);
778 }
779}
780
Mathias Agopiana350ff92010-08-10 17:14:02 -0700781void SurfaceFlinger::handleWorkList()
782{
783 mHwWorkListDirty = false;
784 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
785 if (hwc.initCheck() == NO_ERROR) {
786 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
787 const size_t count = currentLayers.size();
788 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700789 hwc_layer_t* const cur(hwc.getLayers());
790 for (size_t i=0 ; cur && i<count ; i++) {
791 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700792 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700793 cur[i].compositionType = HWC_FRAMEBUFFER;
794 cur[i].flags |= HWC_SKIP_LAYER;
795 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700796 }
797 }
798}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700799
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800void SurfaceFlinger::handleRepaint()
801{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700802 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700803 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804
805 if (UNLIKELY(mDebugRegion)) {
806 debugFlashRegions();
807 }
808
Mathias Agopianb8a55602009-06-26 19:06:36 -0700809 // set the frame buffer
810 const DisplayHardware& hw(graphicPlane(0).displayHardware());
811 glMatrixMode(GL_MODELVIEW);
812 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813
814 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700815 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
816 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700817 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700818 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
819 // takes a rectangle, we must make sure to update that whole
820 // rectangle in that case
821 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700822 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700823 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700824 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700826 // in the BUFFER_PRESERVED case, obviously, we can update only
827 // what's needed and nothing more.
828 // NOTE: this is NOT a common case, as preserving the backbuffer
829 // is costly and usually involves copying the whole update back.
830 }
831 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700832 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700833 // We need to redraw the rectangle that will be updated
834 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700835 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700836 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700837 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700838 } else {
839 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700841 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 }
843 }
844
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700845 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 composeSurfaces(mDirtyRegion);
847
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700848 // update the swap region and clear the dirty region
849 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 mDirtyRegion.clear();
851}
852
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700853void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700855 const DisplayHardware& hw(graphicPlane(0).displayHardware());
856 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700857 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700858 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700859 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700860 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700861
Mathias Agopianf384cc32011-09-08 18:31:55 -0700862 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
863 size_t count = layers.size();
864
865 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700866 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
867 hwc.getNumLayers(), count);
868
869 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700870 if (hwc.initCheck() == NO_ERROR) {
871 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
872 }
Mathias Agopian45721772010-08-12 15:03:26 -0700873
874 /*
875 * update the per-frame h/w composer data for each layer
876 * and build the transparent region of the FB
877 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700878 for (size_t i=0 ; i<count ; i++) {
879 const sp<LayerBase>& layer(layers[i]);
880 layer->setPerFrameData(&cur[i]);
881 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700882 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700883 status_t err = hwc.prepare();
884 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700885
Mathias Agopianf384cc32011-09-08 18:31:55 -0700886 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700887 // what's happening here is tricky.
888 // we want to clear all the layers with the CLEAR_FB flags
889 // that are opaque.
890 // however, since some GPU are efficient at preserving
891 // the backbuffer, we want to take advantage of that so we do the
892 // clear only in the dirty region (other areas will be preserved
893 // on those GPUs).
894 // NOTE: on non backbuffer preserving GPU, the dirty region
895 // has already been expanded as needed, so the code is correct
896 // there too.
897 //
898 // However, the content of the framebuffer cannot be trusted when
899 // we switch to/from FB/OVERLAY, in which case we need to
900 // expand the dirty region to those areas too.
901 //
902 // Note also that there is a special case when switching from
903 // "no layers in FB" to "some layers in FB", where we need to redraw
904 // the entire FB, since some areas might contain uninitialized
905 // data.
906 //
907 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700908 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700909 // that is, we can't erase anything outside the dirty region.
910
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700911 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700912
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700913 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
914 transparent.set(hw.getBounds());
915 dirtyInOut = transparent;
916 } else {
917 for (size_t i=0 ; i<count ; i++) {
918 const sp<LayerBase>& layer(layers[i]);
919 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
920 transparent.orSelf(layer->visibleRegionScreen);
921 }
922 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
923 if (isOverlay != layer->isOverlay()) {
924 // we transitioned to/from overlay, so add this layer
925 // to the dirty region so the framebuffer can be either
926 // cleared or redrawn.
927 dirtyInOut.orSelf(layer->visibleRegionScreen);
928 }
929 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800930 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700931 // don't erase stuff outside the dirty region
932 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700933 }
934
935 /*
936 * clear the area of the FB that need to be transparent
937 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700938 if (!transparent.isEmpty()) {
939 glClearColor(0,0,0,0);
940 Region::const_iterator it = transparent.begin();
941 Region::const_iterator const end = transparent.end();
942 const int32_t height = hw.getHeight();
943 while (it != end) {
944 const Rect& r(*it++);
945 const GLint sy = height - (r.top + r.height());
946 glScissor(r.left, sy, r.width(), r.height());
947 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800948 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700949 }
950 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700951}
Mathias Agopian45721772010-08-12 15:03:26 -0700952
Mathias Agopianf384cc32011-09-08 18:31:55 -0700953void SurfaceFlinger::composeSurfaces(const Region& dirty)
954{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700955 const DisplayHardware& hw(graphicPlane(0).displayHardware());
956 HWComposer& hwc(hw.getHwComposer());
957
958 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
959 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700960 // should never happen unless the window manager has a bug
961 // draw something...
962 drawWormhole();
963 }
964
Mathias Agopian45721772010-08-12 15:03:26 -0700965 /*
966 * and then, render the layers targeted at the framebuffer
967 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700968 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700969 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
970 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700971 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700972 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700973 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700974 }
975 const sp<LayerBase>& layer(layers[i]);
976 const Region clip(dirty.intersect(layer->visibleRegionScreen));
977 if (!clip.isEmpty()) {
978 layer->draw(clip);
979 }
980 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981}
982
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983void SurfaceFlinger::debugFlashRegions()
984{
Mathias Agopian0a917752010-06-14 21:20:00 -0700985 const DisplayHardware& hw(graphicPlane(0).displayHardware());
986 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700987 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700988 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700989 return;
990 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700991
Mathias Agopian0a917752010-06-14 21:20:00 -0700992 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
993 (flags & DisplayHardware::BUFFER_PRESERVED))) {
994 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
995 mDirtyRegion.bounds() : hw.bounds());
996 composeSurfaces(repaint);
997 }
998
Mathias Agopianc492e672011-10-18 14:49:27 -0700999 glDisable(GL_TEXTURE_EXTERNAL_OES);
1000 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 glDisable(GL_SCISSOR_TEST);
1003
Mathias Agopian0926f502009-05-04 14:17:04 -07001004 static int toggle = 0;
1005 toggle = 1 - toggle;
1006 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001007 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001008 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -07001009 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -07001010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Mathias Agopian20f68782009-05-11 00:03:41 -07001012 Region::const_iterator it = mDirtyRegion.begin();
1013 Region::const_iterator const end = mDirtyRegion.end();
1014 while (it != end) {
1015 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001017 { r.left, height - r.top },
1018 { r.left, height - r.bottom },
1019 { r.right, height - r.bottom },
1020 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 };
1022 glVertexPointer(2, GL_FLOAT, 0, vertices);
1023 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1024 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001025
Mathias Agopian0656a682011-09-20 17:22:44 -07001026 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027
1028 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001029 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030
1031 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032}
1033
1034void SurfaceFlinger::drawWormhole() const
1035{
1036 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1037 if (region.isEmpty())
1038 return;
1039
1040 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1041 const int32_t width = hw.getWidth();
1042 const int32_t height = hw.getHeight();
1043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001045 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001046 Region::const_iterator it = region.begin();
1047 Region::const_iterator const end = region.end();
1048 while (it != end) {
1049 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 const GLint sy = height - (r.top + r.height());
1051 glScissor(r.left, sy, r.width(), r.height());
1052 glClear(GL_COLOR_BUFFER_BIT);
1053 }
1054 } else {
1055 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1056 { width, height }, { 0, height } };
1057 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 glVertexPointer(2, GL_SHORT, 0, vertices);
1060 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1061 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001062
1063 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 glEnable(GL_TEXTURE_2D);
1065 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1066 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1067 glMatrixMode(GL_TEXTURE);
1068 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001069
1070 glDisable(GL_BLEND);
1071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001073 Region::const_iterator it = region.begin();
1074 Region::const_iterator const end = region.end();
1075 while (it != end) {
1076 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 const GLint sy = height - (r.top + r.height());
1078 glScissor(r.left, sy, r.width(), r.height());
1079 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1080 }
1081 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001082 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001083 glLoadIdentity();
1084 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 }
1086}
1087
1088void SurfaceFlinger::debugShowFPS() const
1089{
1090 static int mFrameCount;
1091 static int mLastFrameCount = 0;
1092 static nsecs_t mLastFpsTime = 0;
1093 static float mFps = 0;
1094 mFrameCount++;
1095 nsecs_t now = systemTime();
1096 nsecs_t diff = now - mLastFpsTime;
1097 if (diff > ms2ns(250)) {
1098 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1099 mLastFpsTime = now;
1100 mLastFrameCount = mFrameCount;
1101 }
1102 // XXX: mFPS has the value we want
1103 }
1104
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001105status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106{
1107 Mutex::Autolock _l(mStateLock);
1108 addLayer_l(layer);
1109 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1110 return NO_ERROR;
1111}
1112
Mathias Agopian96f08192010-06-02 23:28:45 -07001113status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1114{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001115 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001116 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1117}
1118
1119ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1120 const sp<LayerBaseClient>& lbc)
1121{
Mathias Agopian96f08192010-06-02 23:28:45 -07001122 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001123 size_t name = client->attachLayer(lbc);
1124
1125 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001126
1127 // add this layer to the current state list
1128 addLayer_l(lbc);
1129
Mathias Agopian4f113742011-05-03 16:21:41 -07001130 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001131}
1132
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001133status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134{
1135 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001136 status_t err = purgatorizeLayer_l(layer);
1137 if (err == NO_ERROR)
1138 setTransactionFlags(eTransactionNeeded);
1139 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140}
1141
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001142status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001144 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1145 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001146 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001147 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1149 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001150 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 return NO_ERROR;
1152 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001153 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154}
1155
Mathias Agopian9a112062009-04-17 19:36:26 -07001156status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1157{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001158 // First add the layer to the purgatory list, which makes sure it won't
1159 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001160 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001161 if (err >= 0) {
1162 mLayerPurgatory.add(layerBase);
1163 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001164
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001165 layerBase->onRemoved();
1166
Mathias Agopian3d579642009-06-04 18:46:21 -07001167 // it's possible that we don't find a layer, because it might
1168 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001169 // from the user because there is a race between Client::destroySurface(),
1170 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001171 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1172}
1173
Mathias Agopian96f08192010-06-02 23:28:45 -07001174status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175{
Mathias Agopian96f08192010-06-02 23:28:45 -07001176 layer->forceVisibilityTransaction();
1177 setTransactionFlags(eTraversalNeeded);
1178 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179}
1180
Mathias Agopiandea20b12011-05-03 17:04:02 -07001181uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1182{
1183 return android_atomic_release_load(&mTransactionFlags);
1184}
1185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1187{
1188 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1189}
1190
Mathias Agopianbb641242010-05-18 17:06:55 -07001191uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192{
1193 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1194 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001195 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 }
1197 return old;
1198}
1199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001201void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001202 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001203 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001204
Jamie Gennis28378392011-10-12 17:39:00 -07001205 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001206 if (mCurrentState.orientation != orientation) {
1207 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1208 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001209 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001210 } else if (orientation != eOrientationUnchanged) {
1211 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1212 orientation);
1213 }
1214 }
1215
Mathias Agopian698c0872011-06-28 19:09:31 -07001216 const size_t count = state.size();
1217 for (size_t i=0 ; i<count ; i++) {
1218 const ComposerState& s(state[i]);
1219 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001220 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001221 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001222
Mathias Agopian386aa982011-11-07 21:58:03 -08001223 if (transactionFlags) {
1224 // this triggers the transaction
1225 setTransactionFlags(transactionFlags);
1226
1227 // if this is a synchronous transaction, wait for it to take effect
1228 // before returning.
1229 if (flags & eSynchronous) {
1230 mTransationPending = true;
1231 }
1232 while (mTransationPending) {
1233 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1234 if (CC_UNLIKELY(err != NO_ERROR)) {
1235 // just in case something goes wrong in SF, return to the
1236 // called after a few seconds.
1237 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1238 mTransationPending = false;
1239 break;
1240 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001241 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 }
1243}
1244
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001245int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001246 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247{
1248 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1249 return BAD_VALUE;
1250
1251 Mutex::Autolock _l(mStateLock);
1252 if (mCurrentState.orientation != orientation) {
1253 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001254 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 mCurrentState.orientation = orientation;
1256 setTransactionFlags(eTransactionNeeded);
1257 mTransactionCV.wait(mStateLock);
1258 } else {
1259 orientation = BAD_VALUE;
1260 }
1261 }
1262 return orientation;
1263}
1264
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001265sp<ISurface> SurfaceFlinger::createSurface(
1266 ISurfaceComposerClient::surface_data_t* params,
1267 const String8& name,
1268 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1270 uint32_t flags)
1271{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001272 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001273 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001274
1275 if (int32_t(w|h) < 0) {
1276 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1277 int(w), int(h));
1278 return surfaceHandle;
1279 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001282 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 switch (flags & eFXSurfaceMask) {
1284 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001285 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1286 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 break;
1288 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001289 // for now we treat Blur as Dim, until we can implement it
1290 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001292 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001294 case eFXSurfaceScreenshot:
1295 layer = createScreenshotSurface(client, d, w, h, flags);
1296 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 }
1298
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001299 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001300 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001301 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001302 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001305 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001306 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001307 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001308 if (normalLayer != 0) {
1309 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001310 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001311 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001312 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001313
Mathias Agopian96f08192010-06-02 23:28:45 -07001314 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 }
1316
1317 return surfaceHandle;
1318}
1319
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001320sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001321 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001322 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001323 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324{
1325 // initialize the surfaces
1326 switch (format) { // TODO: take h/w into account
1327 case PIXEL_FORMAT_TRANSPARENT:
1328 case PIXEL_FORMAT_TRANSLUCENT:
1329 format = PIXEL_FORMAT_RGBA_8888;
1330 break;
1331 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001332#ifdef NO_RGBX_8888
1333 format = PIXEL_FORMAT_RGB_565;
1334#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001335 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001336#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 break;
1338 }
1339
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001340#ifdef NO_RGBX_8888
1341 if (format == PIXEL_FORMAT_RGBX_8888)
1342 format = PIXEL_FORMAT_RGBA_8888;
1343#endif
1344
Mathias Agopian96f08192010-06-02 23:28:45 -07001345 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001346 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001347 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001349 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 }
1351 return layer;
1352}
1353
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001354sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001355 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001356 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357{
Mathias Agopian96f08192010-06-02 23:28:45 -07001358 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001359 return layer;
1360}
1361
1362sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1363 const sp<Client>& client, DisplayID display,
1364 uint32_t w, uint32_t h, uint32_t flags)
1365{
1366 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 return layer;
1368}
1369
Mathias Agopian96f08192010-06-02 23:28:45 -07001370status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371{
Mathias Agopian9a112062009-04-17 19:36:26 -07001372 /*
1373 * called by the window manager, when a surface should be marked for
1374 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001375 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001376 * The surface is removed from the current and drawing lists, but placed
1377 * in the purgatory queue, so it's not destroyed right-away (we need
1378 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001379 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001380
Mathias Agopian48d819a2009-09-10 19:41:18 -07001381 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001382 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001383 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001384 if (layer != 0) {
1385 err = purgatorizeLayer_l(layer);
1386 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001387 setTransactionFlags(eTransactionNeeded);
1388 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001389 }
1390 return err;
1391}
1392
Mathias Agopianca4d3602011-05-19 15:38:14 -07001393status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001394{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001395 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001396 status_t err = NO_ERROR;
1397 sp<LayerBaseClient> l(layer.promote());
1398 if (l != NULL) {
1399 Mutex::Autolock _l(mStateLock);
1400 err = removeLayer_l(l);
1401 if (err == NAME_NOT_FOUND) {
1402 // The surface wasn't in the current list, which means it was
1403 // removed already, which means it is in the purgatory,
1404 // and need to be removed from there.
1405 ssize_t idx = mLayerPurgatory.remove(l);
1406 LOGE_IF(idx < 0,
1407 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001408 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001409 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1410 "error removing layer=%p (%s)", l.get(), strerror(-err));
1411 }
1412 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413}
1414
Mathias Agopian698c0872011-06-28 19:09:31 -07001415uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001416 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001417 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001420 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1421 if (layer != 0) {
1422 const uint32_t what = s.what;
1423 if (what & ePositionChanged) {
1424 if (layer->setPosition(s.x, s.y))
1425 flags |= eTraversalNeeded;
1426 }
1427 if (what & eLayerChanged) {
1428 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1429 if (layer->setLayer(s.z)) {
1430 mCurrentState.layersSortedByZ.removeAt(idx);
1431 mCurrentState.layersSortedByZ.add(layer);
1432 // we need traversal (state changed)
1433 // AND transaction (list changed)
1434 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
1436 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001437 if (what & eSizeChanged) {
1438 if (layer->setSize(s.w, s.h)) {
1439 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001440 }
1441 }
1442 if (what & eAlphaChanged) {
1443 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eMatrixChanged) {
1447 if (layer->setMatrix(s.matrix))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eTransparentRegionChanged) {
1451 if (layer->setTransparentRegionHint(s.transparentRegion))
1452 flags |= eTraversalNeeded;
1453 }
1454 if (what & eVisibilityChanged) {
1455 if (layer->setFlags(s.flags, s.mask))
1456 flags |= eTraversalNeeded;
1457 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001459 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460}
1461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462void SurfaceFlinger::screenReleased(int dpy)
1463{
1464 // this may be called by a signal handler, we can't do too much in here
1465 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1466 signalEvent();
1467}
1468
1469void SurfaceFlinger::screenAcquired(int dpy)
1470{
1471 // this may be called by a signal handler, we can't do too much in here
1472 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1473 signalEvent();
1474}
1475
1476status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1477{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001478 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 char buffer[SIZE];
1480 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001481
1482 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 snprintf(buffer, SIZE, "Permission Denial: "
1484 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1485 IPCThreadState::self()->getCallingPid(),
1486 IPCThreadState::self()->getCallingUid());
1487 result.append(buffer);
1488 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001489
1490 // figure out if we're stuck somewhere
1491 const nsecs_t now = systemTime();
1492 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1493 const nsecs_t inTransaction(mDebugInTransaction);
1494 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1495 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1496
1497 // Try to get the main lock, but don't insist if we can't
1498 // (this would indicate SF is stuck, but we want to be able to
1499 // print something in dumpsys).
1500 int retry = 3;
1501 while (mStateLock.tryLock()<0 && --retry>=0) {
1502 usleep(1000000);
1503 }
1504 const bool locked(retry >= 0);
1505 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001506 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001507 "SurfaceFlinger appears to be unresponsive, "
1508 "dumping anyways (no locks held)\n");
1509 result.append(buffer);
1510 }
1511
Mathias Agopian48b888a2011-01-19 16:15:53 -08001512 /*
1513 * Dump the visible layer list
1514 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1516 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001517 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1518 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001520 const sp<LayerBase>& layer(currentLayers[i]);
1521 layer->dump(result, buffer, SIZE);
1522 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001523 s.transparentRegion.dump(result, "transparentRegion");
1524 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1525 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1526 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001527
Mathias Agopian48b888a2011-01-19 16:15:53 -08001528 /*
1529 * Dump the layers in the purgatory
1530 */
1531
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001532 const size_t purgatorySize = mLayerPurgatory.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001533 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1534 result.append(buffer);
1535 for (size_t i=0 ; i<purgatorySize ; i++) {
1536 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1537 layer->shortDump(result, buffer, SIZE);
1538 }
1539
1540 /*
1541 * Dump SurfaceFlinger global state
1542 */
1543
Mathias Agopianad701862011-07-14 18:01:49 -07001544 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001545 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001546
1547 const GLExtensions& extensions(GLExtensions::getInstance());
1548 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1549 extensions.getVendor(),
1550 extensions.getRenderer(),
1551 extensions.getVersion());
1552 result.append(buffer);
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001553
1554 snprintf(buffer, SIZE, "EGL : %s\n",
1555 eglQueryString(graphicPlane(0).getEGLDisplay(),
1556 EGL_VERSION_HW_ANDROID));
1557 result.append(buffer);
1558
Mathias Agopianad701862011-07-14 18:01:49 -07001559 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1560 result.append(buffer);
1561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 mWormholeRegion.dump(result, "WormholeRegion");
1563 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1564 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001565 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 mCurrentState.orientation, hw.canDraw());
1567 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001568 snprintf(buffer, SIZE,
1569 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001570 " last transaction time : %f us\n"
1571 " refresh-rate : %f fps\n"
1572 " x-dpi : %f\n"
1573 " y-dpi : %f\n",
1574 mLastSwapBufferTime/1000.0,
1575 mLastTransactionTime/1000.0,
1576 hw.getRefreshRate(),
1577 hw.getDpiX(),
1578 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001579 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001580
Mathias Agopian9795c422009-08-26 16:36:26 -07001581 if (inSwapBuffersDuration || !locked) {
1582 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1583 inSwapBuffersDuration/1000.0);
1584 result.append(buffer);
1585 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001586
Mathias Agopian9795c422009-08-26 16:36:26 -07001587 if (inTransactionDuration || !locked) {
1588 snprintf(buffer, SIZE, " transaction time: %f us\n",
1589 inTransactionDuration/1000.0);
1590 result.append(buffer);
1591 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001592
Mathias Agopian48b888a2011-01-19 16:15:53 -08001593 /*
1594 * Dump HWComposer state
1595 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001596 HWComposer& hwc(hw.getHwComposer());
1597 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1598 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001599 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001600 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001601 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001602
Mathias Agopian48b888a2011-01-19 16:15:53 -08001603 /*
1604 * Dump gralloc state
1605 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001606 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001607 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001608 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001609
1610 if (locked) {
1611 mStateLock.unlock();
1612 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 }
1614 write(fd, result.string(), result.size());
1615 return NO_ERROR;
1616}
1617
1618status_t SurfaceFlinger::onTransact(
1619 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1620{
1621 switch (code) {
1622 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001623 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001626 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001627 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 {
1629 // codes that require permission check
1630 IPCThreadState* ipc = IPCThreadState::self();
1631 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001632 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001633 if ((uid != AID_GRAPHICS) &&
1634 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001635 LOGE("Permission Denial: "
1636 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1637 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001639 break;
1640 }
1641 case CAPTURE_SCREEN:
1642 {
1643 // codes that require permission check
1644 IPCThreadState* ipc = IPCThreadState::self();
1645 const int pid = ipc->getCallingPid();
1646 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001647 if ((uid != AID_GRAPHICS) &&
1648 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001649 LOGE("Permission Denial: "
1650 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1651 return PERMISSION_DENIED;
1652 }
1653 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 }
1655 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1658 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001659 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001660 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001661 IPCThreadState* ipc = IPCThreadState::self();
1662 const int pid = ipc->getCallingPid();
1663 const int uid = ipc->getCallingUid();
1664 LOGE("Permission Denial: "
1665 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 return PERMISSION_DENIED;
1667 }
1668 int n;
1669 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001670 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001671 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 return NO_ERROR;
1673 case 1002: // SHOW_UPDATES
1674 n = data.readInt32();
1675 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001676 invalidateHwcGeometry();
1677 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678 return NO_ERROR;
1679 case 1003: // SHOW_BACKGROUND
1680 n = data.readInt32();
1681 mDebugBackground = n ? 1 : 0;
1682 return NO_ERROR;
1683 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001684 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001685 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001687 case 1005:{ // force transaction
1688 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1689 return NO_ERROR;
1690 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001691 case 1006:{ // enable/disable GraphicLog
1692 int enabled = data.readInt32();
1693 GraphicLog::getInstance().setEnabled(enabled);
1694 return NO_ERROR;
1695 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001696 case 1008: // toggle use of hw composer
1697 n = data.readInt32();
1698 mDebugDisableHWC = n ? 1 : 0;
1699 invalidateHwcGeometry();
1700 repaintEverything();
1701 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001702 case 1009: // toggle use of transform hint
1703 n = data.readInt32();
1704 mDebugDisableTransformHint = n ? 1 : 0;
1705 invalidateHwcGeometry();
1706 repaintEverything();
1707 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001709 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 reply->writeInt32(0);
1711 reply->writeInt32(mDebugRegion);
1712 reply->writeInt32(mDebugBackground);
1713 return NO_ERROR;
1714 case 1013: {
1715 Mutex::Autolock _l(mStateLock);
1716 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1717 reply->writeInt32(hw.getPageFlipCount());
1718 }
1719 return NO_ERROR;
1720 }
1721 }
1722 return err;
1723}
1724
Mathias Agopian53331da2011-08-22 21:44:41 -07001725void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001726 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001727 const Rect bounds(hw.getBounds());
1728 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001729 signalEvent();
1730}
1731
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001732void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1733 Mutex::Autolock _l(mInvalidateLock);
1734 mInvalidateRegion = reg;
1735}
1736
1737Region SurfaceFlinger::getAndClearInvalidateRegion() {
1738 Mutex::Autolock _l(mInvalidateLock);
1739 Region reg(mInvalidateRegion);
1740 mInvalidateRegion.clear();
1741 return reg;
1742}
1743
Mathias Agopian59119e62010-10-11 12:37:43 -07001744// ---------------------------------------------------------------------------
1745
Mathias Agopian118d0242011-10-13 16:02:48 -07001746status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1747 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1748{
1749 Mutex::Autolock _l(mStateLock);
1750 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1751}
1752
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001753status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1754 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001755{
Mathias Agopian59119e62010-10-11 12:37:43 -07001756 if (!GLExtensions::getInstance().haveFramebufferObject())
1757 return INVALID_OPERATION;
1758
1759 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001760 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001761 const uint32_t hw_w = hw.getWidth();
1762 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001763 GLfloat u = 1;
1764 GLfloat v = 1;
1765
1766 // make sure to clear all GL error flags
1767 while ( glGetError() != GL_NO_ERROR ) ;
1768
1769 // create a FBO
1770 GLuint name, tname;
1771 glGenTextures(1, &tname);
1772 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001773 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1774 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001775 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001776 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001777 GLint tw = (2 << (31 - clz(hw_w)));
1778 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001779 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1780 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001781 u = GLfloat(hw_w) / tw;
1782 v = GLfloat(hw_h) / th;
1783 }
1784 glGenFramebuffersOES(1, &name);
1785 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001786 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1787 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001788
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001789 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001790 glDisable(GL_TEXTURE_EXTERNAL_OES);
1791 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001792 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001793 glClearColor(0,0,0,1);
1794 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001795 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001796 glMatrixMode(GL_MODELVIEW);
1797 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001798 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1799 const size_t count = layers.size();
1800 for (size_t i=0 ; i<count ; ++i) {
1801 const sp<LayerBase>& layer(layers[i]);
1802 layer->drawForSreenShot();
1803 }
1804
Mathias Agopian118d0242011-10-13 16:02:48 -07001805 hw.compositionComplete();
1806
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001807 // back to main framebuffer
1808 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1809 glDisable(GL_SCISSOR_TEST);
1810 glDeleteFramebuffersOES(1, &name);
1811
1812 *textureName = tname;
1813 *uOut = u;
1814 *vOut = v;
1815 return NO_ERROR;
1816}
1817
1818// ---------------------------------------------------------------------------
1819
1820status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1821{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001822 // get screen geometry
1823 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1824 const uint32_t hw_w = hw.getWidth();
1825 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001826 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001827
1828 GLfloat u, v;
1829 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001830 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001831 if (result != NO_ERROR) {
1832 return result;
1833 }
1834
1835 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001836 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001837 glBindTexture(GL_TEXTURE_2D, tname);
1838 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1839 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1840 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1841 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1842 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1843 glVertexPointer(2, GL_FLOAT, 0, vtx);
1844
Mathias Agopianffcf4652011-07-07 17:30:31 -07001845 /*
1846 * Texture coordinate mapping
1847 *
1848 * u
1849 * 1 +----------+---+
1850 * | | | | image is inverted
1851 * | V | | w.r.t. the texture
1852 * 1-v +----------+ | coordinates
1853 * | |
1854 * | |
1855 * | |
1856 * 0 +--------------+
1857 * 0 1
1858 *
1859 */
1860
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001861 class s_curve_interpolator {
1862 const float nbFrames, s, v;
1863 public:
1864 s_curve_interpolator(int nbFrames, float s)
1865 : nbFrames(1.0f / (nbFrames-1)), s(s),
1866 v(1.0f + expf(-s + 0.5f*s)) {
1867 }
1868 float operator()(int f) {
1869 const float x = f * nbFrames;
1870 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1871 }
1872 };
1873
1874 class v_stretch {
1875 const GLfloat hw_w, hw_h;
1876 public:
1877 v_stretch(uint32_t hw_w, uint32_t hw_h)
1878 : hw_w(hw_w), hw_h(hw_h) {
1879 }
1880 void operator()(GLfloat* vtx, float v) {
1881 const GLfloat w = hw_w + (hw_w * v);
1882 const GLfloat h = hw_h - (hw_h * v);
1883 const GLfloat x = (hw_w - w) * 0.5f;
1884 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001885 vtx[0] = x; vtx[1] = y;
1886 vtx[2] = x; vtx[3] = y + h;
1887 vtx[4] = x + w; vtx[5] = y + h;
1888 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001889 }
1890 };
1891
1892 class h_stretch {
1893 const GLfloat hw_w, hw_h;
1894 public:
1895 h_stretch(uint32_t hw_w, uint32_t hw_h)
1896 : hw_w(hw_w), hw_h(hw_h) {
1897 }
1898 void operator()(GLfloat* vtx, float v) {
1899 const GLfloat w = hw_w - (hw_w * v);
1900 const GLfloat h = 1.0f;
1901 const GLfloat x = (hw_w - w) * 0.5f;
1902 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001903 vtx[0] = x; vtx[1] = y;
1904 vtx[2] = x; vtx[3] = y + h;
1905 vtx[4] = x + w; vtx[5] = y + h;
1906 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001907 }
1908 };
1909
1910 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001911 char value[PROPERTY_VALUE_MAX];
1912 property_get("debug.sf.electron_frames", value, "24");
1913 int nbFrames = (atoi(value) + 1) >> 1;
1914 if (nbFrames <= 0) // just in case
1915 nbFrames = 24;
1916
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001917 s_curve_interpolator itr(nbFrames, 7.5f);
1918 s_curve_interpolator itg(nbFrames, 8.0f);
1919 s_curve_interpolator itb(nbFrames, 8.5f);
1920
1921 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001922
1923 glMatrixMode(GL_TEXTURE);
1924 glLoadIdentity();
1925 glMatrixMode(GL_MODELVIEW);
1926 glLoadIdentity();
1927
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001928 glEnable(GL_BLEND);
1929 glBlendFunc(GL_ONE, GL_ONE);
1930 for (int i=0 ; i<nbFrames ; i++) {
1931 float x, y, w, h;
1932 const float vr = itr(i);
1933 const float vg = itg(i);
1934 const float vb = itb(i);
1935
1936 // clear screen
1937 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001938 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001939 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001940
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001941 // draw the red plane
1942 vverts(vtx, vr);
1943 glColorMask(1,0,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 green plane
1947 vverts(vtx, vg);
1948 glColorMask(0,1,0,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 blue plane
1952 vverts(vtx, vb);
1953 glColorMask(0,0,1,1);
1954 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001955
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001956 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001957 glDisable(GL_TEXTURE_2D);
1958 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001959 glColor4f(vg, vg, vg, 1);
1960 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1961 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001962 }
1963
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001964 h_stretch hverts(hw_w, hw_h);
1965 glDisable(GL_BLEND);
1966 glDisable(GL_TEXTURE_2D);
1967 glColorMask(1,1,1,1);
1968 for (int i=0 ; i<nbFrames ; i++) {
1969 const float v = itg(i);
1970 hverts(vtx, v);
1971 glClear(GL_COLOR_BUFFER_BIT);
1972 glColor4f(1-v, 1-v, 1-v, 1);
1973 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1974 hw.flip(screenBounds);
1975 }
1976
1977 glColorMask(1,1,1,1);
1978 glEnable(GL_SCISSOR_TEST);
1979 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1980 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001981 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07001982 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001983 return NO_ERROR;
1984}
1985
1986status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1987{
1988 status_t result = PERMISSION_DENIED;
1989
1990 if (!GLExtensions::getInstance().haveFramebufferObject())
1991 return INVALID_OPERATION;
1992
1993
1994 // get screen geometry
1995 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1996 const uint32_t hw_w = hw.getWidth();
1997 const uint32_t hw_h = hw.getHeight();
1998 const Region screenBounds(hw.bounds());
1999
2000 GLfloat u, v;
2001 GLuint tname;
2002 result = renderScreenToTextureLocked(0, &tname, &u, &v);
2003 if (result != NO_ERROR) {
2004 return result;
2005 }
2006
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002007 GLfloat vtx[8];
2008 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002009 glBindTexture(GL_TEXTURE_2D, tname);
2010 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2011 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2012 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2013 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
2014 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2015 glVertexPointer(2, GL_FLOAT, 0, vtx);
2016
2017 class s_curve_interpolator {
2018 const float nbFrames, s, v;
2019 public:
2020 s_curve_interpolator(int nbFrames, float s)
2021 : nbFrames(1.0f / (nbFrames-1)), s(s),
2022 v(1.0f + expf(-s + 0.5f*s)) {
2023 }
2024 float operator()(int f) {
2025 const float x = f * nbFrames;
2026 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2027 }
2028 };
2029
2030 class v_stretch {
2031 const GLfloat hw_w, hw_h;
2032 public:
2033 v_stretch(uint32_t hw_w, uint32_t hw_h)
2034 : hw_w(hw_w), hw_h(hw_h) {
2035 }
2036 void operator()(GLfloat* vtx, float v) {
2037 const GLfloat w = hw_w + (hw_w * v);
2038 const GLfloat h = hw_h - (hw_h * v);
2039 const GLfloat x = (hw_w - w) * 0.5f;
2040 const GLfloat y = (hw_h - h) * 0.5f;
2041 vtx[0] = x; vtx[1] = y;
2042 vtx[2] = x; vtx[3] = y + h;
2043 vtx[4] = x + w; vtx[5] = y + h;
2044 vtx[6] = x + w; vtx[7] = y;
2045 }
2046 };
2047
2048 class h_stretch {
2049 const GLfloat hw_w, hw_h;
2050 public:
2051 h_stretch(uint32_t hw_w, uint32_t hw_h)
2052 : hw_w(hw_w), hw_h(hw_h) {
2053 }
2054 void operator()(GLfloat* vtx, float v) {
2055 const GLfloat w = hw_w - (hw_w * v);
2056 const GLfloat h = 1.0f;
2057 const GLfloat x = (hw_w - w) * 0.5f;
2058 const GLfloat y = (hw_h - h) * 0.5f;
2059 vtx[0] = x; vtx[1] = y;
2060 vtx[2] = x; vtx[3] = y + h;
2061 vtx[4] = x + w; vtx[5] = y + h;
2062 vtx[6] = x + w; vtx[7] = y;
2063 }
2064 };
2065
Mathias Agopiana6546e52010-10-14 12:33:07 -07002066 // the full animation is 12 frames
2067 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002068 s_curve_interpolator itr(nbFrames, 7.5f);
2069 s_curve_interpolator itg(nbFrames, 8.0f);
2070 s_curve_interpolator itb(nbFrames, 8.5f);
2071
2072 h_stretch hverts(hw_w, hw_h);
2073 glDisable(GL_BLEND);
2074 glDisable(GL_TEXTURE_2D);
2075 glColorMask(1,1,1,1);
2076 for (int i=nbFrames-1 ; i>=0 ; i--) {
2077 const float v = itg(i);
2078 hverts(vtx, v);
2079 glClear(GL_COLOR_BUFFER_BIT);
2080 glColor4f(1-v, 1-v, 1-v, 1);
2081 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2082 hw.flip(screenBounds);
2083 }
2084
Mathias Agopiana6546e52010-10-14 12:33:07 -07002085 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002086 v_stretch vverts(hw_w, hw_h);
2087 glEnable(GL_BLEND);
2088 glBlendFunc(GL_ONE, GL_ONE);
2089 for (int i=nbFrames-1 ; i>=0 ; i--) {
2090 float x, y, w, h;
2091 const float vr = itr(i);
2092 const float vg = itg(i);
2093 const float vb = itb(i);
2094
2095 // clear screen
2096 glColorMask(1,1,1,1);
2097 glClear(GL_COLOR_BUFFER_BIT);
2098 glEnable(GL_TEXTURE_2D);
2099
2100 // draw the red plane
2101 vverts(vtx, vr);
2102 glColorMask(1,0,0,1);
2103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2104
2105 // draw the green plane
2106 vverts(vtx, vg);
2107 glColorMask(0,1,0,1);
2108 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2109
2110 // draw the blue plane
2111 vverts(vtx, vb);
2112 glColorMask(0,0,1,1);
2113 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2114
2115 hw.flip(screenBounds);
2116 }
2117
2118 glColorMask(1,1,1,1);
2119 glEnable(GL_SCISSOR_TEST);
2120 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002121 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002122 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002123 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002124
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002125 return NO_ERROR;
2126}
2127
2128// ---------------------------------------------------------------------------
2129
Mathias Agopianabd671a2010-10-14 14:54:06 -07002130status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002131{
2132 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2133 if (!hw.canDraw()) {
2134 // we're already off
2135 return NO_ERROR;
2136 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002137
2138 // turn off hwc while we're doing the animation
2139 hw.getHwComposer().disable();
2140 // and make sure to turn it back on (if needed) next time we compose
2141 invalidateHwcGeometry();
2142
Mathias Agopianabd671a2010-10-14 14:54:06 -07002143 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2144 electronBeamOffAnimationImplLocked();
2145 }
2146
2147 // always clear the whole screen at the end of the animation
2148 glClearColor(0,0,0,1);
2149 glDisable(GL_SCISSOR_TEST);
2150 glClear(GL_COLOR_BUFFER_BIT);
2151 glEnable(GL_SCISSOR_TEST);
2152 hw.flip( Region(hw.bounds()) );
2153
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002154 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002155}
2156
2157status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2158{
Mathias Agopian59119e62010-10-11 12:37:43 -07002159 class MessageTurnElectronBeamOff : public MessageBase {
2160 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002161 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002162 status_t result;
2163 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002164 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2165 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002166 }
2167 status_t getResult() const {
2168 return result;
2169 }
2170 virtual bool handler() {
2171 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002172 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002173 return true;
2174 }
2175 };
2176
Mathias Agopianabd671a2010-10-14 14:54:06 -07002177 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002178 status_t res = postMessageSync(msg);
2179 if (res == NO_ERROR) {
2180 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002181
2182 // work-around: when the power-manager calls us we activate the
2183 // animation. eventually, the "on" animation will be called
2184 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002185 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002186 }
2187 return res;
2188}
2189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002190// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002191
Mathias Agopianabd671a2010-10-14 14:54:06 -07002192status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002193{
2194 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2195 if (hw.canDraw()) {
2196 // we're already on
2197 return NO_ERROR;
2198 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002199 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2200 electronBeamOnAnimationImplLocked();
2201 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002202
2203 // make sure to redraw the whole screen when the animation is done
2204 mDirtyRegion.set(hw.bounds());
2205 signalEvent();
2206
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002207 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002208}
2209
2210status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2211{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212 class MessageTurnElectronBeamOn : public MessageBase {
2213 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002214 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002215 status_t result;
2216 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002217 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2218 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002219 }
2220 status_t getResult() const {
2221 return result;
2222 }
2223 virtual bool handler() {
2224 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002225 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002226 return true;
2227 }
2228 };
2229
Mathias Agopianabd671a2010-10-14 14:54:06 -07002230 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002231 return NO_ERROR;
2232}
2233
2234// ---------------------------------------------------------------------------
2235
Mathias Agopian74c40c02010-09-29 13:02:36 -07002236status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2237 sp<IMemoryHeap>* heap,
2238 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002239 uint32_t sw, uint32_t sh,
2240 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002241{
2242 status_t result = PERMISSION_DENIED;
2243
2244 // only one display supported for now
2245 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2246 return BAD_VALUE;
2247
2248 if (!GLExtensions::getInstance().haveFramebufferObject())
2249 return INVALID_OPERATION;
2250
2251 // get screen geometry
2252 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2253 const uint32_t hw_w = hw.getWidth();
2254 const uint32_t hw_h = hw.getHeight();
2255
2256 if ((sw > hw_w) || (sh > hw_h))
2257 return BAD_VALUE;
2258
2259 sw = (!sw) ? hw_w : sw;
2260 sh = (!sh) ? hw_h : sh;
2261 const size_t size = sw * sh * 4;
2262
Mathias Agopian1c71a472011-03-02 18:45:50 -08002263 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2264 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002265
Mathias Agopian74c40c02010-09-29 13:02:36 -07002266 // make sure to clear all GL error flags
2267 while ( glGetError() != GL_NO_ERROR ) ;
2268
2269 // create a FBO
2270 GLuint name, tname;
2271 glGenRenderbuffersOES(1, &tname);
2272 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2273 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2274 glGenFramebuffersOES(1, &name);
2275 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2276 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2277 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2278
2279 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002280
Mathias Agopian74c40c02010-09-29 13:02:36 -07002281 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2282
2283 // invert everything, b/c glReadPixel() below will invert the FB
2284 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002285 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002286 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002287 glMatrixMode(GL_PROJECTION);
2288 glPushMatrix();
2289 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002290 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002291 glMatrixMode(GL_MODELVIEW);
2292
2293 // redraw the screen entirely...
2294 glClearColor(0,0,0,1);
2295 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002296
Jamie Gennis9575f602011-10-07 14:51:16 -07002297 const LayerVector& layers(mDrawingState.layersSortedByZ);
2298 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002299 for (size_t i=0 ; i<count ; ++i) {
2300 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002301 const uint32_t flags = layer->drawingState().flags;
2302 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2303 const uint32_t z = layer->drawingState().z;
2304 if (z >= minLayerZ && z <= maxLayerZ) {
2305 layer->drawForSreenShot();
2306 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002307 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002308 }
2309
2310 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002311 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002312 glScissor(0, 0, sw, sh);
2313
2314 // check for errors and return screen capture
2315 if (glGetError() != GL_NO_ERROR) {
2316 // error while rendering
2317 result = INVALID_OPERATION;
2318 } else {
2319 // allocate shared memory large enough to hold the
2320 // screen capture
2321 sp<MemoryHeapBase> base(
2322 new MemoryHeapBase(size, 0, "screen-capture") );
2323 void* const ptr = base->getBase();
2324 if (ptr) {
2325 // capture the screen with glReadPixels()
2326 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2327 if (glGetError() == GL_NO_ERROR) {
2328 *heap = base;
2329 *w = sw;
2330 *h = sh;
2331 *f = PIXEL_FORMAT_RGBA_8888;
2332 result = NO_ERROR;
2333 }
2334 } else {
2335 result = NO_MEMORY;
2336 }
2337 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002338 glEnable(GL_SCISSOR_TEST);
2339 glViewport(0, 0, hw_w, hw_h);
2340 glMatrixMode(GL_PROJECTION);
2341 glPopMatrix();
2342 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 } else {
2344 result = BAD_VALUE;
2345 }
2346
2347 // release FBO resources
2348 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2349 glDeleteRenderbuffersOES(1, &tname);
2350 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002351
2352 hw.compositionComplete();
2353
Mathias Agopian1c71a472011-03-02 18:45:50 -08002354 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002355
Mathias Agopian74c40c02010-09-29 13:02:36 -07002356 return result;
2357}
2358
2359
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002360status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2361 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002362 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002363 uint32_t sw, uint32_t sh,
2364 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002365{
2366 // only one display supported for now
2367 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2368 return BAD_VALUE;
2369
2370 if (!GLExtensions::getInstance().haveFramebufferObject())
2371 return INVALID_OPERATION;
2372
2373 class MessageCaptureScreen : public MessageBase {
2374 SurfaceFlinger* flinger;
2375 DisplayID dpy;
2376 sp<IMemoryHeap>* heap;
2377 uint32_t* w;
2378 uint32_t* h;
2379 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002380 uint32_t sw;
2381 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002382 uint32_t minLayerZ;
2383 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002384 status_t result;
2385 public:
2386 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002388 uint32_t sw, uint32_t sh,
2389 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002390 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002391 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2392 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2393 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002394 {
2395 }
2396 status_t getResult() const {
2397 return result;
2398 }
2399 virtual bool handler() {
2400 Mutex::Autolock _l(flinger->mStateLock);
2401
2402 // if we have secure windows, never allow the screen capture
2403 if (flinger->mSecureFrameBuffer)
2404 return true;
2405
Mathias Agopian74c40c02010-09-29 13:02:36 -07002406 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002407 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002408
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002409 return true;
2410 }
2411 };
2412
2413 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002414 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002415 status_t res = postMessageSync(msg);
2416 if (res == NO_ERROR) {
2417 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2418 }
2419 return res;
2420}
2421
2422// ---------------------------------------------------------------------------
2423
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002424sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2425{
2426 sp<Layer> result;
2427 Mutex::Autolock _l(mStateLock);
2428 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2429 return result;
2430}
2431
2432// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002433
Mathias Agopian96f08192010-06-02 23:28:45 -07002434Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002435 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002436{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437}
2438
Mathias Agopian96f08192010-06-02 23:28:45 -07002439Client::~Client()
2440{
Mathias Agopian96f08192010-06-02 23:28:45 -07002441 const size_t count = mLayers.size();
2442 for (size_t i=0 ; i<count ; i++) {
2443 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2444 if (layer != 0) {
2445 mFlinger->removeLayer(layer);
2446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 }
2448}
2449
Mathias Agopian96f08192010-06-02 23:28:45 -07002450status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002451 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002452}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002453
Mathias Agopian4f113742011-05-03 16:21:41 -07002454size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002455{
Mathias Agopian4f113742011-05-03 16:21:41 -07002456 Mutex::Autolock _l(mLock);
2457 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002458 mLayers.add(name, layer);
2459 return name;
2460}
2461
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002462void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002463{
Mathias Agopian4f113742011-05-03 16:21:41 -07002464 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002465 // we do a linear search here, because this doesn't happen often
2466 const size_t count = mLayers.size();
2467 for (size_t i=0 ; i<count ; i++) {
2468 if (mLayers.valueAt(i) == layer) {
2469 mLayers.removeItemsAt(i, 1);
2470 break;
2471 }
2472 }
2473}
Mathias Agopian4f113742011-05-03 16:21:41 -07002474sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2475{
2476 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002477 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002478 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002479 if (layer != 0) {
2480 lbc = layer.promote();
2481 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002482 }
2483 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484}
2485
Mathias Agopiana67932f2011-04-20 14:20:59 -07002486
2487status_t Client::onTransact(
2488 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2489{
2490 // these must be checked
2491 IPCThreadState* ipc = IPCThreadState::self();
2492 const int pid = ipc->getCallingPid();
2493 const int uid = ipc->getCallingUid();
2494 const int self_pid = getpid();
2495 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2496 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002497 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002498 {
2499 LOGE("Permission Denial: "
2500 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2501 return PERMISSION_DENIED;
2502 }
2503 }
2504 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002505}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002506
2507
Mathias Agopian96f08192010-06-02 23:28:45 -07002508sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002509 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002510 const String8& name,
2511 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 uint32_t flags)
2513{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002514 /*
2515 * createSurface must be called from the GL thread so that it can
2516 * have access to the GL context.
2517 */
2518
2519 class MessageCreateSurface : public MessageBase {
2520 sp<ISurface> result;
2521 SurfaceFlinger* flinger;
2522 ISurfaceComposerClient::surface_data_t* params;
2523 Client* client;
2524 const String8& name;
2525 DisplayID display;
2526 uint32_t w, h;
2527 PixelFormat format;
2528 uint32_t flags;
2529 public:
2530 MessageCreateSurface(SurfaceFlinger* flinger,
2531 ISurfaceComposerClient::surface_data_t* params,
2532 const String8& name, Client* client,
2533 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2534 uint32_t flags)
2535 : flinger(flinger), params(params), client(client), name(name),
2536 display(display), w(w), h(h), format(format), flags(flags)
2537 {
2538 }
2539 sp<ISurface> getResult() const { return result; }
2540 virtual bool handler() {
2541 result = flinger->createSurface(params, name, client,
2542 display, w, h, format, flags);
2543 return true;
2544 }
2545 };
2546
2547 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2548 params, name, this, display, w, h, format, flags);
2549 mFlinger->postMessageSync(msg);
2550 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551}
Mathias Agopian96f08192010-06-02 23:28:45 -07002552status_t Client::destroySurface(SurfaceID sid) {
2553 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002555
2556// ---------------------------------------------------------------------------
2557
Jamie Gennis9a78c902011-01-12 18:30:40 -08002558GraphicBufferAlloc::GraphicBufferAlloc() {}
2559
2560GraphicBufferAlloc::~GraphicBufferAlloc() {}
2561
2562sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002563 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002564 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2565 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002566 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002567 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002568 if (err == NO_MEMORY) {
2569 GraphicBuffer::dumpAllocationsToSystemLog();
2570 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002571 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2572 "failed (%s), handle=%p",
2573 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002574 return 0;
2575 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002576 return graphicBuffer;
2577}
2578
Jamie Gennis9a78c902011-01-12 18:30:40 -08002579// ---------------------------------------------------------------------------
2580
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581GraphicPlane::GraphicPlane()
2582 : mHw(0)
2583{
2584}
2585
2586GraphicPlane::~GraphicPlane() {
2587 delete mHw;
2588}
2589
2590bool GraphicPlane::initialized() const {
2591 return mHw ? true : false;
2592}
2593
Mathias Agopian2b92d892010-02-08 15:49:35 -08002594int GraphicPlane::getWidth() const {
2595 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002596}
2597
Mathias Agopian2b92d892010-02-08 15:49:35 -08002598int GraphicPlane::getHeight() const {
2599 return mHeight;
2600}
2601
2602void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2603{
2604 mHw = hw;
2605
2606 // initialize the display orientation transform.
2607 // it's a constant that should come from the display driver.
2608 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2609 char property[PROPERTY_VALUE_MAX];
2610 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2611 //displayOrientation
2612 switch (atoi(property)) {
2613 case 90:
2614 displayOrientation = ISurfaceComposer::eOrientation90;
2615 break;
2616 case 270:
2617 displayOrientation = ISurfaceComposer::eOrientation270;
2618 break;
2619 }
2620 }
2621
2622 const float w = hw->getWidth();
2623 const float h = hw->getHeight();
2624 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2625 &mDisplayTransform);
2626 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2627 mDisplayWidth = h;
2628 mDisplayHeight = w;
2629 } else {
2630 mDisplayWidth = w;
2631 mDisplayHeight = h;
2632 }
2633
2634 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635}
2636
2637status_t GraphicPlane::orientationToTransfrom(
2638 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002639{
2640 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002641 switch (orientation) {
2642 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002643 flags = Transform::ROT_0;
2644 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002645 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002646 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002647 break;
2648 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002649 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002650 break;
2651 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002652 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002653 break;
2654 default:
2655 return BAD_VALUE;
2656 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002657 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002658 return NO_ERROR;
2659}
2660
2661status_t GraphicPlane::setOrientation(int orientation)
2662{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663 // If the rotation can be handled in hardware, this is where
2664 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002665
2666 const DisplayHardware& hw(displayHardware());
2667 const float w = mDisplayWidth;
2668 const float h = mDisplayHeight;
2669 mWidth = int(w);
2670 mHeight = int(h);
2671
2672 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002673 GraphicPlane::orientationToTransfrom(orientation, w, h,
2674 &orientationTransform);
2675 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2676 mWidth = int(h);
2677 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002679
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002680 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002681 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002682 return NO_ERROR;
2683}
2684
2685const DisplayHardware& GraphicPlane::displayHardware() const {
2686 return *mHw;
2687}
2688
Mathias Agopian59119e62010-10-11 12:37:43 -07002689DisplayHardware& GraphicPlane::editDisplayHardware() {
2690 return *mHw;
2691}
2692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693const Transform& GraphicPlane::transform() const {
2694 return mGlobalTransform;
2695}
2696
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002697EGLDisplay GraphicPlane::getEGLDisplay() const {
2698 return mHw->getEGLDisplay();
2699}
2700
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002701// ---------------------------------------------------------------------------
2702
2703}; // namespace android