blob: 39f0f401b4f99041478b2409f277053c159428a8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <math.h>
Jean-Baptiste Querua837ba72009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/ioctl.h>
28
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian35b48d12010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian1f7bec62010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian8afb7e32011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "LayerDim.h"
Mathias Agopian118d0242011-10-13 16:02:48 -070053#include "LayerScreenshot.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
56#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070057#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopiana67932f2011-04-20 14:20:59 -070059#include <private/surfaceflinger/SharedBufferStack.h>
60
Mathias Agopiana1ecca92009-05-21 19:21:59 -070061/* ideally AID_GRAPHICS would be in a semi-public header
62 * or there would be a way to map a user/group name to its id
63 */
64#ifndef AID_GRAPHICS
65#define AID_GRAPHICS 1003
66#endif
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#define DISPLAY_COUNT 1
69
70namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ---------------------------------------------------------------------------
72
Mathias Agopian99b49842011-06-27 16:05:52 -070073const String16 sHardwareTest("android.permission.HARDWARE_TEST");
74const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
75const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
76const String16 sDump("android.permission.DUMP");
77
78// ---------------------------------------------------------------------------
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080SurfaceFlinger::SurfaceFlinger()
81 : BnSurfaceComposer(), Thread(false),
82 mTransactionFlags(0),
Jamie Gennis28378392011-10-12 17:39:00 -070083 mTransationPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 mLayersRemoved(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -070087 mHwWorkListDirty(false),
Mathias Agopianabd671a2010-10-14 14:54:06 -070088 mElectronBeamAnimationMode(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 mDebugRegion(0),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 mDebugBackground(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -070091 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -070092 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -070093 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -070094 mDebugInSwapBuffers(0),
95 mLastSwapBufferTime(0),
96 mDebugInTransaction(0),
97 mLastTransactionTime(0),
Mathias Agopian3330b202009-10-05 17:07:12 -070098 mBootFinished(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mConsoleSignals(0),
100 mSecureFrameBuffer(0)
101{
102 init();
103}
104
105void SurfaceFlinger::init()
106{
107 LOGI("SurfaceFlinger is starting");
108
109 // debugging stuff...
110 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 property_get("debug.sf.showupdates", value, "0");
113 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 property_get("debug.sf.showbackground", value, "0");
116 mDebugBackground = atoi(value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700118 property_get("debug.sf.ddms", value, "0");
119 mDebugDDMS = atoi(value);
120 if (mDebugDDMS) {
121 DdmConnection::start(getServiceName());
122 }
123
Mathias Agopian78fd5012010-04-20 14:51:04 -0700124 LOGI_IF(mDebugRegion, "showupdates enabled");
125 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
129SurfaceFlinger::~SurfaceFlinger()
130{
131 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132}
133
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700134sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135{
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700136 return mServerHeap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian7e27f052010-05-28 14:22:23 -0700139sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140{
Mathias Agopian96f08192010-06-02 23:28:45 -0700141 sp<ISurfaceComposerClient> bclient;
142 sp<Client> client(new Client(this));
143 status_t err = client->initCheck();
144 if (err == NO_ERROR) {
145 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 return bclient;
148}
149
Jamie Gennis9a78c902011-01-12 18:30:40 -0800150sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
151{
152 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
153 return gba;
154}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
157{
158 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
159 const GraphicPlane& plane(mGraphicPlanes[dpy]);
160 return plane;
161}
162
163GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
164{
165 return const_cast<GraphicPlane&>(
166 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
167}
168
169void SurfaceFlinger::bootFinished()
170{
171 const nsecs_t now = systemTime();
172 const nsecs_t duration = now - mBootTime;
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700173 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700175
176 // wait patiently for the window manager death
177 const String16 name("window");
178 sp<IBinder> window(defaultServiceManager()->getService(name));
179 if (window != 0) {
180 window->linkToDeath(this);
181 }
182
183 // stop boot animation
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700184 property_set("ctl.stop", "bootanim");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700187void SurfaceFlinger::binderDied(const wp<IBinder>& who)
188{
189 // the window manager died on us. prepare its eulogy.
190
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700191 // reset screen orientation
192 setOrientation(0, eOrientationDefault, 0);
193
194 // restart the boot-animation
195 property_set("ctl.start", "bootanim");
196}
197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198void SurfaceFlinger::onFirstRef()
199{
200 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
201
202 // Wait for the main thread to be done with its initialization
203 mReadyToRunBarrier.wait();
204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206static inline uint16_t pack565(int r, int g, int b) {
207 return (r<<11)|(g<<5)|b;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210status_t SurfaceFlinger::readyToRun()
211{
212 LOGI( "SurfaceFlinger's main thread ready to run. "
213 "Initializing graphics H/W...");
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 // we only support one display currently
216 int dpy = 0;
217
218 {
219 // initialize the main display
220 GraphicPlane& plane(graphicPlane(dpy));
221 DisplayHardware* const hw = new DisplayHardware(this, dpy);
222 plane.setDisplayHardware(hw);
223 }
224
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700225 // create the shared control-block
226 mServerHeap = new MemoryHeapBase(4096,
227 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
228 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700229
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700230 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
231 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700232
Mathias Agopian7303c6b2009-07-02 18:11:53 -0700233 new(mServerCblk) surface_flinger_cblk_t;
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 // initialize primary screen
236 // (other display should be initialized in the same manner, but
237 // asynchronously, as they could come and go. None of this is supported
238 // yet).
239 const GraphicPlane& plane(graphicPlane(dpy));
240 const DisplayHardware& hw = plane.displayHardware();
241 const uint32_t w = hw.getWidth();
242 const uint32_t h = hw.getHeight();
243 const uint32_t f = hw.getFormat();
244 hw.makeCurrent();
245
246 // initialize the shared control block
247 mServerCblk->connected |= 1<<dpy;
248 display_cblk_t* dcblk = mServerCblk->displays + dpy;
249 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian2b92d892010-02-08 15:49:35 -0800250 dcblk->w = plane.getWidth();
251 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 dcblk->format = f;
253 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
254 dcblk->xdpi = hw.getDpiX();
255 dcblk->ydpi = hw.getDpiY();
256 dcblk->fps = hw.getRefreshRate();
257 dcblk->density = hw.getDensity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259 // Initialize OpenGL|ES
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700261 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 glEnableClientState(GL_VERTEX_ARRAY);
263 glEnable(GL_SCISSOR_TEST);
264 glShadeModel(GL_FLAT);
265 glDisable(GL_DITHER);
266 glDisable(GL_CULL_FACE);
267
268 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
269 const uint16_t g1 = pack565(0x17,0x2f,0x17);
Jamie Gennis9575f602011-10-07 14:51:16 -0700270 const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 glGenTextures(1, &mWormholeTexName);
272 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
273 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
276 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
Jamie Gennis9575f602011-10-07 14:51:16 -0700278 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
279
280 const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
281 glGenTextures(1, &mProtectedTexName);
282 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
284 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
286 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
287 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
288 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
290 glViewport(0, 0, w, h);
291 glMatrixMode(GL_PROJECTION);
292 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -0700293 // put the origin in the left-bottom corner
294 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
Mathias Agopian439cf852011-11-29 13:07:24 -0800296 mReadyToRunBarrier.open();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
298 /*
299 * We're now ready to accept clients...
300 */
301
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700302 // start boot animation
303 property_set("ctl.start", "bootanim");
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 return NO_ERROR;
306}
307
308// ----------------------------------------------------------------------------
309#if 0
310#pragma mark -
311#pragma mark Events Handler
312#endif
313
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800314void SurfaceFlinger::waitForEvent() {
315 mEventQueue.waitMessage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
317
318void SurfaceFlinger::signalEvent() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700319 mEventQueue.invalidate();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
321
Jamie Gennis582270d2011-08-17 18:19:00 -0700322bool SurfaceFlinger::authenticateSurfaceTexture(
323 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800324 Mutex::Autolock _l(mStateLock);
Jamie Gennis582270d2011-08-17 18:19:00 -0700325 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800326
327 // Check the visible layer list for the ISurface
328 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
329 size_t count = currentLayers.size();
330 for (size_t i=0 ; i<count ; i++) {
331 const sp<LayerBase>& layer(currentLayers[i]);
332 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700333 if (lbc != NULL) {
334 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
335 if (lbcBinder == surfaceTextureBinder) {
336 return true;
337 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800338 }
339 }
340
341 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis582270d2011-08-17 18:19:00 -0700342 // SurfaceTexture gets destroyed before all the clients are done using it,
343 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennis134f0422011-03-08 12:18:54 -0800344 // will instead fail later on when the client tries to use the surface,
345 // which should be reported as "surface XYZ returned an -ENODEV". The
346 // purgatorized layers are no less authentic than the visible ones, so this
347 // should not cause any harm.
348 size_t purgatorySize = mLayerPurgatory.size();
349 for (size_t i=0 ; i<purgatorySize ; i++) {
350 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
351 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis582270d2011-08-17 18:19:00 -0700352 if (lbc != NULL) {
353 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
354 if (lbcBinder == surfaceTextureBinder) {
355 return true;
356 }
Jamie Gennis134f0422011-03-08 12:18:54 -0800357 }
358 }
359
360 return false;
361}
362
Mathias Agopian439cf852011-11-29 13:07:24 -0800363status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
364 nsecs_t reltime, uint32_t flags)
365{
366 return mEventQueue.postMessage(msg, reltime, flags);
Mathias Agopianbb641242010-05-18 17:06:55 -0700367}
368
Mathias Agopian439cf852011-11-29 13:07:24 -0800369status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
370 nsecs_t reltime, uint32_t flags)
371{
372 status_t res = mEventQueue.postMessage(msg, reltime, flags);
373 if (res == NO_ERROR) {
374 msg->wait();
375 }
376 return res;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377}
378
379// ----------------------------------------------------------------------------
380#if 0
381#pragma mark -
382#pragma mark Main loop
383#endif
384
385bool SurfaceFlinger::threadLoop()
386{
387 waitForEvent();
388
389 // check for transactions
390 if (UNLIKELY(mConsoleSignals)) {
391 handleConsoleEvents();
392 }
393
Mathias Agopian698c0872011-06-28 19:09:31 -0700394 // if we're in a global transaction, don't do anything.
395 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
396 uint32_t transactionFlags = peekTransactionFlags(mask);
397 if (UNLIKELY(transactionFlags)) {
398 handleTransaction(transactionFlags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 }
400
401 // post surfaces (if needed)
402 handlePageFlip();
403
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700404 if (mDirtyRegion.isEmpty()) {
405 // nothing new to do.
406 return true;
407 }
408
Mathias Agopiana350ff92010-08-10 17:14:02 -0700409 if (UNLIKELY(mHwWorkListDirty)) {
410 // build the h/w work list
411 handleWorkList();
412 }
413
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Jamie Gennisa402c4c2011-10-14 16:44:08 -0700415 if (LIKELY(hw.canDraw())) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 // repaint the framebuffer (if needed)
Mathias Agopian35b48d12010-09-13 22:57:58 -0700417
418 const int index = hw.getCurrentBufferIndex();
419 GraphicLog& logger(GraphicLog::getInstance());
420
421 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 handleRepaint();
423
Mathias Agopian74faca22009-09-17 16:18:16 -0700424 // inform the h/w that we're done compositing
Mathias Agopian35b48d12010-09-13 22:57:58 -0700425 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopian74faca22009-09-17 16:18:16 -0700426 hw.compositionComplete();
427
Mathias Agopian35b48d12010-09-13 22:57:58 -0700428 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala8392b502010-09-08 06:37:14 -0700429 postFramebuffer();
430
Mathias Agopian35b48d12010-09-13 22:57:58 -0700431 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 } else {
433 // pretend we did the post
Mathias Agopiane6f09842010-12-15 14:41:59 -0800434 hw.compositionComplete();
Mathias Agopian439cf852011-11-29 13:07:24 -0800435 usleep(16667); // 60 fps period
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 }
437 return true;
438}
439
440void SurfaceFlinger::postFramebuffer()
441{
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700442 // this should never happen. we do the flip anyways so we don't
443 // risk to cause a deadlock with hwc
444 LOGW_IF(mSwapRegion.isEmpty(), "mSwapRegion is empty");
Mathias Agopiana44b0412011-10-16 18:46:35 -0700445 const DisplayHardware& hw(graphicPlane(0).displayHardware());
446 const nsecs_t now = systemTime();
447 mDebugInSwapBuffers = now;
448 hw.flip(mSwapRegion);
449 mLastSwapBufferTime = systemTime() - now;
450 mDebugInSwapBuffers = 0;
451 mSwapRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452}
453
454void SurfaceFlinger::handleConsoleEvents()
455{
456 // something to do with the console
457 const DisplayHardware& hw = graphicPlane(0).displayHardware();
458
459 int what = android_atomic_and(0, &mConsoleSignals);
460 if (what & eConsoleAcquired) {
461 hw.acquireScreen();
Mathias Agopian9daa5c92010-10-12 16:05:48 -0700462 // this is a temporary work-around, eventually this should be called
463 // by the power-manager
Mathias Agopianabd671a2010-10-14 14:54:06 -0700464 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 }
466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 if (what & eConsoleReleased) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700468 if (hw.isScreenAcquired()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469 hw.releaseScreen();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 }
471 }
472
473 mDirtyRegion.set(hw.bounds());
474}
475
476void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
477{
Mathias Agopianca4d3602011-05-19 15:38:14 -0700478 Mutex::Autolock _l(mStateLock);
479 const nsecs_t now = systemTime();
480 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481
Mathias Agopianca4d3602011-05-19 15:38:14 -0700482 // Here we're guaranteed that some transaction flags are set
483 // so we can call handleTransactionLocked() unconditionally.
484 // We call getTransactionFlags(), which will also clear the flags,
485 // with mStateLock held to guarantee that mCurrentState won't change
486 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700487
Mathias Agopianca4d3602011-05-19 15:38:14 -0700488 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
489 transactionFlags = getTransactionFlags(mask);
490 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700491
Mathias Agopianca4d3602011-05-19 15:38:14 -0700492 mLastTransactionTime = systemTime() - now;
493 mDebugInTransaction = 0;
494 invalidateHwcGeometry();
495 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -0700496}
497
Mathias Agopianca4d3602011-05-19 15:38:14 -0700498void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -0700499{
500 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 const size_t count = currentLayers.size();
502
503 /*
504 * Traversal of the children
505 * (perform the transaction for each of them if needed)
506 */
507
508 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
509 if (layersNeedTransaction) {
510 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700511 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
513 if (!trFlags) continue;
514
515 const uint32_t flags = layer->doTransaction(0);
516 if (flags & Layer::eVisibleRegion)
517 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 }
519 }
520
521 /*
522 * Perform our own transaction if needed
523 */
524
525 if (transactionFlags & eTransactionNeeded) {
526 if (mCurrentState.orientation != mDrawingState.orientation) {
527 // the orientation has changed, recompute all visible regions
528 // and invalidate everything.
529
530 const int dpy = 0;
531 const int orientation = mCurrentState.orientation;
Jeff Brown21230c62011-09-20 15:08:29 -0700532 // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533 GraphicPlane& plane(graphicPlane(dpy));
534 plane.setOrientation(orientation);
535
536 // update the shared control block
537 const DisplayHardware& hw(plane.displayHardware());
538 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
539 dcblk->orientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -0800540 dcblk->w = plane.getWidth();
541 dcblk->h = plane.getHeight();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542
543 mVisibleRegionsDirty = true;
544 mDirtyRegion.set(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 }
546
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700547 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
548 // layers have been added
549 mVisibleRegionsDirty = true;
550 }
551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 // some layers might have been removed, so
553 // we need to update the regions they're exposing.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700554 if (mLayersRemoved) {
Mathias Agopian48d819a2009-09-10 19:41:18 -0700555 mLayersRemoved = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 mVisibleRegionsDirty = true;
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700557 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian3d579642009-06-04 18:46:21 -0700558 const size_t count = previousLayers.size();
559 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700560 const sp<LayerBase>& layer(previousLayers[i]);
561 if (currentLayers.indexOf( layer ) < 0) {
562 // this layer is not visible anymore
Mathias Agopian5d7126b2009-07-28 14:20:21 -0700563 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopian0aa758d2009-04-22 15:23:34 -0700564 }
565 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567 }
568
569 commitTransaction();
570}
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800573 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574{
575 const GraphicPlane& plane(graphicPlane(0));
576 const Transform& planeTransform(plane.transform());
Mathias Agopianab028732010-03-16 16:41:46 -0700577 const DisplayHardware& hw(plane.displayHardware());
578 const Region screenRegion(hw.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579
580 Region aboveOpaqueLayers;
581 Region aboveCoveredLayers;
582 Region dirty;
583
584 bool secureFrameBuffer = false;
585
586 size_t i = currentLayers.size();
587 while (i--) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700588 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 layer->validateVisibility(planeTransform);
590
591 // start with the whole surface at its current location
Mathias Agopian97011222009-07-28 10:57:27 -0700592 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593
Mathias Agopianab028732010-03-16 16:41:46 -0700594 /*
595 * opaqueRegion: area of a surface that is fully opaque.
596 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700598
599 /*
600 * visibleRegion: area of a surface that is visible on screen
601 * and not fully transparent. This is essentially the layer's
602 * footprint minus the opaque regions above it.
603 * Areas covered by a translucent surface are considered visible.
604 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700606
607 /*
608 * coveredRegion: area of a surface that is covered by all
609 * visible regions above it (which includes the translucent areas).
610 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -0700612
613
614 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian97011222009-07-28 10:57:27 -0700615 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700616 const bool translucent = !layer->isOpaque();
Mathias Agopian97011222009-07-28 10:57:27 -0700617 const Rect bounds(layer->visibleBounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -0700619 visibleRegion.andSelf(screenRegion);
620 if (!visibleRegion.isEmpty()) {
621 // Remove the transparent area from the visible region
622 if (translucent) {
623 visibleRegion.subtractSelf(layer->transparentRegionScreen);
624 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625
Mathias Agopianab028732010-03-16 16:41:46 -0700626 // compute the opaque region
627 const int32_t layerOrientation = layer->getOrientation();
628 if (s.alpha==255 && !translucent &&
629 ((layerOrientation & Transform::ROT_INVALID) == false)) {
630 // the opaque region is the layer's footprint
631 opaqueRegion = visibleRegion;
632 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 }
634 }
635
Mathias Agopianab028732010-03-16 16:41:46 -0700636 // Clip the covered region to the visible region
637 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
638
639 // Update aboveCoveredLayers for next (lower) layer
640 aboveCoveredLayers.orSelf(visibleRegion);
641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 // subtract the opaque region covered by the layers above us
643 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644
645 // compute this layer's dirty region
646 if (layer->contentDirty) {
647 // we need to invalidate the whole region
648 dirty = visibleRegion;
649 // as well, as the old visible region
650 dirty.orSelf(layer->visibleRegionScreen);
651 layer->contentDirty = false;
652 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700653 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -0700654 * the exposed region consists of two components:
655 * 1) what's VISIBLE now and was COVERED before
656 * 2) what's EXPOSED now less what was EXPOSED before
657 *
658 * note that (1) is conservative, we start with the whole
659 * visible region but only keep what used to be covered by
660 * something -- which mean it may have been exposed.
661 *
662 * (2) handles areas that were not covered by anything but got
663 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -0700664 */
Mathias Agopianab028732010-03-16 16:41:46 -0700665 const Region newExposed = visibleRegion - coveredRegion;
666 const Region oldVisibleRegion = layer->visibleRegionScreen;
667 const Region oldCoveredRegion = layer->coveredRegionScreen;
668 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
669 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 }
671 dirty.subtractSelf(aboveOpaqueLayers);
672
673 // accumulate to the screen dirty region
674 dirtyRegion.orSelf(dirty);
675
Mathias Agopianab028732010-03-16 16:41:46 -0700676 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800677 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700678
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 // Store the visible region is screen space
680 layer->setVisibleRegion(visibleRegion);
681 layer->setCoveredRegion(coveredRegion);
682
Mathias Agopian97011222009-07-28 10:57:27 -0700683 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 if (layer->isSecure() && !visibleRegion.isEmpty()) {
685 secureFrameBuffer = true;
686 }
687 }
688
Mathias Agopian97011222009-07-28 10:57:27 -0700689 // invalidate the areas where a layer was removed
690 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
691 mDirtyRegionRemovedLayer.clear();
692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 mSecureFrameBuffer = secureFrameBuffer;
694 opaqueRegion = aboveOpaqueLayers;
695}
696
697
698void SurfaceFlinger::commitTransaction()
699{
700 mDrawingState = mCurrentState;
Jamie Gennis28378392011-10-12 17:39:00 -0700701 mTransationPending = false;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700702 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703}
704
705void SurfaceFlinger::handlePageFlip()
706{
707 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800708 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 visibleRegions |= lockPageFlip(currentLayers);
710
711 const DisplayHardware& hw = graphicPlane(0).displayHardware();
712 const Region screenRegion(hw.bounds());
713 if (visibleRegions) {
714 Region opaqueRegion;
715 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopian4da75192010-08-10 17:19:56 -0700716
717 /*
718 * rebuild the visible layer list
719 */
Mathias Agopian1bbafb92011-03-11 16:54:47 -0800720 const size_t count = currentLayers.size();
Mathias Agopian4da75192010-08-10 17:19:56 -0700721 mVisibleLayersSortedByZ.clear();
Mathias Agopian4da75192010-08-10 17:19:56 -0700722 mVisibleLayersSortedByZ.setCapacity(count);
723 for (size_t i=0 ; i<count ; i++) {
724 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
725 mVisibleLayersSortedByZ.add(currentLayers[i]);
726 }
727
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 mWormholeRegion = screenRegion.subtract(opaqueRegion);
729 mVisibleRegionsDirty = false;
Mathias Agopianad456f92011-01-13 17:53:01 -0800730 invalidateHwcGeometry();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732
733 unlockPageFlip(currentLayers);
Mathias Agopian0dfb7b72011-10-21 15:18:28 -0700734
735 mDirtyRegion.orSelf(getAndClearInvalidateRegion());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 mDirtyRegion.andSelf(screenRegion);
737}
738
Mathias Agopianad456f92011-01-13 17:53:01 -0800739void SurfaceFlinger::invalidateHwcGeometry()
740{
741 mHwWorkListDirty = true;
742}
743
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
745{
746 bool recomputeVisibleRegions = false;
747 size_t count = currentLayers.size();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700748 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700750 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751 layer->lockPageFlip(recomputeVisibleRegions);
752 }
753 return recomputeVisibleRegions;
754}
755
756void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
757{
758 const GraphicPlane& plane(graphicPlane(0));
759 const Transform& planeTransform(plane.transform());
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->unlockPageFlip(planeTransform, mDirtyRegion);
765 }
766}
767
Mathias Agopiana350ff92010-08-10 17:14:02 -0700768void SurfaceFlinger::handleWorkList()
769{
770 mHwWorkListDirty = false;
771 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
772 if (hwc.initCheck() == NO_ERROR) {
773 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
774 const size_t count = currentLayers.size();
775 hwc.createWorkList(count);
Mathias Agopian45721772010-08-12 15:03:26 -0700776 hwc_layer_t* const cur(hwc.getLayers());
777 for (size_t i=0 ; cur && i<count ; i++) {
778 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian53331da2011-08-22 21:44:41 -0700779 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700780 cur[i].compositionType = HWC_FRAMEBUFFER;
781 cur[i].flags |= HWC_SKIP_LAYER;
782 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700783 }
784 }
785}
Mathias Agopianb8a55602009-06-26 19:06:36 -0700786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787void SurfaceFlinger::handleRepaint()
788{
Mathias Agopianb8a55602009-06-26 19:06:36 -0700789 // compute the invalid region
Mathias Agopian0656a682011-09-20 17:22:44 -0700790 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791
792 if (UNLIKELY(mDebugRegion)) {
793 debugFlashRegions();
794 }
795
Mathias Agopianb8a55602009-06-26 19:06:36 -0700796 // set the frame buffer
797 const DisplayHardware& hw(graphicPlane(0).displayHardware());
798 glMatrixMode(GL_MODELVIEW);
799 glLoadIdentity();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800
801 uint32_t flags = hw.getFlags();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700802 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
803 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700804 {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700805 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
806 // takes a rectangle, we must make sure to update that whole
807 // rectangle in that case
808 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700809 // TODO: we really should be able to pass a region to
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700810 // SWAP_RECTANGLE so that we don't have to redraw all this.
Mathias Agopian0656a682011-09-20 17:22:44 -0700811 mDirtyRegion.set(mSwapRegion.bounds());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 } else {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700813 // in the BUFFER_PRESERVED case, obviously, we can update only
814 // what's needed and nothing more.
815 // NOTE: this is NOT a common case, as preserving the backbuffer
816 // is costly and usually involves copying the whole update back.
817 }
818 } else {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700819 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700820 // We need to redraw the rectangle that will be updated
821 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -0700822 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700823 // rectangle instead of a region (see DisplayHardware::flip())
Mathias Agopian0656a682011-09-20 17:22:44 -0700824 mDirtyRegion.set(mSwapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700825 } else {
826 // we need to redraw everything (the whole screen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827 mDirtyRegion.set(hw.bounds());
Mathias Agopian0656a682011-09-20 17:22:44 -0700828 mSwapRegion = mDirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 }
830 }
831
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700832 setupHardwareComposer(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833 composeSurfaces(mDirtyRegion);
834
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700835 // update the swap region and clear the dirty region
836 mSwapRegion.orSelf(mDirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837 mDirtyRegion.clear();
838}
839
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700840void SurfaceFlinger::setupHardwareComposer(Region& dirtyInOut)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841{
Mathias Agopiana350ff92010-08-10 17:14:02 -0700842 const DisplayHardware& hw(graphicPlane(0).displayHardware());
843 HWComposer& hwc(hw.getHwComposer());
Mathias Agopian45721772010-08-12 15:03:26 -0700844 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700845 if (!cur) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700846 return;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700847 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700848
Mathias Agopianf384cc32011-09-08 18:31:55 -0700849 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
850 size_t count = layers.size();
851
852 LOGE_IF(hwc.getNumLayers() != count,
Mathias Agopian45721772010-08-12 15:03:26 -0700853 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
854 hwc.getNumLayers(), count);
855
856 // just to be extra-safe, use the smallest count
Erik Gilling24925bf2010-08-12 23:21:40 -0700857 if (hwc.initCheck() == NO_ERROR) {
858 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
859 }
Mathias Agopian45721772010-08-12 15:03:26 -0700860
861 /*
862 * update the per-frame h/w composer data for each layer
863 * and build the transparent region of the FB
864 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700865 for (size_t i=0 ; i<count ; i++) {
866 const sp<LayerBase>& layer(layers[i]);
867 layer->setPerFrameData(&cur[i]);
868 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700869 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700870 status_t err = hwc.prepare();
871 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiana350ff92010-08-10 17:14:02 -0700872
Mathias Agopianf384cc32011-09-08 18:31:55 -0700873 if (err == NO_ERROR) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700874 // what's happening here is tricky.
875 // we want to clear all the layers with the CLEAR_FB flags
876 // that are opaque.
877 // however, since some GPU are efficient at preserving
878 // the backbuffer, we want to take advantage of that so we do the
879 // clear only in the dirty region (other areas will be preserved
880 // on those GPUs).
881 // NOTE: on non backbuffer preserving GPU, the dirty region
882 // has already been expanded as needed, so the code is correct
883 // there too.
884 //
885 // However, the content of the framebuffer cannot be trusted when
886 // we switch to/from FB/OVERLAY, in which case we need to
887 // expand the dirty region to those areas too.
888 //
889 // Note also that there is a special case when switching from
890 // "no layers in FB" to "some layers in FB", where we need to redraw
891 // the entire FB, since some areas might contain uninitialized
892 // data.
893 //
894 // Also we want to make sure to not clear areas that belong to
Mathias Agopian3a3cad32011-10-18 17:36:28 -0700895 // layers above that won't redraw (we would just be erasing them),
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700896 // that is, we can't erase anything outside the dirty region.
897
Mathias Agopianf9abeb92011-09-09 01:47:48 -0700898 Region transparent;
Mathias Agopianf384cc32011-09-08 18:31:55 -0700899
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700900 if (!fbLayerCount && hwc.getLayerCount(HWC_FRAMEBUFFER)) {
901 transparent.set(hw.getBounds());
902 dirtyInOut = transparent;
903 } else {
904 for (size_t i=0 ; i<count ; i++) {
905 const sp<LayerBase>& layer(layers[i]);
906 if ((cur[i].hints & HWC_HINT_CLEAR_FB) && layer->isOpaque()) {
907 transparent.orSelf(layer->visibleRegionScreen);
908 }
909 bool isOverlay = (cur[i].compositionType != HWC_FRAMEBUFFER);
910 if (isOverlay != layer->isOverlay()) {
911 // we transitioned to/from overlay, so add this layer
912 // to the dirty region so the framebuffer can be either
913 // cleared or redrawn.
914 dirtyInOut.orSelf(layer->visibleRegionScreen);
915 }
916 layer->setOverlay(isOverlay);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800917 }
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700918 // don't erase stuff outside the dirty region
919 transparent.andSelf(dirtyInOut);
Mathias Agopianf384cc32011-09-08 18:31:55 -0700920 }
921
922 /*
923 * clear the area of the FB that need to be transparent
924 */
Mathias Agopianf384cc32011-09-08 18:31:55 -0700925 if (!transparent.isEmpty()) {
926 glClearColor(0,0,0,0);
927 Region::const_iterator it = transparent.begin();
928 Region::const_iterator const end = transparent.end();
929 const int32_t height = hw.getHeight();
930 while (it != end) {
931 const Rect& r(*it++);
932 const GLint sy = height - (r.top + r.height());
933 glScissor(r.left, sy, r.width(), r.height());
934 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf20a3242011-01-19 15:24:23 -0800935 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700936 }
937 }
Mathias Agopianf384cc32011-09-08 18:31:55 -0700938}
Mathias Agopian45721772010-08-12 15:03:26 -0700939
Mathias Agopianf384cc32011-09-08 18:31:55 -0700940void SurfaceFlinger::composeSurfaces(const Region& dirty)
941{
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700942 const DisplayHardware& hw(graphicPlane(0).displayHardware());
943 HWComposer& hwc(hw.getHwComposer());
944
945 const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
946 if (UNLIKELY(fbLayerCount && !mWormholeRegion.isEmpty())) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700947 // should never happen unless the window manager has a bug
948 // draw something...
949 drawWormhole();
950 }
951
Mathias Agopian45721772010-08-12 15:03:26 -0700952 /*
953 * and then, render the layers targeted at the framebuffer
954 */
Mathias Agopiancd20eb02011-09-22 20:57:04 -0700955 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopianf384cc32011-09-08 18:31:55 -0700956 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
957 size_t count = layers.size();
Mathias Agopian45721772010-08-12 15:03:26 -0700958 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700959 if (cur && (cur[i].compositionType != HWC_FRAMEBUFFER)) {
Mathias Agopianf384cc32011-09-08 18:31:55 -0700960 continue;
Mathias Agopian45721772010-08-12 15:03:26 -0700961 }
962 const sp<LayerBase>& layer(layers[i]);
963 const Region clip(dirty.intersect(layer->visibleRegionScreen));
964 if (!clip.isEmpty()) {
965 layer->draw(clip);
966 }
967 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968}
969
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970void SurfaceFlinger::debugFlashRegions()
971{
Mathias Agopian0a917752010-06-14 21:20:00 -0700972 const DisplayHardware& hw(graphicPlane(0).displayHardware());
973 const uint32_t flags = hw.getFlags();
Mathias Agopian53331da2011-08-22 21:44:41 -0700974 const int32_t height = hw.getHeight();
Mathias Agopian0656a682011-09-20 17:22:44 -0700975 if (mSwapRegion.isEmpty()) {
Mathias Agopian53331da2011-08-22 21:44:41 -0700976 return;
977 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700978
Mathias Agopian0a917752010-06-14 21:20:00 -0700979 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
980 (flags & DisplayHardware::BUFFER_PRESERVED))) {
981 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
982 mDirtyRegion.bounds() : hw.bounds());
983 composeSurfaces(repaint);
984 }
985
Mathias Agopianc492e672011-10-18 14:49:27 -0700986 glDisable(GL_TEXTURE_EXTERNAL_OES);
987 glDisable(GL_TEXTURE_2D);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 glDisable(GL_BLEND);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 glDisable(GL_SCISSOR_TEST);
990
Mathias Agopian0926f502009-05-04 14:17:04 -0700991 static int toggle = 0;
992 toggle = 1 - toggle;
993 if (toggle) {
Mathias Agopian0a917752010-06-14 21:20:00 -0700994 glColor4f(1, 0, 1, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700995 } else {
Mathias Agopian0a917752010-06-14 21:20:00 -0700996 glColor4f(1, 1, 0, 1);
Mathias Agopian0926f502009-05-04 14:17:04 -0700997 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998
Mathias Agopian20f68782009-05-11 00:03:41 -0700999 Region::const_iterator it = mDirtyRegion.begin();
1000 Region::const_iterator const end = mDirtyRegion.end();
1001 while (it != end) {
1002 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 GLfloat vertices[][2] = {
Mathias Agopian53331da2011-08-22 21:44:41 -07001004 { r.left, height - r.top },
1005 { r.left, height - r.bottom },
1006 { r.right, height - r.bottom },
1007 { r.right, height - r.top }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 };
1009 glVertexPointer(2, GL_FLOAT, 0, vertices);
1010 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1011 }
Mathias Agopian0a917752010-06-14 21:20:00 -07001012
Mathias Agopian0656a682011-09-20 17:22:44 -07001013 hw.flip(mSwapRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014
1015 if (mDebugRegion > 1)
Mathias Agopian0a917752010-06-14 21:20:00 -07001016 usleep(mDebugRegion * 1000);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017
1018 glEnable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019}
1020
1021void SurfaceFlinger::drawWormhole() const
1022{
1023 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1024 if (region.isEmpty())
1025 return;
1026
1027 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1028 const int32_t width = hw.getWidth();
1029 const int32_t height = hw.getHeight();
1030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 if (LIKELY(!mDebugBackground)) {
Mathias Agopian0a917752010-06-14 21:20:00 -07001032 glClearColor(0,0,0,0);
Mathias Agopian20f68782009-05-11 00:03:41 -07001033 Region::const_iterator it = region.begin();
1034 Region::const_iterator const end = region.end();
1035 while (it != end) {
1036 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 const GLint sy = height - (r.top + r.height());
1038 glScissor(r.left, sy, r.width(), r.height());
1039 glClear(GL_COLOR_BUFFER_BIT);
1040 }
1041 } else {
1042 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1043 { width, height }, { 0, height } };
1044 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
Mathias Agopianc492e672011-10-18 14:49:27 -07001045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 glVertexPointer(2, GL_SHORT, 0, vertices);
1047 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1048 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianc492e672011-10-18 14:49:27 -07001049
1050 glDisable(GL_TEXTURE_EXTERNAL_OES);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 glEnable(GL_TEXTURE_2D);
1052 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1053 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1054 glMatrixMode(GL_TEXTURE);
1055 glLoadIdentity();
Mathias Agopianc492e672011-10-18 14:49:27 -07001056
1057 glDisable(GL_BLEND);
1058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian20f68782009-05-11 00:03:41 -07001060 Region::const_iterator it = region.begin();
1061 Region::const_iterator const end = region.end();
1062 while (it != end) {
1063 const Rect& r = *it++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 const GLint sy = height - (r.top + r.height());
1065 glScissor(r.left, sy, r.width(), r.height());
1066 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1067 }
1068 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001069 glDisable(GL_TEXTURE_2D);
Mathias Agopianebeb7092010-12-14 18:38:36 -08001070 glLoadIdentity();
1071 glMatrixMode(GL_MODELVIEW);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 }
1073}
1074
1075void SurfaceFlinger::debugShowFPS() const
1076{
1077 static int mFrameCount;
1078 static int mLastFrameCount = 0;
1079 static nsecs_t mLastFpsTime = 0;
1080 static float mFps = 0;
1081 mFrameCount++;
1082 nsecs_t now = systemTime();
1083 nsecs_t diff = now - mLastFpsTime;
1084 if (diff > ms2ns(250)) {
1085 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1086 mLastFpsTime = now;
1087 mLastFrameCount = mFrameCount;
1088 }
1089 // XXX: mFPS has the value we want
1090 }
1091
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001092status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093{
1094 Mutex::Autolock _l(mStateLock);
1095 addLayer_l(layer);
1096 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1097 return NO_ERROR;
1098}
1099
Mathias Agopian96f08192010-06-02 23:28:45 -07001100status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1101{
Mathias Agopianf6679fc2010-08-10 18:09:09 -07001102 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001103 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1104}
1105
1106ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1107 const sp<LayerBaseClient>& lbc)
1108{
Mathias Agopian96f08192010-06-02 23:28:45 -07001109 // attach this layer to the client
Mathias Agopian4f113742011-05-03 16:21:41 -07001110 size_t name = client->attachLayer(lbc);
1111
1112 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001113
1114 // add this layer to the current state list
1115 addLayer_l(lbc);
1116
Mathias Agopian4f113742011-05-03 16:21:41 -07001117 return ssize_t(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001118}
1119
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001120status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121{
1122 Mutex::Autolock _l(mStateLock);
Mathias Agopian3d579642009-06-04 18:46:21 -07001123 status_t err = purgatorizeLayer_l(layer);
1124 if (err == NO_ERROR)
1125 setTransactionFlags(eTransactionNeeded);
1126 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127}
1128
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001129status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130{
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001131 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1132 if (lbc != 0) {
Mathias Agopian0d156122011-01-25 20:17:45 -08001133 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1136 if (index >= 0) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001137 mLayersRemoved = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 return NO_ERROR;
1139 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001140 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141}
1142
Mathias Agopian9a112062009-04-17 19:36:26 -07001143status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1144{
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001145 // First add the layer to the purgatory list, which makes sure it won't
1146 // go away, then remove it from the main list (through a transaction).
Mathias Agopian9a112062009-04-17 19:36:26 -07001147 ssize_t err = removeLayer_l(layerBase);
Mathias Agopian76cd4dd2011-01-14 17:37:42 -08001148 if (err >= 0) {
1149 mLayerPurgatory.add(layerBase);
1150 }
Mathias Agopian8c0a3d72009-09-23 16:44:00 -07001151
Mathias Agopian0b3ad462009-10-02 18:12:30 -07001152 layerBase->onRemoved();
1153
Mathias Agopian3d579642009-06-04 18:46:21 -07001154 // it's possible that we don't find a layer, because it might
1155 // have been destroyed already -- this is not technically an error
Mathias Agopian96f08192010-06-02 23:28:45 -07001156 // from the user because there is a race between Client::destroySurface(),
1157 // ~Client() and ~ISurface().
Mathias Agopian9a112062009-04-17 19:36:26 -07001158 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1159}
1160
Mathias Agopian96f08192010-06-02 23:28:45 -07001161status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162{
Mathias Agopian96f08192010-06-02 23:28:45 -07001163 layer->forceVisibilityTransaction();
1164 setTransactionFlags(eTraversalNeeded);
1165 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166}
1167
Mathias Agopiandea20b12011-05-03 17:04:02 -07001168uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1169{
1170 return android_atomic_release_load(&mTransactionFlags);
1171}
1172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1174{
1175 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1176}
1177
Mathias Agopianbb641242010-05-18 17:06:55 -07001178uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179{
1180 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1181 if ((old & flags)==0) { // wake the server up
Mathias Agopianbb641242010-05-18 17:06:55 -07001182 signalEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 }
1184 return old;
1185}
1186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001188void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
Jamie Gennis28378392011-10-12 17:39:00 -07001189 int orientation, uint32_t flags) {
Mathias Agopian698c0872011-06-28 19:09:31 -07001190 Mutex::Autolock _l(mStateLock);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001191
Jamie Gennis28378392011-10-12 17:39:00 -07001192 uint32_t transactionFlags = 0;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001193 if (mCurrentState.orientation != orientation) {
1194 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
1195 mCurrentState.orientation = orientation;
Jamie Gennis28378392011-10-12 17:39:00 -07001196 transactionFlags |= eTransactionNeeded;
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001197 } else if (orientation != eOrientationUnchanged) {
1198 LOGW("setTransactionState: ignoring unrecognized orientation: %d",
1199 orientation);
1200 }
1201 }
1202
Mathias Agopian698c0872011-06-28 19:09:31 -07001203 const size_t count = state.size();
1204 for (size_t i=0 ; i<count ; i++) {
1205 const ComposerState& s(state[i]);
1206 sp<Client> client( static_cast<Client *>(s.client.get()) );
Jamie Gennis28378392011-10-12 17:39:00 -07001207 transactionFlags |= setClientStateLocked(client, s.state);
Mathias Agopian698c0872011-06-28 19:09:31 -07001208 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001209
Mathias Agopian386aa982011-11-07 21:58:03 -08001210 if (transactionFlags) {
1211 // this triggers the transaction
1212 setTransactionFlags(transactionFlags);
1213
1214 // if this is a synchronous transaction, wait for it to take effect
1215 // before returning.
1216 if (flags & eSynchronous) {
1217 mTransationPending = true;
1218 }
1219 while (mTransationPending) {
1220 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1221 if (CC_UNLIKELY(err != NO_ERROR)) {
1222 // just in case something goes wrong in SF, return to the
1223 // called after a few seconds.
1224 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1225 mTransationPending = false;
1226 break;
1227 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001228 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 }
1230}
1231
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001232int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianc08731e2009-03-27 18:11:38 -07001233 int orientation, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234{
1235 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1236 return BAD_VALUE;
1237
1238 Mutex::Autolock _l(mStateLock);
1239 if (mCurrentState.orientation != orientation) {
1240 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Jeff Brown21230c62011-09-20 15:08:29 -07001241 mCurrentState.orientationFlags = flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 mCurrentState.orientation = orientation;
1243 setTransactionFlags(eTransactionNeeded);
1244 mTransactionCV.wait(mStateLock);
1245 } else {
1246 orientation = BAD_VALUE;
1247 }
1248 }
1249 return orientation;
1250}
1251
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001252sp<ISurface> SurfaceFlinger::createSurface(
1253 ISurfaceComposerClient::surface_data_t* params,
1254 const String8& name,
1255 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1257 uint32_t flags)
1258{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001259 sp<LayerBaseClient> layer;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001260 sp<ISurface> surfaceHandle;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001261
1262 if (int32_t(w|h) < 0) {
1263 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1264 int(w), int(h));
1265 return surfaceHandle;
1266 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001269 sp<Layer> normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 switch (flags & eFXSurfaceMask) {
1271 case eFXSurfaceNormal:
Mathias Agopiana5529c82010-12-07 19:38:17 -08001272 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1273 layer = normalLayer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 break;
1275 case eFXSurfaceBlur:
Mathias Agopian1293a8e2010-12-08 17:13:19 -08001276 // for now we treat Blur as Dim, until we can implement it
1277 // efficiently.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 case eFXSurfaceDim:
Mathias Agopian96f08192010-06-02 23:28:45 -07001279 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 break;
Mathias Agopian118d0242011-10-13 16:02:48 -07001281 case eFXSurfaceScreenshot:
1282 layer = createScreenshotSurface(client, d, w, h, flags);
1283 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 }
1285
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001286 if (layer != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001287 layer->initStates(w, h, flags);
Mathias Agopian285dbde2010-03-01 16:09:43 -08001288 layer->setName(name);
Mathias Agopian96f08192010-06-02 23:28:45 -07001289 ssize_t token = addClientLayer(client, layer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 surfaceHandle = layer->getSurface();
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001292 if (surfaceHandle != 0) {
Mathias Agopian96f08192010-06-02 23:28:45 -07001293 params->token = token;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001294 params->identity = layer->getIdentity();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001295 if (normalLayer != 0) {
1296 Mutex::Autolock _l(mStateLock);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001297 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001298 }
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001299 }
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001300
Mathias Agopian96f08192010-06-02 23:28:45 -07001301 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 }
1303
1304 return surfaceHandle;
1305}
1306
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001307sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001308 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001309 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian1c97d2e2009-08-19 17:46:26 -07001310 PixelFormat& format)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311{
1312 // initialize the surfaces
1313 switch (format) { // TODO: take h/w into account
1314 case PIXEL_FORMAT_TRANSPARENT:
1315 case PIXEL_FORMAT_TRANSLUCENT:
1316 format = PIXEL_FORMAT_RGBA_8888;
1317 break;
1318 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001319#ifdef NO_RGBX_8888
1320 format = PIXEL_FORMAT_RGB_565;
1321#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001322 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001323#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 break;
1325 }
1326
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001327#ifdef NO_RGBX_8888
1328 if (format == PIXEL_FORMAT_RGBX_8888)
1329 format = PIXEL_FORMAT_RGBA_8888;
1330#endif
1331
Mathias Agopian96f08192010-06-02 23:28:45 -07001332 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopianf9d93272009-06-19 17:00:27 -07001333 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian96f08192010-06-02 23:28:45 -07001334 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001336 layer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 }
1338 return layer;
1339}
1340
Mathias Agopianb7e930d2010-06-01 15:12:58 -07001341sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopianf9d93272009-06-19 17:00:27 -07001342 const sp<Client>& client, DisplayID display,
Mathias Agopian96f08192010-06-02 23:28:45 -07001343 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344{
Mathias Agopian96f08192010-06-02 23:28:45 -07001345 sp<LayerDim> layer = new LayerDim(this, display, client);
Mathias Agopian118d0242011-10-13 16:02:48 -07001346 return layer;
1347}
1348
1349sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
1350 const sp<Client>& client, DisplayID display,
1351 uint32_t w, uint32_t h, uint32_t flags)
1352{
1353 sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 return layer;
1355}
1356
Mathias Agopian96f08192010-06-02 23:28:45 -07001357status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358{
Mathias Agopian9a112062009-04-17 19:36:26 -07001359 /*
1360 * called by the window manager, when a surface should be marked for
1361 * destruction.
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001362 *
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001363 * The surface is removed from the current and drawing lists, but placed
1364 * in the purgatory queue, so it's not destroyed right-away (we need
1365 * to wait for all client's references to go away first).
Mathias Agopian9a112062009-04-17 19:36:26 -07001366 */
Mathias Agopian9a112062009-04-17 19:36:26 -07001367
Mathias Agopian48d819a2009-09-10 19:41:18 -07001368 status_t err = NAME_NOT_FOUND;
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001369 Mutex::Autolock _l(mStateLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07001370 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian48d819a2009-09-10 19:41:18 -07001371 if (layer != 0) {
1372 err = purgatorizeLayer_l(layer);
1373 if (err == NO_ERROR) {
Mathias Agopian48d819a2009-09-10 19:41:18 -07001374 setTransactionFlags(eTransactionNeeded);
1375 }
Mathias Agopian9a112062009-04-17 19:36:26 -07001376 }
1377 return err;
1378}
1379
Mathias Agopianca4d3602011-05-19 15:38:14 -07001380status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001381{
Mathias Agopian759fdb22009-07-02 17:33:40 -07001382 // called by ~ISurface() when all references are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001383 status_t err = NO_ERROR;
1384 sp<LayerBaseClient> l(layer.promote());
1385 if (l != NULL) {
1386 Mutex::Autolock _l(mStateLock);
1387 err = removeLayer_l(l);
1388 if (err == NAME_NOT_FOUND) {
1389 // The surface wasn't in the current list, which means it was
1390 // removed already, which means it is in the purgatory,
1391 // and need to be removed from there.
1392 ssize_t idx = mLayerPurgatory.remove(l);
1393 LOGE_IF(idx < 0,
1394 "layer=%p is not in the purgatory list", l.get());
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001395 }
Mathias Agopianca4d3602011-05-19 15:38:14 -07001396 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1397 "error removing layer=%p (%s)", l.get(), strerror(-err));
1398 }
1399 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400}
1401
Mathias Agopian698c0872011-06-28 19:09:31 -07001402uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian96f08192010-06-02 23:28:45 -07001403 const sp<Client>& client,
Mathias Agopian698c0872011-06-28 19:09:31 -07001404 const layer_state_t& s)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 uint32_t flags = 0;
Mathias Agopian698c0872011-06-28 19:09:31 -07001407 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1408 if (layer != 0) {
1409 const uint32_t what = s.what;
1410 if (what & ePositionChanged) {
1411 if (layer->setPosition(s.x, s.y))
1412 flags |= eTraversalNeeded;
1413 }
1414 if (what & eLayerChanged) {
1415 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1416 if (layer->setLayer(s.z)) {
1417 mCurrentState.layersSortedByZ.removeAt(idx);
1418 mCurrentState.layersSortedByZ.add(layer);
1419 // we need traversal (state changed)
1420 // AND transaction (list changed)
1421 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 }
1423 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001424 if (what & eSizeChanged) {
1425 if (layer->setSize(s.w, s.h)) {
1426 flags |= eTraversalNeeded;
Mathias Agopian698c0872011-06-28 19:09:31 -07001427 }
1428 }
1429 if (what & eAlphaChanged) {
1430 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1431 flags |= eTraversalNeeded;
1432 }
1433 if (what & eMatrixChanged) {
1434 if (layer->setMatrix(s.matrix))
1435 flags |= eTraversalNeeded;
1436 }
1437 if (what & eTransparentRegionChanged) {
1438 if (layer->setTransparentRegionHint(s.transparentRegion))
1439 flags |= eTraversalNeeded;
1440 }
1441 if (what & eVisibilityChanged) {
1442 if (layer->setFlags(s.flags, s.mask))
1443 flags |= eTraversalNeeded;
1444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001446 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447}
1448
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449void SurfaceFlinger::screenReleased(int dpy)
1450{
1451 // this may be called by a signal handler, we can't do too much in here
1452 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1453 signalEvent();
1454}
1455
1456void SurfaceFlinger::screenAcquired(int dpy)
1457{
1458 // this may be called by a signal handler, we can't do too much in here
1459 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1460 signalEvent();
1461}
1462
1463status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1464{
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001465 const size_t SIZE = 4096;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 char buffer[SIZE];
1467 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07001468
1469 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 snprintf(buffer, SIZE, "Permission Denial: "
1471 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1472 IPCThreadState::self()->getCallingPid(),
1473 IPCThreadState::self()->getCallingUid());
1474 result.append(buffer);
1475 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07001476
1477 // figure out if we're stuck somewhere
1478 const nsecs_t now = systemTime();
1479 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1480 const nsecs_t inTransaction(mDebugInTransaction);
1481 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1482 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1483
1484 // Try to get the main lock, but don't insist if we can't
1485 // (this would indicate SF is stuck, but we want to be able to
1486 // print something in dumpsys).
1487 int retry = 3;
1488 while (mStateLock.tryLock()<0 && --retry>=0) {
1489 usleep(1000000);
1490 }
1491 const bool locked(retry >= 0);
1492 if (!locked) {
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001493 snprintf(buffer, SIZE,
Mathias Agopian9795c422009-08-26 16:36:26 -07001494 "SurfaceFlinger appears to be unresponsive, "
1495 "dumping anyways (no locks held)\n");
1496 result.append(buffer);
1497 }
1498
Mathias Agopian48b888a2011-01-19 16:15:53 -08001499 /*
1500 * Dump the visible layer list
1501 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1503 const size_t count = currentLayers.size();
Mathias Agopian48b888a2011-01-19 16:15:53 -08001504 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1505 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001507 const sp<LayerBase>& layer(currentLayers[i]);
1508 layer->dump(result, buffer, SIZE);
1509 const Layer::State& s(layer->drawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 s.transparentRegion.dump(result, "transparentRegion");
1511 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1512 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1513 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001514
Mathias Agopian48b888a2011-01-19 16:15:53 -08001515 /*
1516 * Dump the layers in the purgatory
1517 */
1518
1519 const size_t purgatorySize = mLayerPurgatory.size();
1520 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1521 result.append(buffer);
1522 for (size_t i=0 ; i<purgatorySize ; i++) {
1523 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1524 layer->shortDump(result, buffer, SIZE);
1525 }
1526
1527 /*
1528 * Dump SurfaceFlinger global state
1529 */
1530
Mathias Agopianad701862011-07-14 18:01:49 -07001531 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian48b888a2011-01-19 16:15:53 -08001532 result.append(buffer);
Mathias Agopianad701862011-07-14 18:01:49 -07001533
1534 const GLExtensions& extensions(GLExtensions::getInstance());
1535 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1536 extensions.getVendor(),
1537 extensions.getRenderer(),
1538 extensions.getVersion());
1539 result.append(buffer);
1540 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1541 result.append(buffer);
1542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 mWormholeRegion.dump(result, "WormholeRegion");
1544 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1545 snprintf(buffer, SIZE,
Jamie Gennisa402c4c2011-10-14 16:44:08 -07001546 " orientation=%d, canDraw=%d\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 mCurrentState.orientation, hw.canDraw());
1548 result.append(buffer);
Mathias Agopian9795c422009-08-26 16:36:26 -07001549 snprintf(buffer, SIZE,
1550 " last eglSwapBuffers() time: %f us\n"
Mathias Agopiand5e4ef92011-10-20 17:22:38 -07001551 " last transaction time : %f us\n"
1552 " refresh-rate : %f fps\n"
1553 " x-dpi : %f\n"
1554 " y-dpi : %f\n",
1555 mLastSwapBufferTime/1000.0,
1556 mLastTransactionTime/1000.0,
1557 hw.getRefreshRate(),
1558 hw.getDpiX(),
1559 hw.getDpiY());
Mathias Agopian9795c422009-08-26 16:36:26 -07001560 result.append(buffer);
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001561
Mathias Agopian9795c422009-08-26 16:36:26 -07001562 if (inSwapBuffersDuration || !locked) {
1563 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1564 inSwapBuffersDuration/1000.0);
1565 result.append(buffer);
1566 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001567
Mathias Agopian9795c422009-08-26 16:36:26 -07001568 if (inTransactionDuration || !locked) {
1569 snprintf(buffer, SIZE, " transaction time: %f us\n",
1570 inTransactionDuration/1000.0);
1571 result.append(buffer);
1572 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07001573
Mathias Agopian48b888a2011-01-19 16:15:53 -08001574 /*
1575 * Dump HWComposer state
1576 */
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001577 HWComposer& hwc(hw.getHwComposer());
1578 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1579 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian53331da2011-08-22 21:44:41 -07001580 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001581 result.append(buffer);
Mathias Agopian22da60c2011-09-09 00:49:11 -07001582 hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
Mathias Agopian73d3ba92010-09-22 18:58:01 -07001583
Mathias Agopian48b888a2011-01-19 16:15:53 -08001584 /*
1585 * Dump gralloc state
1586 */
Mathias Agopian3330b202009-10-05 17:07:12 -07001587 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001588 alloc.dump(result);
Erik Gilling1d21a9c2010-12-01 16:38:01 -08001589 hw.dump(result);
Mathias Agopian9795c422009-08-26 16:36:26 -07001590
1591 if (locked) {
1592 mStateLock.unlock();
1593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
1595 write(fd, result.string(), result.size());
1596 return NO_ERROR;
1597}
1598
1599status_t SurfaceFlinger::onTransact(
1600 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1601{
1602 switch (code) {
1603 case CREATE_CONNECTION:
Mathias Agopian698c0872011-06-28 19:09:31 -07001604 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 case SET_ORIENTATION:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 case BOOT_FINISHED:
Mathias Agopian59119e62010-10-11 12:37:43 -07001607 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001608 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 {
1610 // codes that require permission check
1611 IPCThreadState* ipc = IPCThreadState::self();
1612 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07001613 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001614 if ((uid != AID_GRAPHICS) &&
1615 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001616 LOGE("Permission Denial: "
1617 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1618 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001620 break;
1621 }
1622 case CAPTURE_SCREEN:
1623 {
1624 // codes that require permission check
1625 IPCThreadState* ipc = IPCThreadState::self();
1626 const int pid = ipc->getCallingPid();
1627 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07001628 if ((uid != AID_GRAPHICS) &&
1629 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001630 LOGE("Permission Denial: "
1631 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1632 return PERMISSION_DENIED;
1633 }
1634 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 }
1636 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07001637
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1639 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07001640 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian99b49842011-06-27 16:05:52 -07001641 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07001642 IPCThreadState* ipc = IPCThreadState::self();
1643 const int pid = ipc->getCallingPid();
1644 const int uid = ipc->getCallingUid();
1645 LOGE("Permission Denial: "
1646 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 return PERMISSION_DENIED;
1648 }
1649 int n;
1650 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07001651 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07001652 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 return NO_ERROR;
1654 case 1002: // SHOW_UPDATES
1655 n = data.readInt32();
1656 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07001657 invalidateHwcGeometry();
1658 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 return NO_ERROR;
1660 case 1003: // SHOW_BACKGROUND
1661 n = data.readInt32();
1662 mDebugBackground = n ? 1 : 0;
1663 return NO_ERROR;
1664 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07001665 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001666 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001668 case 1005:{ // force transaction
1669 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1670 return NO_ERROR;
1671 }
Mathias Agopian35b48d12010-09-13 22:57:58 -07001672 case 1006:{ // enable/disable GraphicLog
1673 int enabled = data.readInt32();
1674 GraphicLog::getInstance().setEnabled(enabled);
1675 return NO_ERROR;
1676 }
Mathias Agopian53331da2011-08-22 21:44:41 -07001677 case 1008: // toggle use of hw composer
1678 n = data.readInt32();
1679 mDebugDisableHWC = n ? 1 : 0;
1680 invalidateHwcGeometry();
1681 repaintEverything();
1682 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07001683 case 1009: // toggle use of transform hint
1684 n = data.readInt32();
1685 mDebugDisableTransformHint = n ? 1 : 0;
1686 invalidateHwcGeometry();
1687 repaintEverything();
1688 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07001690 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 reply->writeInt32(0);
1692 reply->writeInt32(mDebugRegion);
1693 reply->writeInt32(mDebugBackground);
1694 return NO_ERROR;
1695 case 1013: {
1696 Mutex::Autolock _l(mStateLock);
1697 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1698 reply->writeInt32(hw.getPageFlipCount());
1699 }
1700 return NO_ERROR;
1701 }
1702 }
1703 return err;
1704}
1705
Mathias Agopian53331da2011-08-22 21:44:41 -07001706void SurfaceFlinger::repaintEverything() {
Mathias Agopian53331da2011-08-22 21:44:41 -07001707 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001708 const Rect bounds(hw.getBounds());
1709 setInvalidateRegion(Region(bounds));
Mathias Agopian53331da2011-08-22 21:44:41 -07001710 signalEvent();
1711}
1712
Mathias Agopian0dfb7b72011-10-21 15:18:28 -07001713void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
1714 Mutex::Autolock _l(mInvalidateLock);
1715 mInvalidateRegion = reg;
1716}
1717
1718Region SurfaceFlinger::getAndClearInvalidateRegion() {
1719 Mutex::Autolock _l(mInvalidateLock);
1720 Region reg(mInvalidateRegion);
1721 mInvalidateRegion.clear();
1722 return reg;
1723}
1724
Mathias Agopian59119e62010-10-11 12:37:43 -07001725// ---------------------------------------------------------------------------
1726
Mathias Agopian118d0242011-10-13 16:02:48 -07001727status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
1728 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
1729{
1730 Mutex::Autolock _l(mStateLock);
1731 return renderScreenToTextureLocked(dpy, textureName, uOut, vOut);
1732}
1733
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001734status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1735 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopian59119e62010-10-11 12:37:43 -07001736{
Mathias Agopian59119e62010-10-11 12:37:43 -07001737 if (!GLExtensions::getInstance().haveFramebufferObject())
1738 return INVALID_OPERATION;
1739
1740 // get screen geometry
Mathias Agopian59119e62010-10-11 12:37:43 -07001741 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopian59119e62010-10-11 12:37:43 -07001742 const uint32_t hw_w = hw.getWidth();
1743 const uint32_t hw_h = hw.getHeight();
Mathias Agopian59119e62010-10-11 12:37:43 -07001744 GLfloat u = 1;
1745 GLfloat v = 1;
1746
1747 // make sure to clear all GL error flags
1748 while ( glGetError() != GL_NO_ERROR ) ;
1749
1750 // create a FBO
1751 GLuint name, tname;
1752 glGenTextures(1, &tname);
1753 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001754 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1755 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001756 if (glGetError() != GL_NO_ERROR) {
Mathias Agopian015fb3f2010-10-14 12:19:37 -07001757 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopian59119e62010-10-11 12:37:43 -07001758 GLint tw = (2 << (31 - clz(hw_w)));
1759 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001760 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1761 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001762 u = GLfloat(hw_w) / tw;
1763 v = GLfloat(hw_h) / th;
1764 }
1765 glGenFramebuffersOES(1, &name);
1766 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001767 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1768 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian59119e62010-10-11 12:37:43 -07001769
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001770 // redraw the screen entirely...
Mathias Agopianc492e672011-10-18 14:49:27 -07001771 glDisable(GL_TEXTURE_EXTERNAL_OES);
1772 glDisable(GL_TEXTURE_2D);
Mathias Agopian62f71142011-10-26 15:11:59 -07001773 glDisable(GL_SCISSOR_TEST);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001774 glClearColor(0,0,0,1);
1775 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian62f71142011-10-26 15:11:59 -07001776 glEnable(GL_SCISSOR_TEST);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001777 glMatrixMode(GL_MODELVIEW);
1778 glLoadIdentity();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001779 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1780 const size_t count = layers.size();
1781 for (size_t i=0 ; i<count ; ++i) {
1782 const sp<LayerBase>& layer(layers[i]);
1783 layer->drawForSreenShot();
1784 }
1785
Mathias Agopian118d0242011-10-13 16:02:48 -07001786 hw.compositionComplete();
1787
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001788 // back to main framebuffer
1789 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1790 glDisable(GL_SCISSOR_TEST);
1791 glDeleteFramebuffersOES(1, &name);
1792
1793 *textureName = tname;
1794 *uOut = u;
1795 *vOut = v;
1796 return NO_ERROR;
1797}
1798
1799// ---------------------------------------------------------------------------
1800
1801status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1802{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001803 // get screen geometry
1804 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1805 const uint32_t hw_w = hw.getWidth();
1806 const uint32_t hw_h = hw.getHeight();
Mathias Agopiana9040d02011-10-10 19:02:07 -07001807 const Region screenBounds(hw.getBounds());
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001808
1809 GLfloat u, v;
1810 GLuint tname;
Mathias Agopian118d0242011-10-13 16:02:48 -07001811 status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001812 if (result != NO_ERROR) {
1813 return result;
1814 }
1815
1816 GLfloat vtx[8];
Mathias Agopiana9040d02011-10-10 19:02:07 -07001817 const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001818 glBindTexture(GL_TEXTURE_2D, tname);
1819 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1820 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1821 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1822 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1823 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1824 glVertexPointer(2, GL_FLOAT, 0, vtx);
1825
Mathias Agopianffcf4652011-07-07 17:30:31 -07001826 /*
1827 * Texture coordinate mapping
1828 *
1829 * u
1830 * 1 +----------+---+
1831 * | | | | image is inverted
1832 * | V | | w.r.t. the texture
1833 * 1-v +----------+ | coordinates
1834 * | |
1835 * | |
1836 * | |
1837 * 0 +--------------+
1838 * 0 1
1839 *
1840 */
1841
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001842 class s_curve_interpolator {
1843 const float nbFrames, s, v;
1844 public:
1845 s_curve_interpolator(int nbFrames, float s)
1846 : nbFrames(1.0f / (nbFrames-1)), s(s),
1847 v(1.0f + expf(-s + 0.5f*s)) {
1848 }
1849 float operator()(int f) {
1850 const float x = f * nbFrames;
1851 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1852 }
1853 };
1854
1855 class v_stretch {
1856 const GLfloat hw_w, hw_h;
1857 public:
1858 v_stretch(uint32_t hw_w, uint32_t hw_h)
1859 : hw_w(hw_w), hw_h(hw_h) {
1860 }
1861 void operator()(GLfloat* vtx, float v) {
1862 const GLfloat w = hw_w + (hw_w * v);
1863 const GLfloat h = hw_h - (hw_h * v);
1864 const GLfloat x = (hw_w - w) * 0.5f;
1865 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001866 vtx[0] = x; vtx[1] = y;
1867 vtx[2] = x; vtx[3] = y + h;
1868 vtx[4] = x + w; vtx[5] = y + h;
1869 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001870 }
1871 };
1872
1873 class h_stretch {
1874 const GLfloat hw_w, hw_h;
1875 public:
1876 h_stretch(uint32_t hw_w, uint32_t hw_h)
1877 : hw_w(hw_w), hw_h(hw_h) {
1878 }
1879 void operator()(GLfloat* vtx, float v) {
1880 const GLfloat w = hw_w - (hw_w * v);
1881 const GLfloat h = 1.0f;
1882 const GLfloat x = (hw_w - w) * 0.5f;
1883 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopianffcf4652011-07-07 17:30:31 -07001884 vtx[0] = x; vtx[1] = y;
1885 vtx[2] = x; vtx[3] = y + h;
1886 vtx[4] = x + w; vtx[5] = y + h;
1887 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001888 }
1889 };
1890
1891 // the full animation is 24 frames
Mathias Agopianffcf4652011-07-07 17:30:31 -07001892 char value[PROPERTY_VALUE_MAX];
1893 property_get("debug.sf.electron_frames", value, "24");
1894 int nbFrames = (atoi(value) + 1) >> 1;
1895 if (nbFrames <= 0) // just in case
1896 nbFrames = 24;
1897
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001898 s_curve_interpolator itr(nbFrames, 7.5f);
1899 s_curve_interpolator itg(nbFrames, 8.0f);
1900 s_curve_interpolator itb(nbFrames, 8.5f);
1901
1902 v_stretch vverts(hw_w, hw_h);
Mathias Agopiana9040d02011-10-10 19:02:07 -07001903
1904 glMatrixMode(GL_TEXTURE);
1905 glLoadIdentity();
1906 glMatrixMode(GL_MODELVIEW);
1907 glLoadIdentity();
1908
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001909 glEnable(GL_BLEND);
1910 glBlendFunc(GL_ONE, GL_ONE);
1911 for (int i=0 ; i<nbFrames ; i++) {
1912 float x, y, w, h;
1913 const float vr = itr(i);
1914 const float vg = itg(i);
1915 const float vb = itb(i);
1916
1917 // clear screen
1918 glColorMask(1,1,1,1);
Mathias Agopian59119e62010-10-11 12:37:43 -07001919 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian59119e62010-10-11 12:37:43 -07001920 glEnable(GL_TEXTURE_2D);
Mathias Agopian59119e62010-10-11 12:37:43 -07001921
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001922 // draw the red plane
1923 vverts(vtx, vr);
1924 glColorMask(1,0,0,1);
1925 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001926
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001927 // draw the green plane
1928 vverts(vtx, vg);
1929 glColorMask(0,1,0,1);
1930 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001931
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001932 // draw the blue plane
1933 vverts(vtx, vb);
1934 glColorMask(0,0,1,1);
1935 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopian59119e62010-10-11 12:37:43 -07001936
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001937 // draw the white highlight (we use the last vertices)
Mathias Agopian59119e62010-10-11 12:37:43 -07001938 glDisable(GL_TEXTURE_2D);
1939 glColorMask(1,1,1,1);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001940 glColor4f(vg, vg, vg, 1);
1941 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1942 hw.flip(screenBounds);
Mathias Agopian59119e62010-10-11 12:37:43 -07001943 }
1944
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001945 h_stretch hverts(hw_w, hw_h);
1946 glDisable(GL_BLEND);
1947 glDisable(GL_TEXTURE_2D);
1948 glColorMask(1,1,1,1);
1949 for (int i=0 ; i<nbFrames ; i++) {
1950 const float v = itg(i);
1951 hverts(vtx, v);
1952 glClear(GL_COLOR_BUFFER_BIT);
1953 glColor4f(1-v, 1-v, 1-v, 1);
1954 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1955 hw.flip(screenBounds);
1956 }
1957
1958 glColorMask(1,1,1,1);
1959 glEnable(GL_SCISSOR_TEST);
1960 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1961 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07001962 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07001963 glDisable(GL_BLEND);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001964 return NO_ERROR;
1965}
1966
1967status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1968{
1969 status_t result = PERMISSION_DENIED;
1970
1971 if (!GLExtensions::getInstance().haveFramebufferObject())
1972 return INVALID_OPERATION;
1973
1974
1975 // get screen geometry
1976 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1977 const uint32_t hw_w = hw.getWidth();
1978 const uint32_t hw_h = hw.getHeight();
1979 const Region screenBounds(hw.bounds());
1980
1981 GLfloat u, v;
1982 GLuint tname;
1983 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1984 if (result != NO_ERROR) {
1985 return result;
1986 }
1987
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001988 GLfloat vtx[8];
1989 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian9daa5c92010-10-12 16:05:48 -07001990 glBindTexture(GL_TEXTURE_2D, tname);
1991 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1992 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1993 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1994 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1995 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1996 glVertexPointer(2, GL_FLOAT, 0, vtx);
1997
1998 class s_curve_interpolator {
1999 const float nbFrames, s, v;
2000 public:
2001 s_curve_interpolator(int nbFrames, float s)
2002 : nbFrames(1.0f / (nbFrames-1)), s(s),
2003 v(1.0f + expf(-s + 0.5f*s)) {
2004 }
2005 float operator()(int f) {
2006 const float x = f * nbFrames;
2007 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
2008 }
2009 };
2010
2011 class v_stretch {
2012 const GLfloat hw_w, hw_h;
2013 public:
2014 v_stretch(uint32_t hw_w, uint32_t hw_h)
2015 : hw_w(hw_w), hw_h(hw_h) {
2016 }
2017 void operator()(GLfloat* vtx, float v) {
2018 const GLfloat w = hw_w + (hw_w * v);
2019 const GLfloat h = hw_h - (hw_h * v);
2020 const GLfloat x = (hw_w - w) * 0.5f;
2021 const GLfloat y = (hw_h - h) * 0.5f;
2022 vtx[0] = x; vtx[1] = y;
2023 vtx[2] = x; vtx[3] = y + h;
2024 vtx[4] = x + w; vtx[5] = y + h;
2025 vtx[6] = x + w; vtx[7] = y;
2026 }
2027 };
2028
2029 class h_stretch {
2030 const GLfloat hw_w, hw_h;
2031 public:
2032 h_stretch(uint32_t hw_w, uint32_t hw_h)
2033 : hw_w(hw_w), hw_h(hw_h) {
2034 }
2035 void operator()(GLfloat* vtx, float v) {
2036 const GLfloat w = hw_w - (hw_w * v);
2037 const GLfloat h = 1.0f;
2038 const GLfloat x = (hw_w - w) * 0.5f;
2039 const GLfloat y = (hw_h - h) * 0.5f;
2040 vtx[0] = x; vtx[1] = y;
2041 vtx[2] = x; vtx[3] = y + h;
2042 vtx[4] = x + w; vtx[5] = y + h;
2043 vtx[6] = x + w; vtx[7] = y;
2044 }
2045 };
2046
Mathias Agopiana6546e52010-10-14 12:33:07 -07002047 // the full animation is 12 frames
2048 int nbFrames = 8;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002049 s_curve_interpolator itr(nbFrames, 7.5f);
2050 s_curve_interpolator itg(nbFrames, 8.0f);
2051 s_curve_interpolator itb(nbFrames, 8.5f);
2052
2053 h_stretch hverts(hw_w, hw_h);
2054 glDisable(GL_BLEND);
2055 glDisable(GL_TEXTURE_2D);
2056 glColorMask(1,1,1,1);
2057 for (int i=nbFrames-1 ; i>=0 ; i--) {
2058 const float v = itg(i);
2059 hverts(vtx, v);
2060 glClear(GL_COLOR_BUFFER_BIT);
2061 glColor4f(1-v, 1-v, 1-v, 1);
2062 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2063 hw.flip(screenBounds);
2064 }
2065
Mathias Agopiana6546e52010-10-14 12:33:07 -07002066 nbFrames = 4;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002067 v_stretch vverts(hw_w, hw_h);
2068 glEnable(GL_BLEND);
2069 glBlendFunc(GL_ONE, GL_ONE);
2070 for (int i=nbFrames-1 ; i>=0 ; i--) {
2071 float x, y, w, h;
2072 const float vr = itr(i);
2073 const float vg = itg(i);
2074 const float vb = itb(i);
2075
2076 // clear screen
2077 glColorMask(1,1,1,1);
2078 glClear(GL_COLOR_BUFFER_BIT);
2079 glEnable(GL_TEXTURE_2D);
2080
2081 // draw the red plane
2082 vverts(vtx, vr);
2083 glColorMask(1,0,0,1);
2084 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2085
2086 // draw the green plane
2087 vverts(vtx, vg);
2088 glColorMask(0,1,0,1);
2089 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2090
2091 // draw the blue plane
2092 vverts(vtx, vb);
2093 glColorMask(0,0,1,1);
2094 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2095
2096 hw.flip(screenBounds);
2097 }
2098
2099 glColorMask(1,1,1,1);
2100 glEnable(GL_SCISSOR_TEST);
2101 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian59119e62010-10-11 12:37:43 -07002102 glDeleteTextures(1, &tname);
Mathias Agopiana67932f2011-04-20 14:20:59 -07002103 glDisable(GL_TEXTURE_2D);
Mathias Agopianc492e672011-10-18 14:49:27 -07002104 glDisable(GL_BLEND);
Mathias Agopian59119e62010-10-11 12:37:43 -07002105
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002106 return NO_ERROR;
2107}
2108
2109// ---------------------------------------------------------------------------
2110
Mathias Agopianabd671a2010-10-14 14:54:06 -07002111status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002112{
2113 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2114 if (!hw.canDraw()) {
2115 // we're already off
2116 return NO_ERROR;
2117 }
Mathias Agopian7ee4cd52011-09-02 12:22:39 -07002118
2119 // turn off hwc while we're doing the animation
2120 hw.getHwComposer().disable();
2121 // and make sure to turn it back on (if needed) next time we compose
2122 invalidateHwcGeometry();
2123
Mathias Agopianabd671a2010-10-14 14:54:06 -07002124 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2125 electronBeamOffAnimationImplLocked();
2126 }
2127
2128 // always clear the whole screen at the end of the animation
2129 glClearColor(0,0,0,1);
2130 glDisable(GL_SCISSOR_TEST);
2131 glClear(GL_COLOR_BUFFER_BIT);
2132 glEnable(GL_SCISSOR_TEST);
2133 hw.flip( Region(hw.bounds()) );
2134
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002135 return NO_ERROR;
Mathias Agopian59119e62010-10-11 12:37:43 -07002136}
2137
2138status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2139{
Mathias Agopian59119e62010-10-11 12:37:43 -07002140 class MessageTurnElectronBeamOff : public MessageBase {
2141 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002142 int32_t mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002143 status_t result;
2144 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002145 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2146 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian59119e62010-10-11 12:37:43 -07002147 }
2148 status_t getResult() const {
2149 return result;
2150 }
2151 virtual bool handler() {
2152 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002153 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002154 return true;
2155 }
2156 };
2157
Mathias Agopianabd671a2010-10-14 14:54:06 -07002158 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopian59119e62010-10-11 12:37:43 -07002159 status_t res = postMessageSync(msg);
2160 if (res == NO_ERROR) {
2161 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002162
2163 // work-around: when the power-manager calls us we activate the
2164 // animation. eventually, the "on" animation will be called
2165 // by the power-manager itself
Mathias Agopianabd671a2010-10-14 14:54:06 -07002166 mElectronBeamAnimationMode = mode;
Mathias Agopian59119e62010-10-11 12:37:43 -07002167 }
2168 return res;
2169}
2170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002171// ---------------------------------------------------------------------------
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002172
Mathias Agopianabd671a2010-10-14 14:54:06 -07002173status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002174{
2175 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2176 if (hw.canDraw()) {
2177 // we're already on
2178 return NO_ERROR;
2179 }
Mathias Agopianabd671a2010-10-14 14:54:06 -07002180 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2181 electronBeamOnAnimationImplLocked();
2182 }
Mathias Agopiana7f03732010-10-14 12:46:24 -07002183
2184 // make sure to redraw the whole screen when the animation is done
2185 mDirtyRegion.set(hw.bounds());
2186 signalEvent();
2187
Mathias Agopian015fb3f2010-10-14 12:19:37 -07002188 return NO_ERROR;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002189}
2190
2191status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2192{
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002193 class MessageTurnElectronBeamOn : public MessageBase {
2194 SurfaceFlinger* flinger;
Mathias Agopianabd671a2010-10-14 14:54:06 -07002195 int32_t mode;
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002196 status_t result;
2197 public:
Mathias Agopianabd671a2010-10-14 14:54:06 -07002198 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2199 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002200 }
2201 status_t getResult() const {
2202 return result;
2203 }
2204 virtual bool handler() {
2205 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopianabd671a2010-10-14 14:54:06 -07002206 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002207 return true;
2208 }
2209 };
2210
Mathias Agopianabd671a2010-10-14 14:54:06 -07002211 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002212 return NO_ERROR;
2213}
2214
2215// ---------------------------------------------------------------------------
2216
Mathias Agopian74c40c02010-09-29 13:02:36 -07002217status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2218 sp<IMemoryHeap>* heap,
2219 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002220 uint32_t sw, uint32_t sh,
2221 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian74c40c02010-09-29 13:02:36 -07002222{
2223 status_t result = PERMISSION_DENIED;
2224
2225 // only one display supported for now
2226 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2227 return BAD_VALUE;
2228
2229 if (!GLExtensions::getInstance().haveFramebufferObject())
2230 return INVALID_OPERATION;
2231
2232 // get screen geometry
2233 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2234 const uint32_t hw_w = hw.getWidth();
2235 const uint32_t hw_h = hw.getHeight();
2236
2237 if ((sw > hw_w) || (sh > hw_h))
2238 return BAD_VALUE;
2239
2240 sw = (!sw) ? hw_w : sw;
2241 sh = (!sh) ? hw_h : sh;
2242 const size_t size = sw * sh * 4;
2243
Mathias Agopian1c71a472011-03-02 18:45:50 -08002244 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2245 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002246
Mathias Agopian74c40c02010-09-29 13:02:36 -07002247 // make sure to clear all GL error flags
2248 while ( glGetError() != GL_NO_ERROR ) ;
2249
2250 // create a FBO
2251 GLuint name, tname;
2252 glGenRenderbuffersOES(1, &tname);
2253 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2254 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2255 glGenFramebuffersOES(1, &name);
2256 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2257 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2258 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2259
2260 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002261
Mathias Agopian74c40c02010-09-29 13:02:36 -07002262 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2263
2264 // invert everything, b/c glReadPixel() below will invert the FB
2265 glViewport(0, 0, sw, sh);
Mathias Agopianf653b892010-12-16 18:46:17 -08002266 glScissor(0, 0, sw, sh);
Mathias Agopianffcf4652011-07-07 17:30:31 -07002267 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002268 glMatrixMode(GL_PROJECTION);
2269 glPushMatrix();
2270 glLoadIdentity();
Mathias Agopianffcf4652011-07-07 17:30:31 -07002271 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002272 glMatrixMode(GL_MODELVIEW);
2273
2274 // redraw the screen entirely...
2275 glClearColor(0,0,0,1);
2276 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianf653b892010-12-16 18:46:17 -08002277
Jamie Gennis9575f602011-10-07 14:51:16 -07002278 const LayerVector& layers(mDrawingState.layersSortedByZ);
2279 const size_t count = layers.size();
Mathias Agopian74c40c02010-09-29 13:02:36 -07002280 for (size_t i=0 ; i<count ; ++i) {
2281 const sp<LayerBase>& layer(layers[i]);
Mathias Agopianb0610332011-08-25 14:36:43 -07002282 const uint32_t flags = layer->drawingState().flags;
2283 if (!(flags & ISurfaceComposer::eLayerHidden)) {
2284 const uint32_t z = layer->drawingState().z;
2285 if (z >= minLayerZ && z <= maxLayerZ) {
2286 layer->drawForSreenShot();
2287 }
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002288 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002289 }
2290
2291 // XXX: this is needed on tegra
Mathias Agopianffcf4652011-07-07 17:30:31 -07002292 glEnable(GL_SCISSOR_TEST);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002293 glScissor(0, 0, sw, sh);
2294
2295 // check for errors and return screen capture
2296 if (glGetError() != GL_NO_ERROR) {
2297 // error while rendering
2298 result = INVALID_OPERATION;
2299 } else {
2300 // allocate shared memory large enough to hold the
2301 // screen capture
2302 sp<MemoryHeapBase> base(
2303 new MemoryHeapBase(size, 0, "screen-capture") );
2304 void* const ptr = base->getBase();
2305 if (ptr) {
2306 // capture the screen with glReadPixels()
2307 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2308 if (glGetError() == GL_NO_ERROR) {
2309 *heap = base;
2310 *w = sw;
2311 *h = sh;
2312 *f = PIXEL_FORMAT_RGBA_8888;
2313 result = NO_ERROR;
2314 }
2315 } else {
2316 result = NO_MEMORY;
2317 }
2318 }
Mathias Agopian74c40c02010-09-29 13:02:36 -07002319 glEnable(GL_SCISSOR_TEST);
2320 glViewport(0, 0, hw_w, hw_h);
2321 glMatrixMode(GL_PROJECTION);
2322 glPopMatrix();
2323 glMatrixMode(GL_MODELVIEW);
Mathias Agopian74c40c02010-09-29 13:02:36 -07002324 } else {
2325 result = BAD_VALUE;
2326 }
2327
2328 // release FBO resources
2329 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2330 glDeleteRenderbuffersOES(1, &tname);
2331 glDeleteFramebuffersOES(1, &name);
Mathias Agopiane6f09842010-12-15 14:41:59 -08002332
2333 hw.compositionComplete();
2334
Mathias Agopian1c71a472011-03-02 18:45:50 -08002335 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002336
Mathias Agopian74c40c02010-09-29 13:02:36 -07002337 return result;
2338}
2339
2340
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002341status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2342 sp<IMemoryHeap>* heap,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002343 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002344 uint32_t sw, uint32_t sh,
2345 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002346{
2347 // only one display supported for now
2348 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2349 return BAD_VALUE;
2350
2351 if (!GLExtensions::getInstance().haveFramebufferObject())
2352 return INVALID_OPERATION;
2353
2354 class MessageCaptureScreen : public MessageBase {
2355 SurfaceFlinger* flinger;
2356 DisplayID dpy;
2357 sp<IMemoryHeap>* heap;
2358 uint32_t* w;
2359 uint32_t* h;
2360 PixelFormat* f;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002361 uint32_t sw;
2362 uint32_t sh;
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002363 uint32_t minLayerZ;
2364 uint32_t maxLayerZ;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002365 status_t result;
2366 public:
2367 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian74c40c02010-09-29 13:02:36 -07002368 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002369 uint32_t sw, uint32_t sh,
2370 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002371 : flinger(flinger), dpy(dpy),
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002372 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2373 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2374 result(PERMISSION_DENIED)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002375 {
2376 }
2377 status_t getResult() const {
2378 return result;
2379 }
2380 virtual bool handler() {
2381 Mutex::Autolock _l(flinger->mStateLock);
2382
2383 // if we have secure windows, never allow the screen capture
2384 if (flinger->mSecureFrameBuffer)
2385 return true;
2386
Mathias Agopian74c40c02010-09-29 13:02:36 -07002387 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002388 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002389
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002390 return true;
2391 }
2392 };
2393
2394 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -08002395 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002396 status_t res = postMessageSync(msg);
2397 if (res == NO_ERROR) {
2398 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2399 }
2400 return res;
2401}
2402
2403// ---------------------------------------------------------------------------
2404
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002405sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2406{
2407 sp<Layer> result;
2408 Mutex::Autolock _l(mStateLock);
2409 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2410 return result;
2411}
2412
2413// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002414
Mathias Agopian96f08192010-06-02 23:28:45 -07002415Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002416 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002417{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002418}
2419
Mathias Agopian96f08192010-06-02 23:28:45 -07002420Client::~Client()
2421{
Mathias Agopian96f08192010-06-02 23:28:45 -07002422 const size_t count = mLayers.size();
2423 for (size_t i=0 ; i<count ; i++) {
2424 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2425 if (layer != 0) {
2426 mFlinger->removeLayer(layer);
2427 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428 }
2429}
2430
Mathias Agopian96f08192010-06-02 23:28:45 -07002431status_t Client::initCheck() const {
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002432 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002433}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002434
Mathias Agopian4f113742011-05-03 16:21:41 -07002435size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002436{
Mathias Agopian4f113742011-05-03 16:21:41 -07002437 Mutex::Autolock _l(mLock);
2438 size_t name = mNameGenerator++;
Mathias Agopian96f08192010-06-02 23:28:45 -07002439 mLayers.add(name, layer);
2440 return name;
2441}
2442
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002443void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian96f08192010-06-02 23:28:45 -07002444{
Mathias Agopian4f113742011-05-03 16:21:41 -07002445 Mutex::Autolock _l(mLock);
Mathias Agopian96f08192010-06-02 23:28:45 -07002446 // we do a linear search here, because this doesn't happen often
2447 const size_t count = mLayers.size();
2448 for (size_t i=0 ; i<count ; i++) {
2449 if (mLayers.valueAt(i) == layer) {
2450 mLayers.removeItemsAt(i, 1);
2451 break;
2452 }
2453 }
2454}
Mathias Agopian4f113742011-05-03 16:21:41 -07002455sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2456{
2457 Mutex::Autolock _l(mLock);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002458 sp<LayerBaseClient> lbc;
Mathias Agopian4f113742011-05-03 16:21:41 -07002459 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian96f08192010-06-02 23:28:45 -07002460 if (layer != 0) {
2461 lbc = layer.promote();
2462 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002463 }
2464 return lbc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002465}
2466
Mathias Agopiana67932f2011-04-20 14:20:59 -07002467
2468status_t Client::onTransact(
2469 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2470{
2471 // these must be checked
2472 IPCThreadState* ipc = IPCThreadState::self();
2473 const int pid = ipc->getCallingPid();
2474 const int uid = ipc->getCallingUid();
2475 const int self_pid = getpid();
2476 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2477 // we're called from a different process, do the real check
Mathias Agopian99b49842011-06-27 16:05:52 -07002478 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopiana67932f2011-04-20 14:20:59 -07002479 {
2480 LOGE("Permission Denial: "
2481 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2482 return PERMISSION_DENIED;
2483 }
2484 }
2485 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002486}
Mathias Agopiana67932f2011-04-20 14:20:59 -07002487
2488
Mathias Agopian96f08192010-06-02 23:28:45 -07002489sp<ISurface> Client::createSurface(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002490 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002491 const String8& name,
2492 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 uint32_t flags)
2494{
Mathias Agopiana67932f2011-04-20 14:20:59 -07002495 /*
2496 * createSurface must be called from the GL thread so that it can
2497 * have access to the GL context.
2498 */
2499
2500 class MessageCreateSurface : public MessageBase {
2501 sp<ISurface> result;
2502 SurfaceFlinger* flinger;
2503 ISurfaceComposerClient::surface_data_t* params;
2504 Client* client;
2505 const String8& name;
2506 DisplayID display;
2507 uint32_t w, h;
2508 PixelFormat format;
2509 uint32_t flags;
2510 public:
2511 MessageCreateSurface(SurfaceFlinger* flinger,
2512 ISurfaceComposerClient::surface_data_t* params,
2513 const String8& name, Client* client,
2514 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2515 uint32_t flags)
2516 : flinger(flinger), params(params), client(client), name(name),
2517 display(display), w(w), h(h), format(format), flags(flags)
2518 {
2519 }
2520 sp<ISurface> getResult() const { return result; }
2521 virtual bool handler() {
2522 result = flinger->createSurface(params, name, client,
2523 display, w, h, format, flags);
2524 return true;
2525 }
2526 };
2527
2528 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2529 params, name, this, display, w, h, format, flags);
2530 mFlinger->postMessageSync(msg);
2531 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532}
Mathias Agopian96f08192010-06-02 23:28:45 -07002533status_t Client::destroySurface(SurfaceID sid) {
2534 return mFlinger->removeSurface(this, sid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002535}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536
2537// ---------------------------------------------------------------------------
2538
Jamie Gennis9a78c902011-01-12 18:30:40 -08002539GraphicBufferAlloc::GraphicBufferAlloc() {}
2540
2541GraphicBufferAlloc::~GraphicBufferAlloc() {}
2542
2543sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002544 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennis9a78c902011-01-12 18:30:40 -08002545 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2546 status_t err = graphicBuffer->initCheck();
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002547 *error = err;
Mathias Agopiana67932f2011-04-20 14:20:59 -07002548 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopiand9e8c642011-07-01 14:53:49 -07002549 if (err == NO_MEMORY) {
2550 GraphicBuffer::dumpAllocationsToSystemLog();
2551 }
Mathias Agopiana67932f2011-04-20 14:20:59 -07002552 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2553 "failed (%s), handle=%p",
2554 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennis9a78c902011-01-12 18:30:40 -08002555 return 0;
2556 }
Jamie Gennis9a78c902011-01-12 18:30:40 -08002557 return graphicBuffer;
2558}
2559
Jamie Gennis9a78c902011-01-12 18:30:40 -08002560// ---------------------------------------------------------------------------
2561
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562GraphicPlane::GraphicPlane()
2563 : mHw(0)
2564{
2565}
2566
2567GraphicPlane::~GraphicPlane() {
2568 delete mHw;
2569}
2570
2571bool GraphicPlane::initialized() const {
2572 return mHw ? true : false;
2573}
2574
Mathias Agopian2b92d892010-02-08 15:49:35 -08002575int GraphicPlane::getWidth() const {
2576 return mWidth;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577}
2578
Mathias Agopian2b92d892010-02-08 15:49:35 -08002579int GraphicPlane::getHeight() const {
2580 return mHeight;
2581}
2582
2583void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2584{
2585 mHw = hw;
2586
2587 // initialize the display orientation transform.
2588 // it's a constant that should come from the display driver.
2589 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2590 char property[PROPERTY_VALUE_MAX];
2591 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2592 //displayOrientation
2593 switch (atoi(property)) {
2594 case 90:
2595 displayOrientation = ISurfaceComposer::eOrientation90;
2596 break;
2597 case 270:
2598 displayOrientation = ISurfaceComposer::eOrientation270;
2599 break;
2600 }
2601 }
2602
2603 const float w = hw->getWidth();
2604 const float h = hw->getHeight();
2605 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2606 &mDisplayTransform);
2607 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2608 mDisplayWidth = h;
2609 mDisplayHeight = w;
2610 } else {
2611 mDisplayWidth = w;
2612 mDisplayHeight = h;
2613 }
2614
2615 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002616}
2617
2618status_t GraphicPlane::orientationToTransfrom(
2619 int orientation, int w, int h, Transform* tr)
Mathias Agopianeda65402010-02-22 03:15:57 -08002620{
2621 uint32_t flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622 switch (orientation) {
2623 case ISurfaceComposer::eOrientationDefault:
Mathias Agopianeda65402010-02-22 03:15:57 -08002624 flags = Transform::ROT_0;
2625 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002626 case ISurfaceComposer::eOrientation90:
Mathias Agopianeda65402010-02-22 03:15:57 -08002627 flags = Transform::ROT_90;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628 break;
2629 case ISurfaceComposer::eOrientation180:
Mathias Agopianeda65402010-02-22 03:15:57 -08002630 flags = Transform::ROT_180;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631 break;
2632 case ISurfaceComposer::eOrientation270:
Mathias Agopianeda65402010-02-22 03:15:57 -08002633 flags = Transform::ROT_270;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634 break;
2635 default:
2636 return BAD_VALUE;
2637 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002638 tr->set(flags, w, h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639 return NO_ERROR;
2640}
2641
2642status_t GraphicPlane::setOrientation(int orientation)
2643{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644 // If the rotation can be handled in hardware, this is where
2645 // the magic should happen.
Mathias Agopian2b92d892010-02-08 15:49:35 -08002646
2647 const DisplayHardware& hw(displayHardware());
2648 const float w = mDisplayWidth;
2649 const float h = mDisplayHeight;
2650 mWidth = int(w);
2651 mHeight = int(h);
2652
2653 Transform orientationTransform;
Mathias Agopianeda65402010-02-22 03:15:57 -08002654 GraphicPlane::orientationToTransfrom(orientation, w, h,
2655 &orientationTransform);
2656 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2657 mWidth = int(h);
2658 mHeight = int(w);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002659 }
Mathias Agopianeda65402010-02-22 03:15:57 -08002660
Mathias Agopian0d1318b2009-03-27 17:58:20 -07002661 mOrientation = orientation;
Mathias Agopian2b92d892010-02-08 15:49:35 -08002662 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663 return NO_ERROR;
2664}
2665
2666const DisplayHardware& GraphicPlane::displayHardware() const {
2667 return *mHw;
2668}
2669
Mathias Agopian59119e62010-10-11 12:37:43 -07002670DisplayHardware& GraphicPlane::editDisplayHardware() {
2671 return *mHw;
2672}
2673
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002674const Transform& GraphicPlane::transform() const {
2675 return mGlobalTransform;
2676}
2677
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002678EGLDisplay GraphicPlane::getEGLDisplay() const {
2679 return mHw->getEGLDisplay();
2680}
2681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002682// ---------------------------------------------------------------------------
2683
2684}; // namespace android