blob: 1f27a70f696f480190cd9f639845eecc39250b8d [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-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 Queru20763732009-01-26 11:51:12 -080024#include <limits.h>
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopiand763b5d2009-07-02 18:11:53 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/String8.h>
38#include <utils/String16.h>
39#include <utils/StopWatch.h>
40
Mathias Agopian6950e422009-10-05 17:07:12 -070041#include <ui/GraphicBufferAllocator.h>
Mathias Agopian04262e92010-09-13 22:57:58 -070042#include <ui/GraphicLog.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45#include <pixelflinger/pixelflinger.h>
46#include <GLES/gl.h>
47
48#include "clz.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070049#include "GLExtensions.h"
Mathias Agopian93d75ec2011-08-15 20:44:40 -070050#include "DdmConnection.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include "Layer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052#include "LayerDim.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053#include "SurfaceFlinger.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55#include "DisplayHardware/DisplayHardware.h"
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070056#include "DisplayHardware/HWComposer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Mathias Agopian7bb843c2011-04-20 14:20:59 -070058#include <private/surfaceflinger/SharedBufferStack.h>
59
Mathias Agopian627e7b52009-05-21 19:21:59 -070060/* ideally AID_GRAPHICS would be in a semi-public header
61 * or there would be a way to map a user/group name to its id
62 */
63#ifndef AID_GRAPHICS
64#define AID_GRAPHICS 1003
65#endif
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067#define DISPLAY_COUNT 1
68
69namespace android {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070// ---------------------------------------------------------------------------
71
Mathias Agopian0dd593f2011-06-27 16:05:52 -070072const String16 sHardwareTest("android.permission.HARDWARE_TEST");
73const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
74const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
75const String16 sDump("android.permission.DUMP");
76
77// ---------------------------------------------------------------------------
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079SurfaceFlinger::SurfaceFlinger()
80 : BnSurfaceComposer(), Thread(false),
81 mTransactionFlags(0),
Mathias Agopian9779b222009-09-07 16:32:45 -070082 mResizeTransationPending(false),
Mathias Agopian1473f462009-04-10 14:24:30 -070083 mLayersRemoved(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 mBootTime(systemTime()),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 mVisibleRegionsDirty(false),
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -070086 mHwWorkListDirty(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mDeferReleaseConsole(false),
88 mFreezeDisplay(false),
Mathias Agopiand4e03f32010-10-14 14:54:06 -070089 mElectronBeamAnimationMode(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 mFreezeCount(0),
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070091 mFreezeDisplayTime(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 mDebugRegion(0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 mDebugBackground(0),
Mathias Agopian93d75ec2011-08-15 20:44:40 -070094 mDebugDDMS(0),
Mathias Agopian6a969242010-09-22 18:58:01 -070095 mDebugDisableHWC(0),
Mathias Agopiana8d49172009-08-26 16:36:26 -070096 mDebugInSwapBuffers(0),
97 mLastSwapBufferTime(0),
98 mDebugInTransaction(0),
99 mLastTransactionTime(0),
Mathias Agopian6950e422009-10-05 17:07:12 -0700100 mBootFinished(false),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 mConsoleSignals(0),
102 mSecureFrameBuffer(0)
103{
104 init();
105}
106
107void SurfaceFlinger::init()
108{
109 LOGI("SurfaceFlinger is starting");
110
111 // debugging stuff...
112 char value[PROPERTY_VALUE_MAX];
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 property_get("debug.sf.showupdates", value, "0");
115 mDebugRegion = atoi(value);
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 property_get("debug.sf.showbackground", value, "0");
118 mDebugBackground = atoi(value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700120 property_get("debug.sf.ddms", value, "0");
121 mDebugDDMS = atoi(value);
122 if (mDebugDDMS) {
123 DdmConnection::start(getServiceName());
124 }
125
Mathias Agopiane5c0a7b2010-04-20 14:51:04 -0700126 LOGI_IF(mDebugRegion, "showupdates enabled");
127 LOGI_IF(mDebugBackground, "showbackground enabled");
Mathias Agopian93d75ec2011-08-15 20:44:40 -0700128 LOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129}
130
131SurfaceFlinger::~SurfaceFlinger()
132{
133 glDeleteTextures(1, &mWormholeTexName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700136sp<IMemoryHeap> SurfaceFlinger::getCblk() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137{
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700138 return mServerHeap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139}
140
Mathias Agopian770492c2010-05-28 14:22:23 -0700141sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142{
Mathias Agopian593c05c2010-06-02 23:28:45 -0700143 sp<ISurfaceComposerClient> bclient;
144 sp<Client> client(new Client(this));
145 status_t err = client->initCheck();
146 if (err == NO_ERROR) {
147 bclient = client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 return bclient;
150}
151
Jamie Gennisf7acf162011-01-12 18:30:40 -0800152sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
153{
154 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
155 return gba;
156}
Mathias Agopian7623da42010-06-01 15:12:58 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
159{
160 LOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
161 const GraphicPlane& plane(mGraphicPlanes[dpy]);
162 return plane;
163}
164
165GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
166{
167 return const_cast<GraphicPlane&>(
168 const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
169}
170
171void SurfaceFlinger::bootFinished()
172{
173 const nsecs_t now = systemTime();
174 const nsecs_t duration = now - mBootTime;
Andreas Hubere3c01832010-08-16 08:49:37 -0700175 LOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian6950e422009-10-05 17:07:12 -0700176 mBootFinished = true;
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700177
178 // wait patiently for the window manager death
179 const String16 name("window");
180 sp<IBinder> window(defaultServiceManager()->getService(name));
181 if (window != 0) {
182 window->linkToDeath(this);
183 }
184
185 // stop boot animation
Mathias Agopian627e7b52009-05-21 19:21:59 -0700186 property_set("ctl.stop", "bootanim");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187}
188
Mathias Agopian0c63ef52011-07-01 17:08:43 -0700189void SurfaceFlinger::binderDied(const wp<IBinder>& who)
190{
191 // the window manager died on us. prepare its eulogy.
192
193 // unfreeze the screen in case it was... frozen
194 mFreezeDisplayTime = 0;
195 mFreezeCount = 0;
196 mFreezeDisplay = false;
197
198 // reset screen orientation
199 setOrientation(0, eOrientationDefault, 0);
200
201 // restart the boot-animation
202 property_set("ctl.start", "bootanim");
203}
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205void SurfaceFlinger::onFirstRef()
206{
207 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
208
209 // Wait for the main thread to be done with its initialization
210 mReadyToRunBarrier.wait();
211}
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213static inline uint16_t pack565(int r, int g, int b) {
214 return (r<<11)|(g<<5)|b;
215}
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217status_t SurfaceFlinger::readyToRun()
218{
219 LOGI( "SurfaceFlinger's main thread ready to run. "
220 "Initializing graphics H/W...");
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 // we only support one display currently
223 int dpy = 0;
224
225 {
226 // initialize the main display
227 GraphicPlane& plane(graphicPlane(dpy));
228 DisplayHardware* const hw = new DisplayHardware(this, dpy);
229 plane.setDisplayHardware(hw);
230 }
231
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700232 // create the shared control-block
233 mServerHeap = new MemoryHeapBase(4096,
234 MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
235 LOGE_IF(mServerHeap==0, "can't create shared memory dealer");
Andreas Hubere3c01832010-08-16 08:49:37 -0700236
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700237 mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
238 LOGE_IF(mServerCblk==0, "can't get to shared control block's address");
Andreas Hubere3c01832010-08-16 08:49:37 -0700239
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700240 new(mServerCblk) surface_flinger_cblk_t;
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // initialize primary screen
243 // (other display should be initialized in the same manner, but
244 // asynchronously, as they could come and go. None of this is supported
245 // yet).
246 const GraphicPlane& plane(graphicPlane(dpy));
247 const DisplayHardware& hw = plane.displayHardware();
248 const uint32_t w = hw.getWidth();
249 const uint32_t h = hw.getHeight();
250 const uint32_t f = hw.getFormat();
251 hw.makeCurrent();
252
253 // initialize the shared control block
254 mServerCblk->connected |= 1<<dpy;
255 display_cblk_t* dcblk = mServerCblk->displays + dpy;
256 memset(dcblk, 0, sizeof(display_cblk_t));
Mathias Agopian66c77a52010-02-08 15:49:35 -0800257 dcblk->w = plane.getWidth();
258 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 dcblk->format = f;
260 dcblk->orientation = ISurfaceComposer::eOrientationDefault;
261 dcblk->xdpi = hw.getDpiX();
262 dcblk->ydpi = hw.getDpiY();
263 dcblk->fps = hw.getRefreshRate();
264 dcblk->density = hw.getDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
266 // Initialize OpenGL|ES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Andreas Hubere3c01832010-08-16 08:49:37 -0700268 glPixelStorei(GL_PACK_ALIGNMENT, 4);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 glEnableClientState(GL_VERTEX_ARRAY);
270 glEnable(GL_SCISSOR_TEST);
271 glShadeModel(GL_FLAT);
272 glDisable(GL_DITHER);
273 glDisable(GL_CULL_FACE);
274
275 const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
276 const uint16_t g1 = pack565(0x17,0x2f,0x17);
277 const uint16_t textureData[4] = { g0, g1, g1, g0 };
278 glGenTextures(1, &mWormholeTexName);
279 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
280 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
282 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
283 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
284 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
285 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, textureData);
286
287 glViewport(0, 0, w, h);
288 glMatrixMode(GL_PROJECTION);
289 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -0700290 // put the origin in the left-bottom corner
291 glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 mReadyToRunBarrier.open();
294
295 /*
296 * We're now ready to accept clients...
297 */
298
Mathias Agopian627e7b52009-05-21 19:21:59 -0700299 // start boot animation
300 property_set("ctl.start", "bootanim");
Andreas Hubere3c01832010-08-16 08:49:37 -0700301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 return NO_ERROR;
303}
304
305// ----------------------------------------------------------------------------
306#if 0
307#pragma mark -
308#pragma mark Events Handler
309#endif
310
311void SurfaceFlinger::waitForEvent()
312{
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700313 while (true) {
314 nsecs_t timeout = -1;
Mathias Agopian0e449762009-12-01 17:23:28 -0800315 const nsecs_t freezeDisplayTimeout = ms2ns(5000);
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700316 if (UNLIKELY(isFrozen())) {
317 // wait 5 seconds
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700318 const nsecs_t now = systemTime();
319 if (mFreezeDisplayTime == 0) {
320 mFreezeDisplayTime = now;
321 }
322 nsecs_t waitTime = freezeDisplayTimeout - (now - mFreezeDisplayTime);
323 timeout = waitTime>0 ? waitTime : 0;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700324 }
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700325
Mathias Agopian898c4c92010-05-18 17:06:55 -0700326 sp<MessageBase> msg = mEventQueue.waitMessage(timeout);
Mathias Agopian0e449762009-12-01 17:23:28 -0800327
328 // see if we timed out
329 if (isFrozen()) {
330 const nsecs_t now = systemTime();
331 nsecs_t frozenTime = (now - mFreezeDisplayTime);
332 if (frozenTime >= freezeDisplayTimeout) {
333 // we timed out and are still frozen
334 LOGW("timeout expired mFreezeDisplay=%d, mFreezeCount=%d",
335 mFreezeDisplay, mFreezeCount);
336 mFreezeDisplayTime = 0;
337 mFreezeCount = 0;
338 mFreezeDisplay = false;
339 }
340 }
341
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700342 if (msg != 0) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700343 switch (msg->what) {
344 case MessageQueue::INVALIDATE:
345 // invalidate message, just return to the main loop
346 return;
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350}
351
352void SurfaceFlinger::signalEvent() {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700353 mEventQueue.invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354}
355
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700356bool SurfaceFlinger::authenticateSurfaceTexture(
357 const sp<ISurfaceTexture>& surfaceTexture) const {
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800358 Mutex::Autolock _l(mStateLock);
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700359 sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder());
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800360
361 // Check the visible layer list for the ISurface
362 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
363 size_t count = currentLayers.size();
364 for (size_t i=0 ; i<count ; i++) {
365 const sp<LayerBase>& layer(currentLayers[i]);
366 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700367 if (lbc != NULL) {
368 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
369 if (lbcBinder == surfaceTextureBinder) {
370 return true;
371 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800372 }
373 }
374
375 // Check the layers in the purgatory. This check is here so that if a
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700376 // SurfaceTexture gets destroyed before all the clients are done using it,
377 // the error will not be reported as "surface XYZ is not authenticated", but
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800378 // will instead fail later on when the client tries to use the surface,
379 // which should be reported as "surface XYZ returned an -ENODEV". The
380 // purgatorized layers are no less authentic than the visible ones, so this
381 // should not cause any harm.
382 size_t purgatorySize = mLayerPurgatory.size();
383 for (size_t i=0 ; i<purgatorySize ; i++) {
384 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
385 sp<LayerBaseClient> lbc(layer->getLayerBaseClient());
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700386 if (lbc != NULL) {
387 wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder();
388 if (lbcBinder == surfaceTextureBinder) {
389 return true;
390 }
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800391 }
392 }
393
394 return false;
395}
396
Mathias Agopian898c4c92010-05-18 17:06:55 -0700397status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
398 nsecs_t reltime, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399{
Mathias Agopian898c4c92010-05-18 17:06:55 -0700400 return mEventQueue.postMessage(msg, reltime, flags);
401}
402
403status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
404 nsecs_t reltime, uint32_t flags)
405{
406 status_t res = mEventQueue.postMessage(msg, reltime, flags);
407 if (res == NO_ERROR) {
408 msg->wait();
409 }
410 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411}
412
413// ----------------------------------------------------------------------------
414#if 0
415#pragma mark -
416#pragma mark Main loop
417#endif
418
419bool SurfaceFlinger::threadLoop()
420{
421 waitForEvent();
422
423 // check for transactions
424 if (UNLIKELY(mConsoleSignals)) {
425 handleConsoleEvents();
426 }
427
Mathias Agopian439863f2011-06-28 19:09:31 -0700428 // if we're in a global transaction, don't do anything.
429 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
430 uint32_t transactionFlags = peekTransactionFlags(mask);
431 if (UNLIKELY(transactionFlags)) {
432 handleTransaction(transactionFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
435 // post surfaces (if needed)
436 handlePageFlip();
437
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700438 if (UNLIKELY(mHwWorkListDirty)) {
439 // build the h/w work list
440 handleWorkList();
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian81384bf2009-09-27 22:47:27 -0700444 if (LIKELY(hw.canDraw() && !isFrozen())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 // repaint the framebuffer (if needed)
Mathias Agopian04262e92010-09-13 22:57:58 -0700446
447 const int index = hw.getCurrentBufferIndex();
448 GraphicLog& logger(GraphicLog::getInstance());
449
450 logger.log(GraphicLog::SF_REPAINT, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 handleRepaint();
452
Mathias Agopianb1a18742009-09-17 16:18:16 -0700453 // inform the h/w that we're done compositing
Mathias Agopian04262e92010-09-13 22:57:58 -0700454 logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);
Mathias Agopianb1a18742009-09-17 16:18:16 -0700455 hw.compositionComplete();
456
Mathias Agopian04262e92010-09-13 22:57:58 -0700457 logger.log(GraphicLog::SF_SWAP_BUFFERS, index);
Antti Hatala4c0a4a22010-09-08 06:37:14 -0700458 postFramebuffer();
459
Mathias Agopian04262e92010-09-13 22:57:58 -0700460 logger.log(GraphicLog::SF_REPAINT_DONE, index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 } else {
462 // pretend we did the post
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -0800463 hw.compositionComplete();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 usleep(16667); // 60 fps period
465 }
466 return true;
467}
468
469void SurfaceFlinger::postFramebuffer()
470{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 if (!mInvalidRegion.isEmpty()) {
472 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopiana8d49172009-08-26 16:36:26 -0700473 const nsecs_t now = systemTime();
474 mDebugInSwapBuffers = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 hw.flip(mInvalidRegion);
Mathias Agopiana8d49172009-08-26 16:36:26 -0700476 mLastSwapBufferTime = systemTime() - now;
477 mDebugInSwapBuffers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mInvalidRegion.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480}
481
482void SurfaceFlinger::handleConsoleEvents()
483{
484 // something to do with the console
485 const DisplayHardware& hw = graphicPlane(0).displayHardware();
486
487 int what = android_atomic_and(0, &mConsoleSignals);
488 if (what & eConsoleAcquired) {
489 hw.acquireScreen();
Mathias Agopian2d2b8032010-10-12 16:05:48 -0700490 // this is a temporary work-around, eventually this should be called
491 // by the power-manager
Mathias Agopiand4e03f32010-10-14 14:54:06 -0700492 SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
Mathias Agopianaab758e2010-10-11 12:37:43 -0700495 if (mDeferReleaseConsole && hw.isScreenAcquired()) {
Mathias Agopianed81f222009-04-14 23:02:51 -0700496 // We got the release signal before the acquire signal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 mDeferReleaseConsole = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 hw.releaseScreen();
499 }
500
501 if (what & eConsoleReleased) {
Mathias Agopianaab758e2010-10-11 12:37:43 -0700502 if (hw.isScreenAcquired()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 hw.releaseScreen();
504 } else {
505 mDeferReleaseConsole = true;
506 }
507 }
508
509 mDirtyRegion.set(hw.bounds());
510}
511
512void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
513{
Mathias Agopian69608112011-05-19 15:38:14 -0700514 Mutex::Autolock _l(mStateLock);
515 const nsecs_t now = systemTime();
516 mDebugInTransaction = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517
Mathias Agopian69608112011-05-19 15:38:14 -0700518 // Here we're guaranteed that some transaction flags are set
519 // so we can call handleTransactionLocked() unconditionally.
520 // We call getTransactionFlags(), which will also clear the flags,
521 // with mStateLock held to guarantee that mCurrentState won't change
522 // until the transaction is committed.
Mathias Agopianff1d4102010-08-10 17:19:56 -0700523
Mathias Agopian69608112011-05-19 15:38:14 -0700524 const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
525 transactionFlags = getTransactionFlags(mask);
526 handleTransactionLocked(transactionFlags);
Mathias Agopian6dcb1552011-05-03 17:04:02 -0700527
Mathias Agopian69608112011-05-19 15:38:14 -0700528 mLastTransactionTime = systemTime() - now;
529 mDebugInTransaction = 0;
530 invalidateHwcGeometry();
531 // here the transaction has been committed
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700532}
533
Mathias Agopian69608112011-05-19 15:38:14 -0700534void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700535{
536 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 const size_t count = currentLayers.size();
538
539 /*
540 * Traversal of the children
541 * (perform the transaction for each of them if needed)
542 */
543
544 const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
545 if (layersNeedTransaction) {
546 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700547 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
549 if (!trFlags) continue;
550
551 const uint32_t flags = layer->doTransaction(0);
552 if (flags & Layer::eVisibleRegion)
553 mVisibleRegionsDirty = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555 }
556
557 /*
558 * Perform our own transaction if needed
559 */
560
561 if (transactionFlags & eTransactionNeeded) {
562 if (mCurrentState.orientation != mDrawingState.orientation) {
563 // the orientation has changed, recompute all visible regions
564 // and invalidate everything.
565
566 const int dpy = 0;
567 const int orientation = mCurrentState.orientation;
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700568 const uint32_t type = mCurrentState.orientationType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 GraphicPlane& plane(graphicPlane(dpy));
570 plane.setOrientation(orientation);
571
572 // update the shared control block
573 const DisplayHardware& hw(plane.displayHardware());
574 volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
575 dcblk->orientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -0800576 dcblk->w = plane.getWidth();
577 dcblk->h = plane.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
579 mVisibleRegionsDirty = true;
580 mDirtyRegion.set(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
583 if (mCurrentState.freezeDisplay != mDrawingState.freezeDisplay) {
584 // freezing or unfreezing the display -> trigger animation if needed
585 mFreezeDisplay = mCurrentState.freezeDisplay;
Mathias Agopian31901cc2010-03-01 17:51:17 -0800586 if (mFreezeDisplay)
587 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700590 if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
591 // layers have been added
592 mVisibleRegionsDirty = true;
593 }
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 // some layers might have been removed, so
596 // we need to update the regions they're exposing.
Mathias Agopian1473f462009-04-10 14:24:30 -0700597 if (mLayersRemoved) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -0700598 mLayersRemoved = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 mVisibleRegionsDirty = true;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700600 const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
Mathias Agopian2d5ee252009-06-04 18:46:21 -0700601 const size_t count = previousLayers.size();
602 for (size_t i=0 ; i<count ; i++) {
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700603 const sp<LayerBase>& layer(previousLayers[i]);
604 if (currentLayers.indexOf( layer ) < 0) {
605 // this layer is not visible anymore
Mathias Agopian33863dd2009-07-28 14:20:21 -0700606 mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
Mathias Agopiana3aa6c92009-04-22 15:23:34 -0700607 }
608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611
612 commitTransaction();
613}
614
615sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
616{
617 return new FreezeLock(const_cast<SurfaceFlinger *>(this));
618}
619
620void SurfaceFlinger::computeVisibleRegions(
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800621 const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622{
623 const GraphicPlane& plane(graphicPlane(0));
624 const Transform& planeTransform(plane.transform());
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700625 const DisplayHardware& hw(plane.displayHardware());
626 const Region screenRegion(hw.bounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 Region aboveOpaqueLayers;
629 Region aboveCoveredLayers;
630 Region dirty;
631
632 bool secureFrameBuffer = false;
633
634 size_t i = currentLayers.size();
635 while (i--) {
Mathias Agopian1473f462009-04-10 14:24:30 -0700636 const sp<LayerBase>& layer = currentLayers[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 layer->validateVisibility(planeTransform);
638
639 // start with the whole surface at its current location
Mathias Agopian12cedff2009-07-28 10:57:27 -0700640 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700642 /*
643 * opaqueRegion: area of a surface that is fully opaque.
644 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 Region opaqueRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700646
647 /*
648 * visibleRegion: area of a surface that is visible on screen
649 * and not fully transparent. This is essentially the layer's
650 * footprint minus the opaque regions above it.
651 * Areas covered by a translucent surface are considered visible.
652 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 Region visibleRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700654
655 /*
656 * coveredRegion: area of a surface that is covered by all
657 * visible regions above it (which includes the translucent areas).
658 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 Region coveredRegion;
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700660
661
662 // handle hidden surfaces by setting the visible region to empty
Mathias Agopian12cedff2009-07-28 10:57:27 -0700663 if (LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700664 const bool translucent = !layer->isOpaque();
Mathias Agopian12cedff2009-07-28 10:57:27 -0700665 const Rect bounds(layer->visibleBounds());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 visibleRegion.set(bounds);
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700667 visibleRegion.andSelf(screenRegion);
668 if (!visibleRegion.isEmpty()) {
669 // Remove the transparent area from the visible region
670 if (translucent) {
671 visibleRegion.subtractSelf(layer->transparentRegionScreen);
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700674 // compute the opaque region
675 const int32_t layerOrientation = layer->getOrientation();
676 if (s.alpha==255 && !translucent &&
677 ((layerOrientation & Transform::ROT_INVALID) == false)) {
678 // the opaque region is the layer's footprint
679 opaqueRegion = visibleRegion;
680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
683
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700684 // Clip the covered region to the visible region
685 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
686
687 // Update aboveCoveredLayers for next (lower) layer
688 aboveCoveredLayers.orSelf(visibleRegion);
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 // subtract the opaque region covered by the layers above us
691 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
693 // compute this layer's dirty region
694 if (layer->contentDirty) {
695 // we need to invalidate the whole region
696 dirty = visibleRegion;
697 // as well, as the old visible region
698 dirty.orSelf(layer->visibleRegionScreen);
699 layer->contentDirty = false;
700 } else {
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700701 /* compute the exposed region:
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700702 * the exposed region consists of two components:
703 * 1) what's VISIBLE now and was COVERED before
704 * 2) what's EXPOSED now less what was EXPOSED before
705 *
706 * note that (1) is conservative, we start with the whole
707 * visible region but only keep what used to be covered by
708 * something -- which mean it may have been exposed.
709 *
710 * (2) handles areas that were not covered by anything but got
711 * exposed because of a resize.
Mathias Agopian0aed7e92009-06-28 02:54:16 -0700712 */
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700713 const Region newExposed = visibleRegion - coveredRegion;
714 const Region oldVisibleRegion = layer->visibleRegionScreen;
715 const Region oldCoveredRegion = layer->coveredRegionScreen;
716 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
717 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719 dirty.subtractSelf(aboveOpaqueLayers);
720
721 // accumulate to the screen dirty region
722 dirtyRegion.orSelf(dirty);
723
Mathias Agopian9c041bb2010-03-16 16:41:46 -0700724 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Hubere3c01832010-08-16 08:49:37 -0700726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 // Store the visible region is screen space
728 layer->setVisibleRegion(visibleRegion);
729 layer->setCoveredRegion(coveredRegion);
730
Mathias Agopian12cedff2009-07-28 10:57:27 -0700731 // If a secure layer is partially visible, lock-down the screen!
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 if (layer->isSecure() && !visibleRegion.isEmpty()) {
733 secureFrameBuffer = true;
734 }
735 }
736
Mathias Agopian12cedff2009-07-28 10:57:27 -0700737 // invalidate the areas where a layer was removed
738 dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
739 mDirtyRegionRemovedLayer.clear();
740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mSecureFrameBuffer = secureFrameBuffer;
742 opaqueRegion = aboveOpaqueLayers;
743}
744
745
746void SurfaceFlinger::commitTransaction()
747{
748 mDrawingState = mCurrentState;
Mathias Agopian9779b222009-09-07 16:32:45 -0700749 mResizeTransationPending = false;
750 mTransactionCV.broadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751}
752
753void SurfaceFlinger::handlePageFlip()
754{
755 bool visibleRegions = mVisibleRegionsDirty;
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800756 const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 visibleRegions |= lockPageFlip(currentLayers);
758
759 const DisplayHardware& hw = graphicPlane(0).displayHardware();
760 const Region screenRegion(hw.bounds());
761 if (visibleRegions) {
762 Region opaqueRegion;
763 computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
Mathias Agopianff1d4102010-08-10 17:19:56 -0700764
765 /*
766 * rebuild the visible layer list
767 */
Mathias Agopianf0ff9062011-03-11 16:54:47 -0800768 const size_t count = currentLayers.size();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700769 mVisibleLayersSortedByZ.clear();
Mathias Agopianff1d4102010-08-10 17:19:56 -0700770 mVisibleLayersSortedByZ.setCapacity(count);
771 for (size_t i=0 ; i<count ; i++) {
772 if (!currentLayers[i]->visibleRegionScreen.isEmpty())
773 mVisibleLayersSortedByZ.add(currentLayers[i]);
774 }
775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 mWormholeRegion = screenRegion.subtract(opaqueRegion);
777 mVisibleRegionsDirty = false;
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800778 invalidateHwcGeometry();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780
781 unlockPageFlip(currentLayers);
782 mDirtyRegion.andSelf(screenRegion);
783}
784
Mathias Agopianfb4dcb12011-01-13 17:53:01 -0800785void SurfaceFlinger::invalidateHwcGeometry()
786{
787 mHwWorkListDirty = true;
788}
789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
791{
792 bool recomputeVisibleRegions = false;
793 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700794 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700796 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 layer->lockPageFlip(recomputeVisibleRegions);
798 }
799 return recomputeVisibleRegions;
800}
801
802void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
803{
804 const GraphicPlane& plane(graphicPlane(0));
805 const Transform& planeTransform(plane.transform());
806 size_t count = currentLayers.size();
Mathias Agopian1473f462009-04-10 14:24:30 -0700807 sp<LayerBase> const* layers = currentLayers.array();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700809 const sp<LayerBase>& layer(layers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 layer->unlockPageFlip(planeTransform, mDirtyRegion);
811 }
812}
813
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700814void SurfaceFlinger::handleWorkList()
815{
816 mHwWorkListDirty = false;
817 HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
818 if (hwc.initCheck() == NO_ERROR) {
819 const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
820 const size_t count = currentLayers.size();
821 hwc.createWorkList(count);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700822 hwc_layer_t* const cur(hwc.getLayers());
823 for (size_t i=0 ; cur && i<count ; i++) {
824 currentLayers[i]->setGeometry(&cur[i]);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700825 if (mDebugDisableHWC || mDebugRegion) {
Mathias Agopian6a969242010-09-22 18:58:01 -0700826 cur[i].compositionType = HWC_FRAMEBUFFER;
827 cur[i].flags |= HWC_SKIP_LAYER;
828 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700829 }
830 }
831}
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833void SurfaceFlinger::handleRepaint()
834{
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700835 // compute the invalid region
836 mInvalidRegion.orSelf(mDirtyRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 if (UNLIKELY(mDebugRegion)) {
839 debugFlashRegions();
840 }
841
Mathias Agopian8c9687a2009-06-26 19:06:36 -0700842 // set the frame buffer
843 const DisplayHardware& hw(graphicPlane(0).displayHardware());
844 glMatrixMode(GL_MODELVIEW);
845 glLoadIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846
847 uint32_t flags = hw.getFlags();
Andreas Hubere3c01832010-08-16 08:49:37 -0700848 if ((flags & DisplayHardware::SWAP_RECTANGLE) ||
849 (flags & DisplayHardware::BUFFER_PRESERVED))
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700850 {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700851 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
852 // takes a rectangle, we must make sure to update that whole
853 // rectangle in that case
854 if (flags & DisplayHardware::SWAP_RECTANGLE) {
Mathias Agopian7623da42010-06-01 15:12:58 -0700855 // TODO: we really should be able to pass a region to
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700856 // SWAP_RECTANGLE so that we don't have to redraw all this.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 mDirtyRegion.set(mInvalidRegion.bounds());
858 } else {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700859 // in the BUFFER_PRESERVED case, obviously, we can update only
860 // what's needed and nothing more.
861 // NOTE: this is NOT a common case, as preserving the backbuffer
862 // is costly and usually involves copying the whole update back.
863 }
864 } else {
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700865 if (flags & DisplayHardware::PARTIAL_UPDATES) {
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700866 // We need to redraw the rectangle that will be updated
867 // (pushed to the framebuffer).
Mathias Agopianf2d28b72009-09-24 14:57:26 -0700868 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopianecfa7cc2009-06-29 18:49:56 -0700869 // rectangle instead of a region (see DisplayHardware::flip())
870 mDirtyRegion.set(mInvalidRegion.bounds());
871 } else {
872 // we need to redraw everything (the whole screen)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 mDirtyRegion.set(hw.bounds());
874 mInvalidRegion = mDirtyRegion;
875 }
876 }
877
878 // compose all surfaces
879 composeSurfaces(mDirtyRegion);
880
881 // clear the dirty regions
882 mDirtyRegion.clear();
883}
884
885void SurfaceFlinger::composeSurfaces(const Region& dirty)
886{
887 if (UNLIKELY(!mWormholeRegion.isEmpty())) {
888 // should never happen unless the window manager has a bug
889 // draw something...
890 drawWormhole();
891 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700892
893 status_t err = NO_ERROR;
Mathias Agopianff1d4102010-08-10 17:19:56 -0700894 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700895 size_t count = layers.size();
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700896
897 const DisplayHardware& hw(graphicPlane(0).displayHardware());
898 HWComposer& hwc(hw.getHwComposer());
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700899 hwc_layer_t* const cur(hwc.getLayers());
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700900
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700901 LOGE_IF(cur && hwc.getNumLayers() != count,
902 "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
903 hwc.getNumLayers(), count);
904
905 // just to be extra-safe, use the smallest count
Erik Gilling0cf2f432010-08-12 23:21:40 -0700906 if (hwc.initCheck() == NO_ERROR) {
907 count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
908 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700909
910 /*
911 * update the per-frame h/w composer data for each layer
912 * and build the transparent region of the FB
913 */
914 Region transparent;
915 if (cur) {
916 for (size_t i=0 ; i<count ; i++) {
917 const sp<LayerBase>& layer(layers[i]);
918 layer->setPerFrameData(&cur[i]);
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700919 }
920 err = hwc.prepare();
921 LOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700922
Mathias Agopian45491692011-01-19 15:24:23 -0800923 if (err == NO_ERROR) {
924 for (size_t i=0 ; i<count ; i++) {
925 if (cur[i].hints & HWC_HINT_CLEAR_FB) {
926 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700927 if (layer->isOpaque()) {
Mathias Agopian45491692011-01-19 15:24:23 -0800928 transparent.orSelf(layer->visibleRegionScreen);
929 }
930 }
931 }
932
933 /*
934 * clear the area of the FB that need to be transparent
935 */
936 transparent.andSelf(dirty);
937 if (!transparent.isEmpty()) {
938 glClearColor(0,0,0,0);
939 Region::const_iterator it = transparent.begin();
940 Region::const_iterator const end = transparent.end();
941 const int32_t height = hw.getHeight();
942 while (it != end) {
943 const Rect& r(*it++);
944 const GLint sy = height - (r.top + r.height());
945 glScissor(r.left, sy, r.width(), r.height());
946 glClear(GL_COLOR_BUFFER_BIT);
947 }
948 }
Mathias Agopiane0d5f5b2010-08-10 17:14:02 -0700949 }
950 }
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700951
952
953 /*
954 * and then, render the layers targeted at the framebuffer
955 */
956 for (size_t i=0 ; i<count ; i++) {
957 if (cur) {
Antti Hatala982f58b2010-09-09 02:32:30 -0700958 if ((cur[i].compositionType != HWC_FRAMEBUFFER) &&
959 !(cur[i].flags & HWC_SKIP_LAYER)) {
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700960 // skip layers handled by the HAL
961 continue;
962 }
963 }
Mathias Agopian6a969242010-09-22 18:58:01 -0700964
Mathias Agopianf8e705d2010-08-12 15:03:26 -0700965 const sp<LayerBase>& layer(layers[i]);
966 const Region clip(dirty.intersect(layer->visibleRegionScreen));
967 if (!clip.isEmpty()) {
968 layer->draw(clip);
969 }
970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971}
972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973void SurfaceFlinger::debugFlashRegions()
974{
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700975 const DisplayHardware& hw(graphicPlane(0).displayHardware());
976 const uint32_t flags = hw.getFlags();
Mathias Agopian7f76a3c2011-08-22 21:44:41 -0700977 const int32_t height = hw.getHeight();
978 if (mInvalidRegion.isEmpty()) {
979 return;
980 }
Mathias Agopian2e20bff2009-05-04 19:29:25 -0700981
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700982 if (!((flags & DisplayHardware::SWAP_RECTANGLE) ||
983 (flags & DisplayHardware::BUFFER_PRESERVED))) {
984 const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
985 mDirtyRegion.bounds() : hw.bounds());
986 composeSurfaces(repaint);
987 }
988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 glDisable(GL_BLEND);
990 glDisable(GL_DITHER);
991 glDisable(GL_SCISSOR_TEST);
992
Mathias Agopiandff8e582009-05-04 14:17:04 -0700993 static int toggle = 0;
994 toggle = 1 - toggle;
995 if (toggle) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700996 glColor4f(1, 0, 1, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700997 } else {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700998 glColor4f(1, 1, 0, 1);
Mathias Agopiandff8e582009-05-04 14:17:04 -0700999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001001 Region::const_iterator it = mDirtyRegion.begin();
1002 Region::const_iterator const end = mDirtyRegion.end();
1003 while (it != end) {
1004 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 GLfloat vertices[][2] = {
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001006 { r.left, height - r.top },
1007 { r.left, height - r.bottom },
1008 { r.right, height - r.bottom },
1009 { r.right, height - r.top }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 };
1011 glVertexPointer(2, GL_FLOAT, 0, vertices);
1012 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1013 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001014
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001015 hw.flip(mInvalidRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016
1017 if (mDebugRegion > 1)
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001018 usleep(mDebugRegion * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
1020 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021}
1022
1023void SurfaceFlinger::drawWormhole() const
1024{
1025 const Region region(mWormholeRegion.intersect(mDirtyRegion));
1026 if (region.isEmpty())
1027 return;
1028
1029 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1030 const int32_t width = hw.getWidth();
1031 const int32_t height = hw.getHeight();
1032
1033 glDisable(GL_BLEND);
1034 glDisable(GL_DITHER);
1035
1036 if (LIKELY(!mDebugBackground)) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001037 glClearColor(0,0,0,0);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001038 Region::const_iterator it = region.begin();
1039 Region::const_iterator const end = region.end();
1040 while (it != end) {
1041 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 const GLint sy = height - (r.top + r.height());
1043 glScissor(r.left, sy, r.width(), r.height());
1044 glClear(GL_COLOR_BUFFER_BIT);
1045 }
1046 } else {
1047 const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
1048 { width, height }, { 0, height } };
1049 const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } };
1050 glVertexPointer(2, GL_SHORT, 0, vertices);
1051 glTexCoordPointer(2, GL_SHORT, 0, tcoords);
1052 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Michael I. Golde20a56d2010-09-15 15:46:24 -07001053#if defined(GL_OES_EGL_image_external)
Mathias Agopian781953d2010-06-25 18:02:21 -07001054 if (GLExtensions::getInstance().haveTextureExternal()) {
1055 glDisable(GL_TEXTURE_EXTERNAL_OES);
1056 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -07001057#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 glEnable(GL_TEXTURE_2D);
1059 glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
1060 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1061 glMatrixMode(GL_TEXTURE);
1062 glLoadIdentity();
1063 glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
Mathias Agopian6158b1b2009-05-11 00:03:41 -07001064 Region::const_iterator it = region.begin();
1065 Region::const_iterator const end = region.end();
1066 while (it != end) {
1067 const Rect& r = *it++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 const GLint sy = height - (r.top + r.height());
1069 glScissor(r.left, sy, r.width(), r.height());
1070 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1071 }
1072 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001073 glDisable(GL_TEXTURE_2D);
Mathias Agopian04a709e42010-12-14 18:38:36 -08001074 glLoadIdentity();
1075 glMatrixMode(GL_MODELVIEW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077}
1078
1079void SurfaceFlinger::debugShowFPS() const
1080{
1081 static int mFrameCount;
1082 static int mLastFrameCount = 0;
1083 static nsecs_t mLastFpsTime = 0;
1084 static float mFps = 0;
1085 mFrameCount++;
1086 nsecs_t now = systemTime();
1087 nsecs_t diff = now - mLastFpsTime;
1088 if (diff > ms2ns(250)) {
1089 mFps = ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
1090 mLastFpsTime = now;
1091 mLastFrameCount = mFrameCount;
1092 }
1093 // XXX: mFPS has the value we want
1094 }
1095
Mathias Agopian1473f462009-04-10 14:24:30 -07001096status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097{
1098 Mutex::Autolock _l(mStateLock);
1099 addLayer_l(layer);
1100 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1101 return NO_ERROR;
1102}
1103
Mathias Agopian593c05c2010-06-02 23:28:45 -07001104status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
1105{
Mathias Agopian1efba9a2010-08-10 18:09:09 -07001106 ssize_t i = mCurrentState.layersSortedByZ.add(layer);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001107 return (i < 0) ? status_t(i) : status_t(NO_ERROR);
1108}
1109
1110ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
1111 const sp<LayerBaseClient>& lbc)
1112{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001113 // attach this layer to the client
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001114 size_t name = client->attachLayer(lbc);
1115
1116 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001117
1118 // add this layer to the current state list
1119 addLayer_l(lbc);
1120
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07001121 return ssize_t(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001122}
1123
Mathias Agopian1473f462009-04-10 14:24:30 -07001124status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125{
1126 Mutex::Autolock _l(mStateLock);
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001127 status_t err = purgatorizeLayer_l(layer);
1128 if (err == NO_ERROR)
1129 setTransactionFlags(eTransactionNeeded);
1130 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131}
1132
Mathias Agopian1473f462009-04-10 14:24:30 -07001133status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134{
Mathias Agopian7623da42010-06-01 15:12:58 -07001135 sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
1136 if (lbc != 0) {
Mathias Agopiand35c6662011-01-25 20:17:45 -08001137 mLayerMap.removeItem( lbc->getSurfaceBinder() );
Mathias Agopian7623da42010-06-01 15:12:58 -07001138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
1140 if (index >= 0) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001141 mLayersRemoved = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 return NO_ERROR;
1143 }
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001144 return status_t(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145}
1146
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001147status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
1148{
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001149 // First add the layer to the purgatory list, which makes sure it won't
1150 // go away, then remove it from the main list (through a transaction).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001151 ssize_t err = removeLayer_l(layerBase);
Mathias Agopianf4dfe1b2011-01-14 17:37:42 -08001152 if (err >= 0) {
1153 mLayerPurgatory.add(layerBase);
1154 }
Mathias Agopian2e4b68d2009-09-23 16:44:00 -07001155
Mathias Agopian0c4cec72009-10-02 18:12:30 -07001156 layerBase->onRemoved();
1157
Mathias Agopian2d5ee252009-06-04 18:46:21 -07001158 // it's possible that we don't find a layer, because it might
1159 // have been destroyed already -- this is not technically an error
Mathias Agopian593c05c2010-06-02 23:28:45 -07001160 // from the user because there is a race between Client::destroySurface(),
1161 // ~Client() and ~ISurface().
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001162 return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
1163}
1164
Mathias Agopian593c05c2010-06-02 23:28:45 -07001165status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001167 layer->forceVisibilityTransaction();
1168 setTransactionFlags(eTraversalNeeded);
1169 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170}
1171
Mathias Agopian6dcb1552011-05-03 17:04:02 -07001172uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1173{
1174 return android_atomic_release_load(&mTransactionFlags);
1175}
1176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1178{
1179 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1180}
1181
Mathias Agopian898c4c92010-05-18 17:06:55 -07001182uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183{
1184 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1185 if ((old & flags)==0) { // wake the server up
Mathias Agopian898c4c92010-05-18 17:06:55 -07001186 signalEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 return old;
1189}
1190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191
Mathias Agopian439863f2011-06-28 19:09:31 -07001192void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state) {
1193 Mutex::Autolock _l(mStateLock);
Mathias Agopian9779b222009-09-07 16:32:45 -07001194
Mathias Agopian439863f2011-06-28 19:09:31 -07001195 uint32_t flags = 0;
1196 const size_t count = state.size();
1197 for (size_t i=0 ; i<count ; i++) {
1198 const ComposerState& s(state[i]);
1199 sp<Client> client( static_cast<Client *>(s.client.get()) );
1200 flags |= setClientStateLocked(client, s.state);
1201 }
1202 if (flags) {
1203 setTransactionFlags(flags);
1204 }
1205
1206 signalEvent();
1207
1208 // if there is a transaction with a resize, wait for it to
1209 // take effect before returning.
1210 while (mResizeTransationPending) {
1211 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1212 if (CC_UNLIKELY(err != NO_ERROR)) {
1213 // just in case something goes wrong in SF, return to the
1214 // called after a few seconds.
1215 LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");
1216 mResizeTransationPending = false;
1217 break;
Mathias Agopian9779b222009-09-07 16:32:45 -07001218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220}
1221
1222status_t SurfaceFlinger::freezeDisplay(DisplayID dpy, uint32_t flags)
1223{
1224 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1225 return BAD_VALUE;
1226
1227 Mutex::Autolock _l(mStateLock);
1228 mCurrentState.freezeDisplay = 1;
1229 setTransactionFlags(eTransactionNeeded);
1230
1231 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001232 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 return NO_ERROR;
1234}
1235
1236status_t SurfaceFlinger::unfreezeDisplay(DisplayID dpy, uint32_t flags)
1237{
1238 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1239 return BAD_VALUE;
1240
1241 Mutex::Autolock _l(mStateLock);
1242 mCurrentState.freezeDisplay = 0;
1243 setTransactionFlags(eTransactionNeeded);
1244
1245 // flags is intended to communicate some sort of animation behavior
Mathias Agopianed81f222009-04-14 23:02:51 -07001246 // (for instance fading)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 return NO_ERROR;
1248}
1249
Andreas Hubere3c01832010-08-16 08:49:37 -07001250int SurfaceFlinger::setOrientation(DisplayID dpy,
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001251 int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252{
1253 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
1254 return BAD_VALUE;
1255
1256 Mutex::Autolock _l(mStateLock);
1257 if (mCurrentState.orientation != orientation) {
1258 if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
Mathias Agopianeb0c86e2009-03-27 18:11:38 -07001259 mCurrentState.orientationType = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 mCurrentState.orientation = orientation;
1261 setTransactionFlags(eTransactionNeeded);
1262 mTransactionCV.wait(mStateLock);
1263 } else {
1264 orientation = BAD_VALUE;
1265 }
1266 }
1267 return orientation;
1268}
1269
Mathias Agopian9638e5c2011-04-20 14:19:32 -07001270sp<ISurface> SurfaceFlinger::createSurface(
1271 ISurfaceComposerClient::surface_data_t* params,
1272 const String8& name,
1273 const sp<Client>& client,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
1275 uint32_t flags)
1276{
Mathias Agopian1473f462009-04-10 14:24:30 -07001277 sp<LayerBaseClient> layer;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001278 sp<ISurface> surfaceHandle;
Mathias Agopian4d2dbeb2009-07-09 18:16:43 -07001279
1280 if (int32_t(w|h) < 0) {
1281 LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
1282 int(w), int(h));
1283 return surfaceHandle;
1284 }
Andreas Hubere3c01832010-08-16 08:49:37 -07001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);
Mathias Agopian7623da42010-06-01 15:12:58 -07001287 sp<Layer> normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 switch (flags & eFXSurfaceMask) {
1289 case eFXSurfaceNormal:
Mathias Agopiand2112302010-12-07 19:38:17 -08001290 normalLayer = createNormalSurface(client, d, w, h, flags, format);
1291 layer = normalLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 break;
1293 case eFXSurfaceBlur:
Mathias Agopianc3802d22010-12-08 17:13:19 -08001294 // for now we treat Blur as Dim, until we can implement it
1295 // efficiently.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 case eFXSurfaceDim:
Mathias Agopian593c05c2010-06-02 23:28:45 -07001297 layer = createDimSurface(client, d, w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 break;
1299 }
1300
Mathias Agopian1473f462009-04-10 14:24:30 -07001301 if (layer != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001302 layer->initStates(w, h, flags);
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08001303 layer->setName(name);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001304 ssize_t token = addClientLayer(client, layer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 surfaceHandle = layer->getSurface();
Andreas Hubere3c01832010-08-16 08:49:37 -07001307 if (surfaceHandle != 0) {
Mathias Agopian593c05c2010-06-02 23:28:45 -07001308 params->token = token;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001309 params->identity = layer->getIdentity();
Mathias Agopian7623da42010-06-01 15:12:58 -07001310 if (normalLayer != 0) {
1311 Mutex::Autolock _l(mStateLock);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001312 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
Mathias Agopian7623da42010-06-01 15:12:58 -07001313 }
Mathias Agopian18b6b492009-08-19 17:46:26 -07001314 }
Mathias Agopian7623da42010-06-01 15:12:58 -07001315
Mathias Agopian593c05c2010-06-02 23:28:45 -07001316 setTransactionFlags(eTransactionNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318
1319 return surfaceHandle;
1320}
1321
Mathias Agopian7623da42010-06-01 15:12:58 -07001322sp<Layer> SurfaceFlinger::createNormalSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001323 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001324 uint32_t w, uint32_t h, uint32_t flags,
Mathias Agopian18b6b492009-08-19 17:46:26 -07001325 PixelFormat& format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326{
1327 // initialize the surfaces
1328 switch (format) { // TODO: take h/w into account
1329 case PIXEL_FORMAT_TRANSPARENT:
1330 case PIXEL_FORMAT_TRANSLUCENT:
1331 format = PIXEL_FORMAT_RGBA_8888;
1332 break;
1333 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001334#ifdef NO_RGBX_8888
1335 format = PIXEL_FORMAT_RGB_565;
1336#else
Mathias Agopian59962ce2010-04-05 18:01:24 -07001337 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001338#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 break;
1340 }
1341
Mathias Agopian4cfb3a62010-06-30 15:43:47 -07001342#ifdef NO_RGBX_8888
1343 if (format == PIXEL_FORMAT_RGBX_8888)
1344 format = PIXEL_FORMAT_RGBA_8888;
1345#endif
1346
Mathias Agopian593c05c2010-06-02 23:28:45 -07001347 sp<Layer> layer = new Layer(this, display, client);
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001348 status_t err = layer->setBuffers(w, h, format, flags);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001349 if (LIKELY(err != NO_ERROR)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -07001351 layer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 return layer;
1354}
1355
Mathias Agopian7623da42010-06-01 15:12:58 -07001356sp<LayerDim> SurfaceFlinger::createDimSurface(
Mathias Agopian6edf5af2009-06-19 17:00:27 -07001357 const sp<Client>& client, DisplayID display,
Mathias Agopian593c05c2010-06-02 23:28:45 -07001358 uint32_t w, uint32_t h, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359{
Mathias Agopian593c05c2010-06-02 23:28:45 -07001360 sp<LayerDim> layer = new LayerDim(this, display, client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 layer->initStates(w, h, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 return layer;
1363}
1364
Mathias Agopian593c05c2010-06-02 23:28:45 -07001365status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366{
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001367 /*
1368 * called by the window manager, when a surface should be marked for
1369 * destruction.
Andreas Hubere3c01832010-08-16 08:49:37 -07001370 *
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001371 * The surface is removed from the current and drawing lists, but placed
1372 * in the purgatory queue, so it's not destroyed right-away (we need
1373 * to wait for all client's references to go away first).
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001374 */
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001375
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001376 status_t err = NAME_NOT_FOUND;
Mathias Agopiana3aa6c92009-04-22 15:23:34 -07001377 Mutex::Autolock _l(mStateLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07001378 sp<LayerBaseClient> layer = client->getLayerUser(sid);
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001379 if (layer != 0) {
1380 err = purgatorizeLayer_l(layer);
1381 if (err == NO_ERROR) {
Mathias Agopian248b5bd2009-09-10 19:41:18 -07001382 setTransactionFlags(eTransactionNeeded);
1383 }
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001384 }
1385 return err;
1386}
1387
Mathias Agopian69608112011-05-19 15:38:14 -07001388status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
Mathias Agopian6cf0db22009-04-17 19:36:26 -07001389{
Mathias Agopian359140c2009-07-02 17:33:40 -07001390 // called by ~ISurface() when all references are gone
Mathias Agopian69608112011-05-19 15:38:14 -07001391 status_t err = NO_ERROR;
1392 sp<LayerBaseClient> l(layer.promote());
1393 if (l != NULL) {
1394 Mutex::Autolock _l(mStateLock);
1395 err = removeLayer_l(l);
1396 if (err == NAME_NOT_FOUND) {
1397 // The surface wasn't in the current list, which means it was
1398 // removed already, which means it is in the purgatory,
1399 // and need to be removed from there.
1400 ssize_t idx = mLayerPurgatory.remove(l);
1401 LOGE_IF(idx < 0,
1402 "layer=%p is not in the purgatory list", l.get());
Mathias Agopian6ead5d92009-04-20 19:39:12 -07001403 }
Mathias Agopian69608112011-05-19 15:38:14 -07001404 LOGE_IF(err<0 && err != NAME_NOT_FOUND,
1405 "error removing layer=%p (%s)", l.get(), strerror(-err));
1406 }
1407 return err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408}
1409
Mathias Agopian439863f2011-06-28 19:09:31 -07001410uint32_t SurfaceFlinger::setClientStateLocked(
Mathias Agopian593c05c2010-06-02 23:28:45 -07001411 const sp<Client>& client,
Mathias Agopian439863f2011-06-28 19:09:31 -07001412 const layer_state_t& s)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 uint32_t flags = 0;
Mathias Agopian439863f2011-06-28 19:09:31 -07001415 sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
1416 if (layer != 0) {
1417 const uint32_t what = s.what;
1418 if (what & ePositionChanged) {
1419 if (layer->setPosition(s.x, s.y))
1420 flags |= eTraversalNeeded;
1421 }
1422 if (what & eLayerChanged) {
1423 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1424 if (layer->setLayer(s.z)) {
1425 mCurrentState.layersSortedByZ.removeAt(idx);
1426 mCurrentState.layersSortedByZ.add(layer);
1427 // we need traversal (state changed)
1428 // AND transaction (list changed)
1429 flags |= eTransactionNeeded|eTraversalNeeded;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001432 if (what & eSizeChanged) {
1433 if (layer->setSize(s.w, s.h)) {
1434 flags |= eTraversalNeeded;
1435 mResizeTransationPending = true;
1436 }
1437 }
1438 if (what & eAlphaChanged) {
1439 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1440 flags |= eTraversalNeeded;
1441 }
1442 if (what & eMatrixChanged) {
1443 if (layer->setMatrix(s.matrix))
1444 flags |= eTraversalNeeded;
1445 }
1446 if (what & eTransparentRegionChanged) {
1447 if (layer->setTransparentRegionHint(s.transparentRegion))
1448 flags |= eTraversalNeeded;
1449 }
1450 if (what & eVisibilityChanged) {
1451 if (layer->setFlags(s.flags, s.mask))
1452 flags |= eTraversalNeeded;
1453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
Mathias Agopian439863f2011-06-28 19:09:31 -07001455 return flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456}
1457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458void SurfaceFlinger::screenReleased(int dpy)
1459{
1460 // this may be called by a signal handler, we can't do too much in here
1461 android_atomic_or(eConsoleReleased, &mConsoleSignals);
1462 signalEvent();
1463}
1464
1465void SurfaceFlinger::screenAcquired(int dpy)
1466{
1467 // this may be called by a signal handler, we can't do too much in here
1468 android_atomic_or(eConsoleAcquired, &mConsoleSignals);
1469 signalEvent();
1470}
1471
1472status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
1473{
Erik Gilling94720d72010-12-01 16:38:01 -08001474 const size_t SIZE = 4096;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 char buffer[SIZE];
1476 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001477
1478 if (!PermissionCache::checkCallingPermission(sDump)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 snprintf(buffer, SIZE, "Permission Denial: "
1480 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
1481 IPCThreadState::self()->getCallingPid(),
1482 IPCThreadState::self()->getCallingUid());
1483 result.append(buffer);
1484 } else {
Mathias Agopiana8d49172009-08-26 16:36:26 -07001485
1486 // figure out if we're stuck somewhere
1487 const nsecs_t now = systemTime();
1488 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
1489 const nsecs_t inTransaction(mDebugInTransaction);
1490 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
1491 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
1492
1493 // Try to get the main lock, but don't insist if we can't
1494 // (this would indicate SF is stuck, but we want to be able to
1495 // print something in dumpsys).
1496 int retry = 3;
1497 while (mStateLock.tryLock()<0 && --retry>=0) {
1498 usleep(1000000);
1499 }
1500 const bool locked(retry >= 0);
1501 if (!locked) {
Andreas Hubere3c01832010-08-16 08:49:37 -07001502 snprintf(buffer, SIZE,
Mathias Agopiana8d49172009-08-26 16:36:26 -07001503 "SurfaceFlinger appears to be unresponsive, "
1504 "dumping anyways (no locks held)\n");
1505 result.append(buffer);
1506 }
1507
Mathias Agopian06a61e22011-01-19 16:15:53 -08001508 /*
1509 * Dump the visible layer list
1510 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
1512 const size_t count = currentLayers.size();
Mathias Agopian06a61e22011-01-19 16:15:53 -08001513 snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count);
1514 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian9bce8732010-04-20 17:55:49 -07001516 const sp<LayerBase>& layer(currentLayers[i]);
1517 layer->dump(result, buffer, SIZE);
1518 const Layer::State& s(layer->drawingState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 s.transparentRegion.dump(result, "transparentRegion");
1520 layer->transparentRegionScreen.dump(result, "transparentRegionScreen");
1521 layer->visibleRegionScreen.dump(result, "visibleRegionScreen");
1522 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001523
Mathias Agopian06a61e22011-01-19 16:15:53 -08001524 /*
1525 * Dump the layers in the purgatory
1526 */
1527
1528 const size_t purgatorySize = mLayerPurgatory.size();
1529 snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize);
1530 result.append(buffer);
1531 for (size_t i=0 ; i<purgatorySize ; i++) {
1532 const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i));
1533 layer->shortDump(result, buffer, SIZE);
1534 }
1535
1536 /*
1537 * Dump SurfaceFlinger global state
1538 */
1539
Mathias Agopianab951172011-07-14 18:01:49 -07001540 snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
Mathias Agopian06a61e22011-01-19 16:15:53 -08001541 result.append(buffer);
Mathias Agopianab951172011-07-14 18:01:49 -07001542
1543 const GLExtensions& extensions(GLExtensions::getInstance());
1544 snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
1545 extensions.getVendor(),
1546 extensions.getRenderer(),
1547 extensions.getVersion());
1548 result.append(buffer);
1549 snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
1550 result.append(buffer);
1551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 mWormholeRegion.dump(result, "WormholeRegion");
1553 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1554 snprintf(buffer, SIZE,
1555 " display frozen: %s, freezeCount=%d, orientation=%d, canDraw=%d\n",
1556 mFreezeDisplay?"yes":"no", mFreezeCount,
1557 mCurrentState.orientation, hw.canDraw());
1558 result.append(buffer);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001559 snprintf(buffer, SIZE,
1560 " last eglSwapBuffers() time: %f us\n"
1561 " last transaction time : %f us\n",
1562 mLastSwapBufferTime/1000.0, mLastTransactionTime/1000.0);
1563 result.append(buffer);
Mathias Agopian9bce8732010-04-20 17:55:49 -07001564
Mathias Agopiana8d49172009-08-26 16:36:26 -07001565 if (inSwapBuffersDuration || !locked) {
1566 snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
1567 inSwapBuffersDuration/1000.0);
1568 result.append(buffer);
1569 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001570
Mathias Agopiana8d49172009-08-26 16:36:26 -07001571 if (inTransactionDuration || !locked) {
1572 snprintf(buffer, SIZE, " transaction time: %f us\n",
1573 inTransactionDuration/1000.0);
1574 result.append(buffer);
1575 }
Mathias Agopian9bce8732010-04-20 17:55:49 -07001576
Mathias Agopian06a61e22011-01-19 16:15:53 -08001577 /*
1578 * Dump HWComposer state
1579 */
Mathias Agopian6a969242010-09-22 18:58:01 -07001580 HWComposer& hwc(hw.getHwComposer());
1581 snprintf(buffer, SIZE, " h/w composer %s and %s\n",
1582 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001583 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian6a969242010-09-22 18:58:01 -07001584 result.append(buffer);
Mathias Agopian90da4dd2010-09-23 18:13:21 -07001585 hwc.dump(result, buffer, SIZE);
Mathias Agopian6a969242010-09-22 18:58:01 -07001586
Mathias Agopian06a61e22011-01-19 16:15:53 -08001587 /*
1588 * Dump gralloc state
1589 */
Mathias Agopian6950e422009-10-05 17:07:12 -07001590 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Mathias Agopian1473f462009-04-10 14:24:30 -07001591 alloc.dump(result);
Erik Gilling94720d72010-12-01 16:38:01 -08001592 hw.dump(result);
Mathias Agopiana8d49172009-08-26 16:36:26 -07001593
1594 if (locked) {
1595 mStateLock.unlock();
1596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 write(fd, result.string(), result.size());
1599 return NO_ERROR;
1600}
1601
1602status_t SurfaceFlinger::onTransact(
1603 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1604{
1605 switch (code) {
1606 case CREATE_CONNECTION:
Mathias Agopian439863f2011-06-28 19:09:31 -07001607 case SET_TRANSACTION_STATE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 case SET_ORIENTATION:
1609 case FREEZE_DISPLAY:
1610 case UNFREEZE_DISPLAY:
1611 case BOOT_FINISHED:
Mathias Agopianaab758e2010-10-11 12:37:43 -07001612 case TURN_ELECTRON_BEAM_OFF:
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001613 case TURN_ELECTRON_BEAM_ON:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 {
1615 // codes that require permission check
1616 IPCThreadState* ipc = IPCThreadState::self();
1617 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -07001618 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001619 if ((uid != AID_GRAPHICS) &&
1620 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001621 LOGE("Permission Denial: "
1622 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
1623 return PERMISSION_DENIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001625 break;
1626 }
1627 case CAPTURE_SCREEN:
1628 {
1629 // codes that require permission check
1630 IPCThreadState* ipc = IPCThreadState::self();
1631 const int pid = ipc->getCallingPid();
1632 const int uid = ipc->getCallingUid();
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001633 if ((uid != AID_GRAPHICS) &&
1634 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001635 LOGE("Permission Denial: "
1636 "can't read framebuffer pid=%d, uid=%d", pid, uid);
1637 return PERMISSION_DENIED;
1638 }
1639 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641 }
Mathias Agopianca5edbe2010-09-24 11:26:58 -07001642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
1644 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopian8c9687a2009-06-26 19:06:36 -07001645 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian0dd593f2011-06-27 16:05:52 -07001646 if (UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian151e8592009-06-15 18:24:59 -07001647 IPCThreadState* ipc = IPCThreadState::self();
1648 const int pid = ipc->getCallingPid();
1649 const int uid = ipc->getCallingUid();
1650 LOGE("Permission Denial: "
1651 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 return PERMISSION_DENIED;
1653 }
1654 int n;
1655 switch (code) {
Mathias Agopian17f638b2009-04-16 20:04:08 -07001656 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian04262e92010-09-13 22:57:58 -07001657 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 return NO_ERROR;
1659 case 1002: // SHOW_UPDATES
1660 n = data.readInt32();
1661 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001662 invalidateHwcGeometry();
1663 repaintEverything();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 return NO_ERROR;
1665 case 1003: // SHOW_BACKGROUND
1666 n = data.readInt32();
1667 mDebugBackground = n ? 1 : 0;
1668 return NO_ERROR;
1669 case 1004:{ // repaint everything
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001670 repaintEverything();
Mathias Agopian9779b222009-09-07 16:32:45 -07001671 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
Mathias Agopian9779b222009-09-07 16:32:45 -07001673 case 1005:{ // force transaction
1674 setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
1675 return NO_ERROR;
1676 }
Mathias Agopian04262e92010-09-13 22:57:58 -07001677 case 1006:{ // enable/disable GraphicLog
1678 int enabled = data.readInt32();
1679 GraphicLog::getInstance().setEnabled(enabled);
1680 return NO_ERROR;
1681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 case 1007: // set mFreezeCount
1683 mFreezeCount = data.readInt32();
Mathias Agopian0e449762009-12-01 17:23:28 -08001684 mFreezeDisplayTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 return NO_ERROR;
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001686 case 1008: // toggle use of hw composer
1687 n = data.readInt32();
1688 mDebugDisableHWC = n ? 1 : 0;
1689 invalidateHwcGeometry();
1690 repaintEverything();
1691 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 case 1010: // interrogate.
Mathias Agopian17f638b2009-04-16 20:04:08 -07001693 reply->writeInt32(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 reply->writeInt32(0);
1695 reply->writeInt32(mDebugRegion);
1696 reply->writeInt32(mDebugBackground);
1697 return NO_ERROR;
1698 case 1013: {
1699 Mutex::Autolock _l(mStateLock);
1700 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1701 reply->writeInt32(hw.getPageFlipCount());
1702 }
1703 return NO_ERROR;
1704 }
1705 }
1706 return err;
1707}
1708
Mathias Agopian7f76a3c2011-08-22 21:44:41 -07001709void SurfaceFlinger::repaintEverything() {
1710 Mutex::Autolock _l(mStateLock);
1711 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1712 mDirtyRegion.set(hw.bounds()); // careful that's not thread-safe
1713 signalEvent();
1714}
1715
Mathias Agopianaab758e2010-10-11 12:37:43 -07001716// ---------------------------------------------------------------------------
1717
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001718status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
1719 GLuint* textureName, GLfloat* uOut, GLfloat* vOut)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001720{
Mathias Agopianaab758e2010-10-11 12:37:43 -07001721 if (!GLExtensions::getInstance().haveFramebufferObject())
1722 return INVALID_OPERATION;
1723
1724 // get screen geometry
Mathias Agopianaab758e2010-10-11 12:37:43 -07001725 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
Mathias Agopianaab758e2010-10-11 12:37:43 -07001726 const uint32_t hw_w = hw.getWidth();
1727 const uint32_t hw_h = hw.getHeight();
Mathias Agopianaab758e2010-10-11 12:37:43 -07001728 GLfloat u = 1;
1729 GLfloat v = 1;
1730
1731 // make sure to clear all GL error flags
1732 while ( glGetError() != GL_NO_ERROR ) ;
1733
1734 // create a FBO
1735 GLuint name, tname;
1736 glGenTextures(1, &tname);
1737 glBindTexture(GL_TEXTURE_2D, tname);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001738 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1739 hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001740 if (glGetError() != GL_NO_ERROR) {
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07001741 while ( glGetError() != GL_NO_ERROR ) ;
Mathias Agopianaab758e2010-10-11 12:37:43 -07001742 GLint tw = (2 << (31 - clz(hw_w)));
1743 GLint th = (2 << (31 - clz(hw_h)));
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001744 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
1745 tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001746 u = GLfloat(hw_w) / tw;
1747 v = GLfloat(hw_h) / th;
1748 }
1749 glGenFramebuffersOES(1, &name);
1750 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001751 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
1752 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001753
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001754 // redraw the screen entirely...
1755 glClearColor(0,0,0,1);
1756 glClear(GL_COLOR_BUFFER_BIT);
1757 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
1758 const size_t count = layers.size();
1759 for (size_t i=0 ; i<count ; ++i) {
1760 const sp<LayerBase>& layer(layers[i]);
1761 layer->drawForSreenShot();
1762 }
1763
1764 // back to main framebuffer
1765 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1766 glDisable(GL_SCISSOR_TEST);
1767 glDeleteFramebuffersOES(1, &name);
1768
1769 *textureName = tname;
1770 *uOut = u;
1771 *vOut = v;
1772 return NO_ERROR;
1773}
1774
1775// ---------------------------------------------------------------------------
1776
1777status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
1778{
1779 status_t result = PERMISSION_DENIED;
1780
1781 if (!GLExtensions::getInstance().haveFramebufferObject())
1782 return INVALID_OPERATION;
1783
1784 // get screen geometry
1785 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1786 const uint32_t hw_w = hw.getWidth();
1787 const uint32_t hw_h = hw.getHeight();
1788 const Region screenBounds(hw.bounds());
1789
1790 GLfloat u, v;
1791 GLuint tname;
1792 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1793 if (result != NO_ERROR) {
1794 return result;
1795 }
1796
1797 GLfloat vtx[8];
Mathias Agopian3a831d22011-07-07 17:30:31 -07001798 const GLfloat texCoords[4][2] = { {0,1}, {0,1-v}, {u,1-v}, {u,1} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001799 glBindTexture(GL_TEXTURE_2D, tname);
1800 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1801 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1802 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1803 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1804 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1805 glVertexPointer(2, GL_FLOAT, 0, vtx);
1806
Mathias Agopian3a831d22011-07-07 17:30:31 -07001807 /*
1808 * Texture coordinate mapping
1809 *
1810 * u
1811 * 1 +----------+---+
1812 * | | | | image is inverted
1813 * | V | | w.r.t. the texture
1814 * 1-v +----------+ | coordinates
1815 * | |
1816 * | |
1817 * | |
1818 * 0 +--------------+
1819 * 0 1
1820 *
1821 */
1822
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001823 class s_curve_interpolator {
1824 const float nbFrames, s, v;
1825 public:
1826 s_curve_interpolator(int nbFrames, float s)
1827 : nbFrames(1.0f / (nbFrames-1)), s(s),
1828 v(1.0f + expf(-s + 0.5f*s)) {
1829 }
1830 float operator()(int f) {
1831 const float x = f * nbFrames;
1832 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1833 }
1834 };
1835
1836 class v_stretch {
1837 const GLfloat hw_w, hw_h;
1838 public:
1839 v_stretch(uint32_t hw_w, uint32_t hw_h)
1840 : hw_w(hw_w), hw_h(hw_h) {
1841 }
1842 void operator()(GLfloat* vtx, float v) {
1843 const GLfloat w = hw_w + (hw_w * v);
1844 const GLfloat h = hw_h - (hw_h * v);
1845 const GLfloat x = (hw_w - w) * 0.5f;
1846 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001847 vtx[0] = x; vtx[1] = y;
1848 vtx[2] = x; vtx[3] = y + h;
1849 vtx[4] = x + w; vtx[5] = y + h;
1850 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001851 }
1852 };
1853
1854 class h_stretch {
1855 const GLfloat hw_w, hw_h;
1856 public:
1857 h_stretch(uint32_t hw_w, uint32_t hw_h)
1858 : hw_w(hw_w), hw_h(hw_h) {
1859 }
1860 void operator()(GLfloat* vtx, float v) {
1861 const GLfloat w = hw_w - (hw_w * v);
1862 const GLfloat h = 1.0f;
1863 const GLfloat x = (hw_w - w) * 0.5f;
1864 const GLfloat y = (hw_h - h) * 0.5f;
Mathias Agopian3a831d22011-07-07 17:30:31 -07001865 vtx[0] = x; vtx[1] = y;
1866 vtx[2] = x; vtx[3] = y + h;
1867 vtx[4] = x + w; vtx[5] = y + h;
1868 vtx[6] = x + w; vtx[7] = y;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001869 }
1870 };
1871
1872 // the full animation is 24 frames
Mathias Agopian3a831d22011-07-07 17:30:31 -07001873 char value[PROPERTY_VALUE_MAX];
1874 property_get("debug.sf.electron_frames", value, "24");
1875 int nbFrames = (atoi(value) + 1) >> 1;
1876 if (nbFrames <= 0) // just in case
1877 nbFrames = 24;
1878
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001879 s_curve_interpolator itr(nbFrames, 7.5f);
1880 s_curve_interpolator itg(nbFrames, 8.0f);
1881 s_curve_interpolator itb(nbFrames, 8.5f);
1882
1883 v_stretch vverts(hw_w, hw_h);
1884 glEnable(GL_BLEND);
1885 glBlendFunc(GL_ONE, GL_ONE);
1886 for (int i=0 ; i<nbFrames ; i++) {
1887 float x, y, w, h;
1888 const float vr = itr(i);
1889 const float vg = itg(i);
1890 const float vb = itb(i);
1891
1892 // clear screen
1893 glColorMask(1,1,1,1);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001894 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001895 glEnable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001896
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001897 // draw the red plane
1898 vverts(vtx, vr);
1899 glColorMask(1,0,0,1);
1900 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001901
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001902 // draw the green plane
1903 vverts(vtx, vg);
1904 glColorMask(0,1,0,1);
1905 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001906
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001907 // draw the blue plane
1908 vverts(vtx, vb);
1909 glColorMask(0,0,1,1);
1910 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001911
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001912 // draw the white highlight (we use the last vertices)
Mathias Agopianaab758e2010-10-11 12:37:43 -07001913 glDisable(GL_TEXTURE_2D);
1914 glColorMask(1,1,1,1);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001915 glColor4f(vg, vg, vg, 1);
1916 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1917 hw.flip(screenBounds);
Mathias Agopianaab758e2010-10-11 12:37:43 -07001918 }
1919
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001920 h_stretch hverts(hw_w, hw_h);
1921 glDisable(GL_BLEND);
1922 glDisable(GL_TEXTURE_2D);
1923 glColorMask(1,1,1,1);
1924 for (int i=0 ; i<nbFrames ; i++) {
1925 const float v = itg(i);
1926 hverts(vtx, v);
1927 glClear(GL_COLOR_BUFFER_BIT);
1928 glColor4f(1-v, 1-v, 1-v, 1);
1929 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1930 hw.flip(screenBounds);
1931 }
1932
1933 glColorMask(1,1,1,1);
1934 glEnable(GL_SCISSOR_TEST);
1935 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1936 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07001937 glDisable(GL_TEXTURE_2D);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001938 return NO_ERROR;
1939}
1940
1941status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
1942{
1943 status_t result = PERMISSION_DENIED;
1944
1945 if (!GLExtensions::getInstance().haveFramebufferObject())
1946 return INVALID_OPERATION;
1947
1948
1949 // get screen geometry
1950 const DisplayHardware& hw(graphicPlane(0).displayHardware());
1951 const uint32_t hw_w = hw.getWidth();
1952 const uint32_t hw_h = hw.getHeight();
1953 const Region screenBounds(hw.bounds());
1954
1955 GLfloat u, v;
1956 GLuint tname;
1957 result = renderScreenToTextureLocked(0, &tname, &u, &v);
1958 if (result != NO_ERROR) {
1959 return result;
1960 }
1961
1962 // back to main framebuffer
1963 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
1964 glDisable(GL_SCISSOR_TEST);
1965
1966 GLfloat vtx[8];
1967 const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
Mathias Agopian2d2b8032010-10-12 16:05:48 -07001968 glBindTexture(GL_TEXTURE_2D, tname);
1969 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1970 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1971 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1972 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
1973 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1974 glVertexPointer(2, GL_FLOAT, 0, vtx);
1975
1976 class s_curve_interpolator {
1977 const float nbFrames, s, v;
1978 public:
1979 s_curve_interpolator(int nbFrames, float s)
1980 : nbFrames(1.0f / (nbFrames-1)), s(s),
1981 v(1.0f + expf(-s + 0.5f*s)) {
1982 }
1983 float operator()(int f) {
1984 const float x = f * nbFrames;
1985 return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
1986 }
1987 };
1988
1989 class v_stretch {
1990 const GLfloat hw_w, hw_h;
1991 public:
1992 v_stretch(uint32_t hw_w, uint32_t hw_h)
1993 : hw_w(hw_w), hw_h(hw_h) {
1994 }
1995 void operator()(GLfloat* vtx, float v) {
1996 const GLfloat w = hw_w + (hw_w * v);
1997 const GLfloat h = hw_h - (hw_h * v);
1998 const GLfloat x = (hw_w - w) * 0.5f;
1999 const GLfloat y = (hw_h - h) * 0.5f;
2000 vtx[0] = x; vtx[1] = y;
2001 vtx[2] = x; vtx[3] = y + h;
2002 vtx[4] = x + w; vtx[5] = y + h;
2003 vtx[6] = x + w; vtx[7] = y;
2004 }
2005 };
2006
2007 class h_stretch {
2008 const GLfloat hw_w, hw_h;
2009 public:
2010 h_stretch(uint32_t hw_w, uint32_t hw_h)
2011 : hw_w(hw_w), hw_h(hw_h) {
2012 }
2013 void operator()(GLfloat* vtx, float v) {
2014 const GLfloat w = hw_w - (hw_w * v);
2015 const GLfloat h = 1.0f;
2016 const GLfloat x = (hw_w - w) * 0.5f;
2017 const GLfloat y = (hw_h - h) * 0.5f;
2018 vtx[0] = x; vtx[1] = y;
2019 vtx[2] = x; vtx[3] = y + h;
2020 vtx[4] = x + w; vtx[5] = y + h;
2021 vtx[6] = x + w; vtx[7] = y;
2022 }
2023 };
2024
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002025 // the full animation is 12 frames
2026 int nbFrames = 8;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002027 s_curve_interpolator itr(nbFrames, 7.5f);
2028 s_curve_interpolator itg(nbFrames, 8.0f);
2029 s_curve_interpolator itb(nbFrames, 8.5f);
2030
2031 h_stretch hverts(hw_w, hw_h);
2032 glDisable(GL_BLEND);
2033 glDisable(GL_TEXTURE_2D);
2034 glColorMask(1,1,1,1);
2035 for (int i=nbFrames-1 ; i>=0 ; i--) {
2036 const float v = itg(i);
2037 hverts(vtx, v);
2038 glClear(GL_COLOR_BUFFER_BIT);
2039 glColor4f(1-v, 1-v, 1-v, 1);
2040 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2041 hw.flip(screenBounds);
2042 }
2043
Mathias Agopiandfa08fb2010-10-14 12:33:07 -07002044 nbFrames = 4;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002045 v_stretch vverts(hw_w, hw_h);
2046 glEnable(GL_BLEND);
2047 glBlendFunc(GL_ONE, GL_ONE);
2048 for (int i=nbFrames-1 ; i>=0 ; i--) {
2049 float x, y, w, h;
2050 const float vr = itr(i);
2051 const float vg = itg(i);
2052 const float vb = itb(i);
2053
2054 // clear screen
2055 glColorMask(1,1,1,1);
2056 glClear(GL_COLOR_BUFFER_BIT);
2057 glEnable(GL_TEXTURE_2D);
2058
2059 // draw the red plane
2060 vverts(vtx, vr);
2061 glColorMask(1,0,0,1);
2062 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2063
2064 // draw the green plane
2065 vverts(vtx, vg);
2066 glColorMask(0,1,0,1);
2067 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2068
2069 // draw the blue plane
2070 vverts(vtx, vb);
2071 glColorMask(0,0,1,1);
2072 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
2073
2074 hw.flip(screenBounds);
2075 }
2076
2077 glColorMask(1,1,1,1);
2078 glEnable(GL_SCISSOR_TEST);
2079 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002080 glDeleteTextures(1, &tname);
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002081 glDisable(GL_TEXTURE_2D);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002082
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002083 return NO_ERROR;
2084}
2085
2086// ---------------------------------------------------------------------------
2087
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002088status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002089{
2090 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2091 if (!hw.canDraw()) {
2092 // we're already off
2093 return NO_ERROR;
2094 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002095 if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
2096 electronBeamOffAnimationImplLocked();
2097 }
2098
2099 // always clear the whole screen at the end of the animation
2100 glClearColor(0,0,0,1);
2101 glDisable(GL_SCISSOR_TEST);
2102 glClear(GL_COLOR_BUFFER_BIT);
2103 glEnable(GL_SCISSOR_TEST);
2104 hw.flip( Region(hw.bounds()) );
2105
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002106 hw.setCanDraw(false);
2107 return NO_ERROR;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002108}
2109
2110status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
2111{
Mathias Agopianaab758e2010-10-11 12:37:43 -07002112 class MessageTurnElectronBeamOff : public MessageBase {
2113 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002114 int32_t mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002115 status_t result;
2116 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002117 MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
2118 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopianaab758e2010-10-11 12:37:43 -07002119 }
2120 status_t getResult() const {
2121 return result;
2122 }
2123 virtual bool handler() {
2124 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002125 result = flinger->turnElectronBeamOffImplLocked(mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002126 return true;
2127 }
2128 };
2129
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002130 sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
Mathias Agopianaab758e2010-10-11 12:37:43 -07002131 status_t res = postMessageSync(msg);
2132 if (res == NO_ERROR) {
2133 res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002134
2135 // work-around: when the power-manager calls us we activate the
2136 // animation. eventually, the "on" animation will be called
2137 // by the power-manager itself
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002138 mElectronBeamAnimationMode = mode;
Mathias Agopianaab758e2010-10-11 12:37:43 -07002139 }
2140 return res;
2141}
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143// ---------------------------------------------------------------------------
Mathias Agopian7623da42010-06-01 15:12:58 -07002144
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002145status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002146{
2147 DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
2148 if (hw.canDraw()) {
2149 // we're already on
2150 return NO_ERROR;
2151 }
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002152 if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
2153 electronBeamOnAnimationImplLocked();
2154 }
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002155 hw.setCanDraw(true);
Mathias Agopian8b6a0542010-10-14 12:46:24 -07002156
2157 // make sure to redraw the whole screen when the animation is done
2158 mDirtyRegion.set(hw.bounds());
2159 signalEvent();
2160
Mathias Agopiana6cd6d32010-10-14 12:19:37 -07002161 return NO_ERROR;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002162}
2163
2164status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
2165{
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002166 class MessageTurnElectronBeamOn : public MessageBase {
2167 SurfaceFlinger* flinger;
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002168 int32_t mode;
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002169 status_t result;
2170 public:
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002171 MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
2172 : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002173 }
2174 status_t getResult() const {
2175 return result;
2176 }
2177 virtual bool handler() {
2178 Mutex::Autolock _l(flinger->mStateLock);
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002179 result = flinger->turnElectronBeamOnImplLocked(mode);
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002180 return true;
2181 }
2182 };
2183
Mathias Agopiand4e03f32010-10-14 14:54:06 -07002184 postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
Mathias Agopian2d2b8032010-10-12 16:05:48 -07002185 return NO_ERROR;
2186}
2187
2188// ---------------------------------------------------------------------------
2189
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002190status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
2191 sp<IMemoryHeap>* heap,
2192 uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002193 uint32_t sw, uint32_t sh,
2194 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002195{
2196 status_t result = PERMISSION_DENIED;
2197
2198 // only one display supported for now
2199 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2200 return BAD_VALUE;
2201
Jamie Gennisf72606c2011-03-09 17:05:02 -08002202 // make sure none of the layers are protected
2203 const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
2204 const size_t count = layers.size();
2205 for (size_t i=0 ; i<count ; ++i) {
2206 const sp<LayerBase>& layer(layers[i]);
2207 const uint32_t z = layer->drawingState().z;
2208 if (z >= minLayerZ && z <= maxLayerZ) {
2209 if (layer->isProtected()) {
2210 return INVALID_OPERATION;
2211 }
2212 }
2213 }
2214
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002215 if (!GLExtensions::getInstance().haveFramebufferObject())
2216 return INVALID_OPERATION;
2217
2218 // get screen geometry
2219 const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
2220 const uint32_t hw_w = hw.getWidth();
2221 const uint32_t hw_h = hw.getHeight();
2222
2223 if ((sw > hw_w) || (sh > hw_h))
2224 return BAD_VALUE;
2225
2226 sw = (!sw) ? hw_w : sw;
2227 sh = (!sh) ? hw_h : sh;
2228 const size_t size = sw * sh * 4;
2229
Mathias Agopian32ae0942011-03-02 18:45:50 -08002230 //LOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
2231 // sw, sh, minLayerZ, maxLayerZ);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002232
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002233 // make sure to clear all GL error flags
2234 while ( glGetError() != GL_NO_ERROR ) ;
2235
2236 // create a FBO
2237 GLuint name, tname;
2238 glGenRenderbuffersOES(1, &tname);
2239 glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname);
2240 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh);
2241 glGenFramebuffersOES(1, &name);
2242 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2243 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES,
2244 GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname);
2245
2246 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002247
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002248 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2249
2250 // invert everything, b/c glReadPixel() below will invert the FB
2251 glViewport(0, 0, sw, sh);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002252 glScissor(0, 0, sw, sh);
Mathias Agopian3a831d22011-07-07 17:30:31 -07002253 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002254 glMatrixMode(GL_PROJECTION);
2255 glPushMatrix();
2256 glLoadIdentity();
Mathias Agopian3a831d22011-07-07 17:30:31 -07002257 glOrthof(0, hw_w, hw_h, 0, 0, 1);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002258 glMatrixMode(GL_MODELVIEW);
2259
2260 // redraw the screen entirely...
2261 glClearColor(0,0,0,1);
2262 glClear(GL_COLOR_BUFFER_BIT);
Mathias Agopian1c4e4fc02010-12-16 18:46:17 -08002263
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002264 for (size_t i=0 ; i<count ; ++i) {
2265 const sp<LayerBase>& layer(layers[i]);
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002266 const uint32_t z = layer->drawingState().z;
2267 if (z >= minLayerZ && z <= maxLayerZ) {
2268 layer->drawForSreenShot();
2269 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002270 }
2271
2272 // XXX: this is needed on tegra
Mathias Agopian3a831d22011-07-07 17:30:31 -07002273 glEnable(GL_SCISSOR_TEST);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002274 glScissor(0, 0, sw, sh);
2275
2276 // check for errors and return screen capture
2277 if (glGetError() != GL_NO_ERROR) {
2278 // error while rendering
2279 result = INVALID_OPERATION;
2280 } else {
2281 // allocate shared memory large enough to hold the
2282 // screen capture
2283 sp<MemoryHeapBase> base(
2284 new MemoryHeapBase(size, 0, "screen-capture") );
2285 void* const ptr = base->getBase();
2286 if (ptr) {
2287 // capture the screen with glReadPixels()
2288 glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
2289 if (glGetError() == GL_NO_ERROR) {
2290 *heap = base;
2291 *w = sw;
2292 *h = sh;
2293 *f = PIXEL_FORMAT_RGBA_8888;
2294 result = NO_ERROR;
2295 }
2296 } else {
2297 result = NO_MEMORY;
2298 }
2299 }
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002300 glEnable(GL_SCISSOR_TEST);
2301 glViewport(0, 0, hw_w, hw_h);
2302 glMatrixMode(GL_PROJECTION);
2303 glPopMatrix();
2304 glMatrixMode(GL_MODELVIEW);
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002305 } else {
2306 result = BAD_VALUE;
2307 }
2308
2309 // release FBO resources
2310 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2311 glDeleteRenderbuffersOES(1, &tname);
2312 glDeleteFramebuffersOES(1, &name);
Mathias Agopiana6b8c1c2010-12-15 14:41:59 -08002313
2314 hw.compositionComplete();
2315
Mathias Agopian32ae0942011-03-02 18:45:50 -08002316 // LOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
Mathias Agopiancd2cfb62011-01-16 14:05:02 -08002317
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002318 return result;
2319}
2320
2321
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002322status_t SurfaceFlinger::captureScreen(DisplayID dpy,
2323 sp<IMemoryHeap>* heap,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002324 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002325 uint32_t sw, uint32_t sh,
2326 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002327{
2328 // only one display supported for now
2329 if (UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
2330 return BAD_VALUE;
2331
2332 if (!GLExtensions::getInstance().haveFramebufferObject())
2333 return INVALID_OPERATION;
2334
2335 class MessageCaptureScreen : public MessageBase {
2336 SurfaceFlinger* flinger;
2337 DisplayID dpy;
2338 sp<IMemoryHeap>* heap;
2339 uint32_t* w;
2340 uint32_t* h;
2341 PixelFormat* f;
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002342 uint32_t sw;
2343 uint32_t sh;
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002344 uint32_t minLayerZ;
2345 uint32_t maxLayerZ;
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002346 status_t result;
2347 public:
2348 MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002349 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002350 uint32_t sw, uint32_t sh,
2351 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002352 : flinger(flinger), dpy(dpy),
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002353 heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
2354 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2355 result(PERMISSION_DENIED)
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002356 {
2357 }
2358 status_t getResult() const {
2359 return result;
2360 }
2361 virtual bool handler() {
2362 Mutex::Autolock _l(flinger->mStateLock);
2363
2364 // if we have secure windows, never allow the screen capture
2365 if (flinger->mSecureFrameBuffer)
2366 return true;
2367
Mathias Agopian38ed2e32010-09-29 13:02:36 -07002368 result = flinger->captureScreenImplLocked(dpy,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002369 heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002370
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002371 return true;
2372 }
2373 };
2374
2375 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian3dd25a62010-12-10 16:22:31 -08002376 dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
Mathias Agopianca5edbe2010-09-24 11:26:58 -07002377 status_t res = postMessageSync(msg);
2378 if (res == NO_ERROR) {
2379 res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
2380 }
2381 return res;
2382}
2383
2384// ---------------------------------------------------------------------------
2385
Mathias Agopian7623da42010-06-01 15:12:58 -07002386sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
2387{
2388 sp<Layer> result;
2389 Mutex::Autolock _l(mStateLock);
2390 result = mLayerMap.valueFor( sur->asBinder() ).promote();
2391 return result;
2392}
2393
2394// ---------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395
Mathias Agopian593c05c2010-06-02 23:28:45 -07002396Client::Client(const sp<SurfaceFlinger>& flinger)
Mathias Agopian7623da42010-06-01 15:12:58 -07002397 : mFlinger(flinger), mNameGenerator(1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399}
2400
Mathias Agopian593c05c2010-06-02 23:28:45 -07002401Client::~Client()
2402{
Mathias Agopian593c05c2010-06-02 23:28:45 -07002403 const size_t count = mLayers.size();
2404 for (size_t i=0 ; i<count ; i++) {
2405 sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
2406 if (layer != 0) {
2407 mFlinger->removeLayer(layer);
2408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
2410}
2411
Mathias Agopian593c05c2010-06-02 23:28:45 -07002412status_t Client::initCheck() const {
Mathias Agopian7623da42010-06-01 15:12:58 -07002413 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414}
Mathias Agopian1473f462009-04-10 14:24:30 -07002415
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002416size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002417{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002418 Mutex::Autolock _l(mLock);
2419 size_t name = mNameGenerator++;
Mathias Agopian593c05c2010-06-02 23:28:45 -07002420 mLayers.add(name, layer);
2421 return name;
2422}
2423
Mathias Agopian7623da42010-06-01 15:12:58 -07002424void Client::detachLayer(const LayerBaseClient* layer)
Mathias Agopian593c05c2010-06-02 23:28:45 -07002425{
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002426 Mutex::Autolock _l(mLock);
Mathias Agopian593c05c2010-06-02 23:28:45 -07002427 // we do a linear search here, because this doesn't happen often
2428 const size_t count = mLayers.size();
2429 for (size_t i=0 ; i<count ; i++) {
2430 if (mLayers.valueAt(i) == layer) {
2431 mLayers.removeItemsAt(i, 1);
2432 break;
2433 }
2434 }
2435}
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002436sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
2437{
2438 Mutex::Autolock _l(mLock);
Mathias Agopian1473f462009-04-10 14:24:30 -07002439 sp<LayerBaseClient> lbc;
Mathias Agopian5fa7ad62011-05-03 16:21:41 -07002440 wp<LayerBaseClient> layer(mLayers.valueFor(i));
Mathias Agopian593c05c2010-06-02 23:28:45 -07002441 if (layer != 0) {
2442 lbc = layer.promote();
2443 LOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
Mathias Agopian1473f462009-04-10 14:24:30 -07002444 }
2445 return lbc;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446}
2447
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002448
2449status_t Client::onTransact(
2450 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2451{
2452 // these must be checked
2453 IPCThreadState* ipc = IPCThreadState::self();
2454 const int pid = ipc->getCallingPid();
2455 const int uid = ipc->getCallingUid();
2456 const int self_pid = getpid();
2457 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
2458 // we're called from a different process, do the real check
Mathias Agopian0dd593f2011-06-27 16:05:52 -07002459 if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002460 {
2461 LOGE("Permission Denial: "
2462 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
2463 return PERMISSION_DENIED;
2464 }
2465 }
2466 return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
Mathias Agopian7623da42010-06-01 15:12:58 -07002467}
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002468
2469
Mathias Agopian593c05c2010-06-02 23:28:45 -07002470sp<ISurface> Client::createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -07002471 ISurfaceComposerClient::surface_data_t* params,
Mathias Agopian7623da42010-06-01 15:12:58 -07002472 const String8& name,
2473 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 uint32_t flags)
2475{
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002476 /*
2477 * createSurface must be called from the GL thread so that it can
2478 * have access to the GL context.
2479 */
2480
2481 class MessageCreateSurface : public MessageBase {
2482 sp<ISurface> result;
2483 SurfaceFlinger* flinger;
2484 ISurfaceComposerClient::surface_data_t* params;
2485 Client* client;
2486 const String8& name;
2487 DisplayID display;
2488 uint32_t w, h;
2489 PixelFormat format;
2490 uint32_t flags;
2491 public:
2492 MessageCreateSurface(SurfaceFlinger* flinger,
2493 ISurfaceComposerClient::surface_data_t* params,
2494 const String8& name, Client* client,
2495 DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
2496 uint32_t flags)
2497 : flinger(flinger), params(params), client(client), name(name),
2498 display(display), w(w), h(h), format(format), flags(flags)
2499 {
2500 }
2501 sp<ISurface> getResult() const { return result; }
2502 virtual bool handler() {
2503 result = flinger->createSurface(params, name, client,
2504 display, w, h, format, flags);
2505 return true;
2506 }
2507 };
2508
2509 sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
2510 params, name, this, display, w, h, format, flags);
2511 mFlinger->postMessageSync(msg);
2512 return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513}
Mathias Agopian593c05c2010-06-02 23:28:45 -07002514status_t Client::destroySurface(SurfaceID sid) {
2515 return mFlinger->removeSurface(this, sid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517
2518// ---------------------------------------------------------------------------
2519
Jamie Gennisf7acf162011-01-12 18:30:40 -08002520GraphicBufferAlloc::GraphicBufferAlloc() {}
2521
2522GraphicBufferAlloc::~GraphicBufferAlloc() {}
2523
2524sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h,
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002525 PixelFormat format, uint32_t usage, status_t* error) {
Jamie Gennisf7acf162011-01-12 18:30:40 -08002526 sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage));
2527 status_t err = graphicBuffer->initCheck();
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002528 *error = err;
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002529 if (err != 0 || graphicBuffer->handle == 0) {
Mathias Agopianeec0f7e2011-07-01 14:53:49 -07002530 if (err == NO_MEMORY) {
2531 GraphicBuffer::dumpAllocationsToSystemLog();
2532 }
Mathias Agopian7bb843c2011-04-20 14:20:59 -07002533 LOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) "
2534 "failed (%s), handle=%p",
2535 w, h, strerror(-err), graphicBuffer->handle);
Jamie Gennisf7acf162011-01-12 18:30:40 -08002536 return 0;
2537 }
Jamie Gennisf7acf162011-01-12 18:30:40 -08002538 return graphicBuffer;
2539}
2540
Jamie Gennisf7acf162011-01-12 18:30:40 -08002541// ---------------------------------------------------------------------------
2542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543GraphicPlane::GraphicPlane()
2544 : mHw(0)
2545{
2546}
2547
2548GraphicPlane::~GraphicPlane() {
2549 delete mHw;
2550}
2551
2552bool GraphicPlane::initialized() const {
2553 return mHw ? true : false;
2554}
2555
Mathias Agopian66c77a52010-02-08 15:49:35 -08002556int GraphicPlane::getWidth() const {
2557 return mWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558}
2559
Mathias Agopian66c77a52010-02-08 15:49:35 -08002560int GraphicPlane::getHeight() const {
2561 return mHeight;
2562}
2563
2564void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
2565{
2566 mHw = hw;
2567
2568 // initialize the display orientation transform.
2569 // it's a constant that should come from the display driver.
2570 int displayOrientation = ISurfaceComposer::eOrientationDefault;
2571 char property[PROPERTY_VALUE_MAX];
2572 if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
2573 //displayOrientation
2574 switch (atoi(property)) {
2575 case 90:
2576 displayOrientation = ISurfaceComposer::eOrientation90;
2577 break;
2578 case 270:
2579 displayOrientation = ISurfaceComposer::eOrientation270;
2580 break;
2581 }
2582 }
2583
2584 const float w = hw->getWidth();
2585 const float h = hw->getHeight();
2586 GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
2587 &mDisplayTransform);
2588 if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
2589 mDisplayWidth = h;
2590 mDisplayHeight = w;
2591 } else {
2592 mDisplayWidth = w;
2593 mDisplayHeight = h;
2594 }
2595
2596 setOrientation(ISurfaceComposer::eOrientationDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597}
2598
2599status_t GraphicPlane::orientationToTransfrom(
2600 int orientation, int w, int h, Transform* tr)
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002601{
2602 uint32_t flags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 switch (orientation) {
2604 case ISurfaceComposer::eOrientationDefault:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002605 flags = Transform::ROT_0;
2606 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 case ISurfaceComposer::eOrientation90:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002608 flags = Transform::ROT_90;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 break;
2610 case ISurfaceComposer::eOrientation180:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002611 flags = Transform::ROT_180;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 break;
2613 case ISurfaceComposer::eOrientation270:
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002614 flags = Transform::ROT_270;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 break;
2616 default:
2617 return BAD_VALUE;
2618 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002619 tr->set(flags, w, h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 return NO_ERROR;
2621}
2622
2623status_t GraphicPlane::setOrientation(int orientation)
2624{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 // If the rotation can be handled in hardware, this is where
2626 // the magic should happen.
Mathias Agopian66c77a52010-02-08 15:49:35 -08002627
2628 const DisplayHardware& hw(displayHardware());
2629 const float w = mDisplayWidth;
2630 const float h = mDisplayHeight;
2631 mWidth = int(w);
2632 mHeight = int(h);
2633
2634 Transform orientationTransform;
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002635 GraphicPlane::orientationToTransfrom(orientation, w, h,
2636 &orientationTransform);
2637 if (orientation & ISurfaceComposer::eOrientationSwapMask) {
2638 mWidth = int(h);
2639 mHeight = int(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 }
Mathias Agopian8c20ca32010-02-22 03:15:57 -08002641
Mathias Agopian3552f532009-03-27 17:58:20 -07002642 mOrientation = orientation;
Mathias Agopian66c77a52010-02-08 15:49:35 -08002643 mGlobalTransform = mDisplayTransform * orientationTransform;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 return NO_ERROR;
2645}
2646
2647const DisplayHardware& GraphicPlane::displayHardware() const {
2648 return *mHw;
2649}
2650
Mathias Agopianaab758e2010-10-11 12:37:43 -07002651DisplayHardware& GraphicPlane::editDisplayHardware() {
2652 return *mHw;
2653}
2654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655const Transform& GraphicPlane::transform() const {
2656 return mGlobalTransform;
2657}
2658
Mathias Agopian1473f462009-04-10 14:24:30 -07002659EGLDisplay GraphicPlane::getEGLDisplay() const {
2660 return mHw->getEGLDisplay();
2661}
2662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663// ---------------------------------------------------------------------------
2664
2665}; // namespace android