summaryrefslogtreecommitdiffstats
path: root/services/surfaceflinger/SurfaceFlinger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'services/surfaceflinger/SurfaceFlinger.cpp')
-rw-r--r--services/surfaceflinger/SurfaceFlinger.cpp2364
1 files changed, 1013 insertions, 1351 deletions
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 51fcce4..c63d0cf 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -16,17 +16,13 @@
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
-#include <stdlib.h>
-#include <stdio.h>
#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
+#include <sys/types.h>
#include <errno.h>
#include <math.h>
-#include <limits.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
+
+#include <EGL/egl.h>
+#include <GLES/gl.h>
#include <cutils/log.h>
#include <cutils/properties.h>
@@ -36,20 +32,27 @@
#include <binder/MemoryHeapBase.h>
#include <binder/PermissionCache.h>
+#include <ui/DisplayInfo.h>
+
+#include <gui/BitTube.h>
+#include <gui/BufferQueue.h>
#include <gui/IDisplayEventConnection.h>
+#include <gui/SurfaceTextureClient.h>
+
+#include <ui/GraphicBufferAllocator.h>
+#include <ui/PixelFormat.h>
#include <utils/String8.h>
#include <utils/String16.h>
#include <utils/StopWatch.h>
#include <utils/Trace.h>
-#include <ui/GraphicBufferAllocator.h>
-#include <ui/PixelFormat.h>
-
-#include <GLES/gl.h>
+#include <private/android_filesystem_config.h>
#include "clz.h"
#include "DdmConnection.h"
+#include "DisplayDevice.h"
+#include "Client.h"
#include "EventThread.h"
#include "GLExtensions.h"
#include "Layer.h"
@@ -57,12 +60,9 @@
#include "LayerScreenshot.h"
#include "SurfaceFlinger.h"
-#include "DisplayHardware/DisplayHardware.h"
+#include "DisplayHardware/FramebufferSurface.h"
#include "DisplayHardware/HWComposer.h"
-#include <private/android_filesystem_config.h>
-#include <private/gui/SharedBufferStack.h>
-#include <gui/BitTube.h>
#define EGL_VERSION_HW_ANDROID 0x3143
@@ -83,10 +83,10 @@ SurfaceFlinger::SurfaceFlinger()
mTransactionFlags(0),
mTransationPending(false),
mLayersRemoved(false),
+ mRepaintEverything(0),
mBootTime(systemTime()),
mVisibleRegionsDirty(false),
mHwWorkListDirty(false),
- mElectronBeamAnimationMode(0),
mDebugRegion(0),
mDebugDDMS(0),
mDebugDisableHWC(0),
@@ -95,13 +95,7 @@ SurfaceFlinger::SurfaceFlinger()
mLastSwapBufferTime(0),
mDebugInTransaction(0),
mLastTransactionTime(0),
- mBootFinished(false),
- mSecureFrameBuffer(0)
-{
- init();
-}
-
-void SurfaceFlinger::init()
+ mBootFinished(false)
{
ALOGI("SurfaceFlinger is starting");
@@ -111,16 +105,14 @@ void SurfaceFlinger::init()
property_get("debug.sf.showupdates", value, "0");
mDebugRegion = atoi(value);
-#ifdef DDMS_DEBUGGING
property_get("debug.sf.ddms", value, "0");
mDebugDDMS = atoi(value);
if (mDebugDDMS) {
DdmConnection::start(getServiceName());
}
-#endif
- ALOGI_IF(mDebugRegion, "showupdates enabled");
- ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
+ ALOGI_IF(mDebugRegion, "showupdates enabled");
+ ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
}
void SurfaceFlinger::onFirstRef()
@@ -136,7 +128,9 @@ void SurfaceFlinger::onFirstRef()
SurfaceFlinger::~SurfaceFlinger()
{
- glDeleteTextures(1, &mWormholeTexName);
+ EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+ eglTerminate(display);
}
void SurfaceFlinger::binderDied(const wp<IBinder>& who)
@@ -145,17 +139,18 @@ void SurfaceFlinger::binderDied(const wp<IBinder>& who)
// reset screen orientation
Vector<ComposerState> state;
- setTransactionState(state, eOrientationDefault, 0);
+ Vector<DisplayState> displays;
+ DisplayState d;
+ d.what = DisplayState::eOrientationChanged;
+ d.token = mDefaultDisplays[DisplayDevice::DISPLAY_ID_MAIN];
+ d.orientation = DisplayState::eOrientationDefault;
+ displays.add(d);
+ setTransactionState(state, displays, 0);
// restart the boot-animation
startBootAnim();
}
-sp<IMemoryHeap> SurfaceFlinger::getCblk() const
-{
- return mServerHeap;
-}
-
sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
{
sp<ISurfaceComposerClient> bclient;
@@ -167,23 +162,43 @@ sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
return bclient;
}
-sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
+sp<IBinder> SurfaceFlinger::createDisplay()
{
- sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
- return gba;
+ class DisplayToken : public BBinder {
+ sp<SurfaceFlinger> flinger;
+ virtual ~DisplayToken() {
+ // no more references, this display must be terminated
+ Mutex::Autolock _l(flinger->mStateLock);
+ flinger->mCurrentState.displays.removeItem(this);
+ flinger->setTransactionFlags(eDisplayTransactionNeeded);
+ }
+ public:
+ DisplayToken(const sp<SurfaceFlinger>& flinger)
+ : flinger(flinger) {
+ }
+ };
+
+ sp<BBinder> token = new DisplayToken(this);
+
+ Mutex::Autolock _l(mStateLock);
+ DisplayDeviceState info(intptr_t(token.get())); // FIXME: we shouldn't use the address for the id
+ mCurrentState.displays.add(token, info);
+
+ return token;
}
-const GraphicPlane& SurfaceFlinger::graphicPlane(int dpy) const
-{
- ALOGE_IF(uint32_t(dpy) >= DISPLAY_COUNT, "Invalid DisplayID %d", dpy);
- const GraphicPlane& plane(mGraphicPlanes[dpy]);
- return plane;
+sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
+ if (uint32_t(id) >= DisplayDevice::DISPLAY_ID_COUNT) {
+ ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
+ return NULL;
+ }
+ return mDefaultDisplays[id];
}
-GraphicPlane& SurfaceFlinger::graphicPlane(int dpy)
+sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
{
- return const_cast<GraphicPlane&>(
- const_cast<SurfaceFlinger const *>(this)->graphicPlane(dpy));
+ sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
+ return gba;
}
void SurfaceFlinger::bootFinished()
@@ -197,7 +212,7 @@ void SurfaceFlinger::bootFinished()
const String16 name("window");
sp<IBinder> window(defaultServiceManager()->getService(name));
if (window != 0) {
- window->linkToDeath(this);
+ window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
}
// stop boot animation
@@ -206,60 +221,110 @@ void SurfaceFlinger::bootFinished()
property_set("service.bootanim.exit", "1");
}
-static inline uint16_t pack565(int r, int g, int b) {
- return (r<<11)|(g<<5)|b;
+void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
+ class MessageDestroyGLTexture : public MessageBase {
+ GLuint texture;
+ public:
+ MessageDestroyGLTexture(GLuint texture)
+ : texture(texture) {
+ }
+ virtual bool handler() {
+ glDeleteTextures(1, &texture);
+ return true;
+ }
+ };
+ postMessageAsync(new MessageDestroyGLTexture(texture));
+}
+
+status_t SurfaceFlinger::selectConfigForPixelFormat(
+ EGLDisplay dpy,
+ EGLint const* attrs,
+ PixelFormat format,
+ EGLConfig* outConfig)
+{
+ EGLConfig config = NULL;
+ EGLint numConfigs = -1, n=0;
+ eglGetConfigs(dpy, NULL, 0, &numConfigs);
+ EGLConfig* const configs = new EGLConfig[numConfigs];
+ eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
+ for (int i=0 ; i<n ; i++) {
+ EGLint nativeVisualId = 0;
+ eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
+ if (nativeVisualId>0 && format == nativeVisualId) {
+ *outConfig = configs[i];
+ delete [] configs;
+ return NO_ERROR;
+ }
+ }
+ delete [] configs;
+ return NAME_NOT_FOUND;
}
-status_t SurfaceFlinger::readyToRun()
-{
- ALOGI( "SurfaceFlinger's main thread ready to run. "
- "Initializing graphics H/W...");
+EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
+ // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
+ // it is to be used with WIFI displays
+ EGLConfig config;
+ EGLint dummy;
+ status_t err;
+ EGLint attribs[] = {
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+ EGL_RECORDABLE_ANDROID, EGL_TRUE,
+ EGL_NONE
+ };
+ err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
+ if (err) {
+ // maybe we failed because of EGL_RECORDABLE_ANDROID
+ ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID");
+ attribs[2] = EGL_NONE;
+ err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config);
+ }
+ ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
+ if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
+ ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
+ }
+ return config;
+}
+
+EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) {
+ // Also create our EGLContext
+ EGLint contextAttributes[] = {
+#ifdef EGL_IMG_context_priority
+#ifdef HAS_CONTEXT_PRIORITY
+#warning "using EGL_IMG_context_priority"
+ EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG,
+#endif
+#endif
+ EGL_NONE, EGL_NONE
+ };
+ EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes);
+ ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
+ return ctxt;
+}
+
+void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) {
+ EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext);
+ if (!result) {
+ ALOGE("Couldn't create a working GLES context. check logs. exiting...");
+ exit(0);
+ }
+
+ GLExtensions& extensions(GLExtensions::getInstance());
+ extensions.initWithGLStrings(
+ glGetString(GL_VENDOR),
+ glGetString(GL_RENDERER),
+ glGetString(GL_VERSION),
+ glGetString(GL_EXTENSIONS),
+ eglQueryString(display, EGL_VENDOR),
+ eglQueryString(display, EGL_VERSION),
+ eglQueryString(display, EGL_EXTENSIONS));
+
+ EGLint w, h;
+ eglQuerySurface(display, surface, EGL_WIDTH, &w);
+ eglQuerySurface(display, surface, EGL_HEIGHT, &h);
+
+ glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
+ glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
- // we only support one display currently
- int dpy = 0;
-
- {
- // initialize the main display
- GraphicPlane& plane(graphicPlane(dpy));
- DisplayHardware* const hw = new DisplayHardware(this, dpy);
- plane.setDisplayHardware(hw);
- }
-
- // create the shared control-block
- mServerHeap = new MemoryHeapBase(4096,
- MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");
- ALOGE_IF(mServerHeap==0, "can't create shared memory dealer");
-
- mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());
- ALOGE_IF(mServerCblk==0, "can't get to shared control block's address");
-
- new(mServerCblk) surface_flinger_cblk_t;
-
- // initialize primary screen
- // (other display should be initialized in the same manner, but
- // asynchronously, as they could come and go. None of this is supported
- // yet).
- const GraphicPlane& plane(graphicPlane(dpy));
- const DisplayHardware& hw = plane.displayHardware();
- const uint32_t w = hw.getWidth();
- const uint32_t h = hw.getHeight();
- const uint32_t f = hw.getFormat();
- hw.makeCurrent();
-
- // initialize the shared control block
- mServerCblk->connected |= 1<<dpy;
- display_cblk_t* dcblk = mServerCblk->displays + dpy;
- memset(dcblk, 0, sizeof(display_cblk_t));
- dcblk->w = plane.getWidth();
- dcblk->h = plane.getHeight();
- dcblk->format = f;
- dcblk->orientation = ISurfaceComposer::eOrientationDefault;
- dcblk->xdpi = hw.getDpiX();
- dcblk->ydpi = hw.getDpiY();
- dcblk->fps = hw.getRefreshRate();
- dcblk->density = hw.getDensity();
-
- // Initialize OpenGL|ES
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glEnableClientState(GL_VERTEX_ARRAY);
@@ -267,17 +332,11 @@ status_t SurfaceFlinger::readyToRun()
glDisable(GL_DITHER);
glDisable(GL_CULL_FACE);
- const uint16_t g0 = pack565(0x0F,0x1F,0x0F);
- const uint16_t g1 = pack565(0x17,0x2f,0x17);
- const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };
- glGenTextures(1, &mWormholeTexName);
- glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
- GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);
+ struct pack565 {
+ inline uint16_t operator() (int r, int g, int b) const {
+ return (r<<11)|(g<<5)|b;
+ }
+ } pack565;
const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };
glGenTextures(1, &mProtectedTexName);
@@ -295,16 +354,81 @@ status_t SurfaceFlinger::readyToRun()
// put the origin in the left-bottom corner
glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h
+ // print some debugging info
+ EGLint r,g,b,a;
+ eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r);
+ eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g);
+ eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b);
+ eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a);
+ ALOGI("EGL informations:");
+ ALOGI("vendor : %s", extensions.getEglVendor());
+ ALOGI("version : %s", extensions.getEglVersion());
+ ALOGI("extensions: %s", extensions.getEglExtension());
+ ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
+ ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
+ ALOGI("OpenGL ES informations:");
+ ALOGI("vendor : %s", extensions.getVendor());
+ ALOGI("renderer : %s", extensions.getRenderer());
+ ALOGI("version : %s", extensions.getVersion());
+ ALOGI("extensions: %s", extensions.getExtension());
+ ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
+ ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]);
+}
+
+status_t SurfaceFlinger::readyToRun()
+{
+ ALOGI( "SurfaceFlinger's main thread ready to run. "
+ "Initializing graphics H/W...");
+
+ // initialize EGL
+ mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ eglInitialize(mEGLDisplay, NULL, NULL);
+
+ // Initialize the main display
+ // create native window to main display
+ sp<FramebufferSurface> fbs = FramebufferSurface::create();
+ if (fbs == NULL) {
+ ALOGE("Display subsystem failed to initialize. check logs. exiting...");
+ exit(0);
+ }
+
+ sp<SurfaceTextureClient> stc(new SurfaceTextureClient(
+ static_cast<sp<ISurfaceTexture> >(fbs->getBufferQueue())));
+
+ // initialize the config and context
+ int format;
+ ANativeWindow* const anw = stc.get();
+ anw->query(anw, NATIVE_WINDOW_FORMAT, &format);
+ mEGLConfig = selectEGLConfig(mEGLDisplay, format);
+ mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
+
+ // initialize our main display hardware
+
+ for (size_t i=0 ; i<DisplayDevice::DISPLAY_ID_COUNT ; i++) {
+ mDefaultDisplays[i] = new BBinder();
+ mCurrentState.displays.add(mDefaultDisplays[i], DisplayDeviceState(i));
+ }
+ sp<DisplayDevice> hw = new DisplayDevice(this,
+ DisplayDevice::DISPLAY_ID_MAIN, anw, fbs, mEGLConfig);
+ mDisplays.add(hw->getDisplayId(), hw);
+
+ // initialize OpenGL ES
+ EGLSurface surface = hw->getEGLSurface();
+ initializeGL(mEGLDisplay, surface);
// start the EventThread
mEventThread = new EventThread(this);
mEventQueue.setEventThread(mEventThread);
- hw.startSleepManagement();
- /*
- * We're now ready to accept clients...
- */
+ // initialize the H/W composer
+ mHwc = new HWComposer(this,
+ *static_cast<HWComposer::EventHandler *>(this),
+ fbs->getFbHal());
+ // initialize our drawing state
+ mDrawingState = mCurrentState;
+
+ // We're now ready to accept clients...
mReadyToRunBarrier.open();
// start boot animation
@@ -319,6 +443,15 @@ void SurfaceFlinger::startBootAnim() {
property_set("ctl.start", "bootanim");
}
+uint32_t SurfaceFlinger::getMaxTextureSize() const {
+ return mMaxTextureSize;
+}
+
+uint32_t SurfaceFlinger::getMaxViewportDims() const {
+ return mMaxViewportDims[0] < mMaxViewportDims[1] ?
+ mMaxViewportDims[0] : mMaxViewportDims[1];
+}
+
// ----------------------------------------------------------------------------
bool SurfaceFlinger::authenticateSurfaceTexture(
@@ -362,12 +495,93 @@ bool SurfaceFlinger::authenticateSurfaceTexture(
return false;
}
+status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
+ // TODO: this is here only for compatibility -- should go away eventually.
+ if (uint32_t(dpy) >= 1) {
+ return BAD_INDEX;
+ }
+
+ const HWComposer& hwc(getHwComposer());
+ float xdpi = hwc.getDpiX();
+ float ydpi = hwc.getDpiY();
+
+ // TODO: Not sure if display density should handled by SF any longer
+ class Density {
+ static int getDensityFromProperty(char const* propName) {
+ char property[PROPERTY_VALUE_MAX];
+ int density = 0;
+ if (property_get(propName, property, NULL) > 0) {
+ density = atoi(property);
+ }
+ return density;
+ }
+ public:
+ static int getEmuDensity() {
+ return getDensityFromProperty("qemu.sf.lcd_density"); }
+ static int getBuildDensity() {
+ return getDensityFromProperty("ro.sf.lcd_density"); }
+ };
+ // The density of the device is provided by a build property
+ float density = Density::getBuildDensity() / 160.0f;
+ if (density == 0) {
+ // the build doesn't provide a density -- this is wrong!
+ // use xdpi instead
+ ALOGE("ro.sf.lcd_density must be defined as a build property");
+ density = xdpi / 160.0f;
+ }
+ if (Density::getEmuDensity()) {
+ // if "qemu.sf.lcd_density" is specified, it overrides everything
+ xdpi = ydpi = density = Density::getEmuDensity();
+ density /= 160.0f;
+ }
+
+ sp<const DisplayDevice> hw(getDefaultDisplayDevice());
+ info->w = hw->getWidth();
+ info->h = hw->getHeight();
+ info->xdpi = xdpi;
+ info->ydpi = ydpi;
+ info->fps = float(1e9 / hwc.getRefreshPeriod());
+ info->density = density;
+ info->orientation = hw->getOrientation();
+ // TODO: this needs to go away (currently needed only by webkit)
+ getPixelFormatInfo(hw->getFormat(), &info->pixelFormatInfo);
+ return NO_ERROR;
+}
+
// ----------------------------------------------------------------------------
sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
return mEventThread->createEventConnection();
}
+void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
+
+ sp<IBinder> token;
+ { // scope for the lock
+ Mutex::Autolock _l(mStateLock);
+ token = mExtDisplayToken;
+ }
+
+ if (token == 0) {
+ token = createDisplay();
+ }
+
+ { // scope for the lock
+ Mutex::Autolock _l(mStateLock);
+ if (surface == 0) {
+ // release our current display. we're guarantee to have
+ // a reference to it (token), while we hold the lock
+ mExtDisplayToken = 0;
+ } else {
+ mExtDisplayToken = token;
+ }
+
+ DisplayDeviceState& info(mCurrentState.displays.editValueFor(token));
+ info.surface = surface;
+ setTransactionFlags(eDisplayTransactionNeeded);
+ }
+}
+
// ----------------------------------------------------------------------------
void SurfaceFlinger::waitForEvent() {
@@ -400,83 +614,262 @@ status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
return res;
}
-bool SurfaceFlinger::threadLoop()
-{
+bool SurfaceFlinger::threadLoop() {
waitForEvent();
return true;
}
-void SurfaceFlinger::onMessageReceived(int32_t what)
-{
+void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) {
+ mEventThread->onVSyncReceived(dpy, timestamp);
+}
+
+void SurfaceFlinger::eventControl(int event, int enabled) {
+ getHwComposer().eventControl(event, enabled);
+}
+
+void SurfaceFlinger::onMessageReceived(int32_t what) {
ATRACE_CALL();
switch (what) {
- case MessageQueue::REFRESH: {
-// case MessageQueue::INVALIDATE: {
- // if we're in a global transaction, don't do anything.
- const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
- uint32_t transactionFlags = peekTransactionFlags(mask);
- if (CC_UNLIKELY(transactionFlags)) {
- handleTransaction(transactionFlags);
+ case MessageQueue::INVALIDATE:
+ handleMessageTransaction();
+ handleMessageInvalidate();
+ signalRefresh();
+ break;
+ case MessageQueue::REFRESH:
+ handleMessageRefresh();
+ break;
+ }
+}
+
+void SurfaceFlinger::handleMessageTransaction() {
+ uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
+ if (transactionFlags) {
+ handleTransaction(transactionFlags);
+ }
+}
+
+void SurfaceFlinger::handleMessageInvalidate() {
+ ATRACE_CALL();
+ handlePageFlip();
+}
+
+void SurfaceFlinger::handleMessageRefresh() {
+ ATRACE_CALL();
+ preComposition();
+ rebuildLayerStacks();
+ setUpHWComposer();
+ doDebugFlashRegions();
+ doComposition();
+ postComposition();
+}
+
+void SurfaceFlinger::doDebugFlashRegions()
+{
+ // is debugging enabled
+ if (CC_LIKELY(!mDebugRegion))
+ return;
+
+ const bool repaintEverything = mRepaintEverything;
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ const sp<DisplayDevice>& hw(mDisplays[dpy]);
+ if (hw->canDraw()) {
+ // transform the dirty region into this screen's coordinate space
+ const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
+ if (!dirtyRegion.isEmpty()) {
+ // redraw the whole screen
+ doComposeSurfaces(hw, Region(hw->bounds()));
+
+ // and draw the dirty region
+ glDisable(GL_TEXTURE_EXTERNAL_OES);
+ glDisable(GL_TEXTURE_2D);
+ glDisable(GL_BLEND);
+ glColor4f(1, 0, 1, 1);
+ const int32_t height = hw->getHeight();
+ Region::const_iterator it = dirtyRegion.begin();
+ Region::const_iterator const end = dirtyRegion.end();
+ while (it != end) {
+ const Rect& r = *it++;
+ GLfloat vertices[][2] = {
+ { r.left, height - r.top },
+ { r.left, height - r.bottom },
+ { r.right, height - r.bottom },
+ { r.right, height - r.top }
+ };
+ glVertexPointer(2, GL_FLOAT, 0, vertices);
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+ }
+ hw->compositionComplete();
+ // FIXME
+ if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
+ eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
+ }
}
+ }
+ }
- // post surfaces (if needed)
- handlePageFlip();
+ postFramebuffer();
-// signalRefresh();
-//
-// } break;
-//
-// case MessageQueue::REFRESH: {
+ if (mDebugRegion > 1) {
+ usleep(mDebugRegion * 1000);
+ }
+}
- handleRefresh();
+void SurfaceFlinger::preComposition()
+{
+ bool needExtraInvalidate = false;
+ const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
+ const size_t count = currentLayers.size();
+ for (size_t i=0 ; i<count ; i++) {
+ if (currentLayers[i]->onPreComposition()) {
+ needExtraInvalidate = true;
+ }
+ }
+ if (needExtraInvalidate) {
+ signalLayerUpdate();
+ }
+}
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
+void SurfaceFlinger::postComposition()
+{
+ const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
+ const size_t count = currentLayers.size();
+ for (size_t i=0 ; i<count ; i++) {
+ currentLayers[i]->onPostComposition();
+ }
+}
-// if (mDirtyRegion.isEmpty()) {
-// return;
-// }
+void SurfaceFlinger::rebuildLayerStacks() {
+ // rebuild the visible layer list per screen
+ if (CC_UNLIKELY(mVisibleRegionsDirty)) {
+ ATRACE_CALL();
+ mVisibleRegionsDirty = false;
+ invalidateHwcGeometry();
+ const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ const sp<DisplayDevice>& hw(mDisplays[dpy]);
+ Region opaqueRegion;
+ Region dirtyRegion;
+ computeVisibleRegions(currentLayers,
+ hw->getLayerStack(), dirtyRegion, opaqueRegion);
+ hw->dirtyRegion.orSelf(dirtyRegion);
- if (CC_UNLIKELY(mHwWorkListDirty)) {
- // build the h/w work list
- handleWorkList();
+ Vector< sp<LayerBase> > layersSortedByZ;
+ const size_t count = currentLayers.size();
+ for (size_t i=0 ; i<count ; i++) {
+ const Layer::State& s(currentLayers[i]->drawingState());
+ if (s.layerStack == hw->getLayerStack()) {
+ if (!currentLayers[i]->visibleRegion.isEmpty()) {
+ layersSortedByZ.add(currentLayers[i]);
+ }
+ }
}
+ hw->setVisibleLayersSortedByZ(layersSortedByZ);
+ hw->undefinedRegion.set(hw->getBounds());
+ hw->undefinedRegion.subtractSelf(
+ hw->getTransform().transform(opaqueRegion));
+ }
+ }
+}
- if (CC_LIKELY(hw.canDraw())) {
- // repaint the framebuffer (if needed)
- handleRepaint();
- // inform the h/w that we're done compositing
- hw.compositionComplete();
- postFramebuffer();
- } else {
- // pretend we did the post
- hw.compositionComplete();
+void SurfaceFlinger::setUpHWComposer() {
+ HWComposer& hwc(getHwComposer());
+ if (hwc.initCheck() == NO_ERROR) {
+ // build the h/w work list
+ const bool workListsDirty = mHwWorkListDirty;
+ mHwWorkListDirty = false;
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ sp<const DisplayDevice> hw(mDisplays[dpy]);
+ const Vector< sp<LayerBase> >& currentLayers(
+ hw->getVisibleLayersSortedByZ());
+ const size_t count = currentLayers.size();
+
+ const int32_t id = hw->getDisplayId();
+ if (hwc.createWorkList(id, count) >= 0) {
+ HWComposer::LayerListIterator cur = hwc.begin(id);
+ const HWComposer::LayerListIterator end = hwc.end(id);
+ for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
+ const sp<LayerBase>& layer(currentLayers[i]);
+
+ if (CC_UNLIKELY(workListsDirty)) {
+ layer->setGeometry(hw, *cur);
+ if (mDebugDisableHWC || mDebugRegion) {
+ cur->setSkip(true);
+ }
+ }
+
+ /*
+ * update the per-frame h/w composer data for each layer
+ * and build the transparent region of the FB
+ */
+ layer->setPerFrameData(hw, *cur);
+ }
}
+ }
+ status_t err = hwc.prepare();
+ ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
+ }
+}
- } break;
+void SurfaceFlinger::doComposition() {
+ ATRACE_CALL();
+ const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ const sp<DisplayDevice>& hw(mDisplays[dpy]);
+ if (hw->canDraw()) {
+ // transform the dirty region into this screen's coordinate space
+ const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
+ if (!dirtyRegion.isEmpty()) {
+ // repaint the framebuffer (if needed)
+ doDisplayComposition(hw, dirtyRegion);
+ }
+ hw->dirtyRegion.clear();
+ hw->flip(hw->swapRegion);
+ hw->swapRegion.clear();
+ }
+ // inform the h/w that we're done compositing
+ hw->compositionComplete();
}
+ postFramebuffer();
}
void SurfaceFlinger::postFramebuffer()
{
ATRACE_CALL();
- // mSwapRegion can be empty here is some cases, for instance if a hidden
- // or fully transparent window is updating.
- // in that case, we need to flip anyways to not risk a deadlock with
- // h/w composer.
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
const nsecs_t now = systemTime();
mDebugInSwapBuffers = now;
- hw.flip(mSwapRegion);
- size_t numLayers = mVisibleLayersSortedByZ.size();
- for (size_t i = 0; i < numLayers; i++) {
- mVisibleLayersSortedByZ[i]->onLayerDisplayed();
+ HWComposer& hwc(getHwComposer());
+ if (hwc.initCheck() == NO_ERROR) {
+ // FIXME: eventually commit() won't take arguments
+ // FIXME: EGL spec says:
+ // "surface must be bound to the calling thread's current context,
+ // for the current rendering API."
+ DisplayDevice::makeCurrent(
+ getDisplayDevice(DisplayDevice::DISPLAY_ID_MAIN), mEGLContext);
+ hwc.commit(mEGLDisplay, getDefaultDisplayDevice()->getEGLSurface());
+ }
+
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ sp<const DisplayDevice> hw(mDisplays[dpy]);
+ const Vector< sp<LayerBase> >& currentLayers(hw->getVisibleLayersSortedByZ());
+ const size_t count = currentLayers.size();
+ if (hwc.initCheck() == NO_ERROR) {
+ int32_t id = hw->getDisplayId();
+ HWComposer::LayerListIterator cur = hwc.begin(id);
+ const HWComposer::LayerListIterator end = hwc.end(id);
+ for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
+ currentLayers[i]->onLayerDisplayed(hw, &*cur);
+ }
+ } else {
+ for (size_t i = 0; i < count; i++) {
+ currentLayers[i]->onLayerDisplayed(hw, NULL);
+ }
+ }
}
mLastSwapBufferTime = systemTime() - now;
mDebugInSwapBuffers = 0;
- mSwapRegion.clear();
}
void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
@@ -493,8 +886,7 @@ void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
// with mStateLock held to guarantee that mCurrentState won't change
// until the transaction is committed.
- const uint32_t mask = eTransactionNeeded | eTraversalNeeded;
- transactionFlags = getTransactionFlags(mask);
+ transactionFlags = getTransactionFlags(eTransactionMask);
handleTransactionLocked(transactionFlags);
mLastTransactionTime = systemTime() - now;
@@ -513,8 +905,7 @@ void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
* (perform the transaction for each of them if needed)
*/
- const bool layersNeedTransaction = transactionFlags & eTraversalNeeded;
- if (layersNeedTransaction) {
+ if (transactionFlags & eTraversalNeeded) {
for (size_t i=0 ; i<count ; i++) {
const sp<LayerBase>& layer = currentLayers[i];
uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
@@ -527,49 +918,107 @@ void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
}
/*
- * Perform our own transaction if needed
+ * Perform display own transactions if needed
*/
- if (transactionFlags & eTransactionNeeded) {
- if (mCurrentState.orientation != mDrawingState.orientation) {
- // the orientation has changed, recompute all visible regions
- // and invalidate everything.
-
- const int dpy = 0;
- const int orientation = mCurrentState.orientation;
- // Currently unused: const uint32_t flags = mCurrentState.orientationFlags;
- GraphicPlane& plane(graphicPlane(dpy));
- plane.setOrientation(orientation);
-
- // update the shared control block
- const DisplayHardware& hw(plane.displayHardware());
- volatile display_cblk_t* dcblk = mServerCblk->displays + dpy;
- dcblk->orientation = orientation;
- dcblk->w = plane.getWidth();
- dcblk->h = plane.getHeight();
-
+ if (transactionFlags & eDisplayTransactionNeeded) {
+ // here we take advantage of Vector's copy-on-write semantics to
+ // improve performance by skipping the transaction entirely when
+ // know that the lists are identical
+ const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
+ const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
+ if (!curr.isIdenticalTo(draw)) {
mVisibleRegionsDirty = true;
- mDirtyRegion.set(hw.bounds());
- }
+ const size_t cc = curr.size();
+ const size_t dc = draw.size();
+
+ // find the displays that were removed
+ // (ie: in drawing state but not in current state)
+ // also handle displays that changed
+ // (ie: displays that are in both lists)
+ for (size_t i=0 ; i<dc ; i++) {
+ const ssize_t j = curr.indexOfKey(draw.keyAt(i));
+ if (j < 0) {
+ // in drawing state but not in current state
+ if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) {
+ mDisplays.removeItem(draw[i].id);
+ } else {
+ ALOGW("trying to remove the main display");
+ }
+ } else {
+ // this display is in both lists. see if something changed.
+ const DisplayDeviceState& state(curr[j]);
+ if (state.surface->asBinder() != draw[i].surface->asBinder()) {
+ // changing the surface is like destroying and
+ // recreating the DisplayDevice
+
+ sp<SurfaceTextureClient> stc(
+ new SurfaceTextureClient(state.surface));
+
+ sp<DisplayDevice> disp = new DisplayDevice(this,
+ state.id, stc, 0, mEGLConfig);
+
+ disp->setLayerStack(state.layerStack);
+ disp->setOrientation(state.orientation);
+ // TODO: take viewport and frame into account
+ mDisplays.replaceValueFor(state.id, disp);
+ }
+ if (state.layerStack != draw[i].layerStack) {
+ const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
+ disp->setLayerStack(state.layerStack);
+ }
+ if (state.orientation != draw[i].orientation ||
+ state.viewport != draw[i].viewport ||
+ state.frame != draw[i].frame) {
+ const sp<DisplayDevice>& disp(getDisplayDevice(state.id));
+ disp->setOrientation(state.orientation);
+ // TODO: take viewport and frame into account
+ }
+ }
+ }
- if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) {
- // layers have been added
- mVisibleRegionsDirty = true;
+ // find displays that were added
+ // (ie: in current state but not in drawing state)
+ for (size_t i=0 ; i<cc ; i++) {
+ if (draw.indexOfKey(curr.keyAt(i)) < 0) {
+ const DisplayDeviceState& state(curr[i]);
+ sp<SurfaceTextureClient> stc(
+ new SurfaceTextureClient(state.surface));
+ sp<DisplayDevice> disp = new DisplayDevice(this, state.id,
+ stc, 0, mEGLConfig);
+ mDisplays.add(state.id, disp);
+ }
+ }
}
+ }
- // some layers might have been removed, so
- // we need to update the regions they're exposing.
- if (mLayersRemoved) {
- mLayersRemoved = false;
- mVisibleRegionsDirty = true;
- const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
- const size_t count = previousLayers.size();
- for (size_t i=0 ; i<count ; i++) {
- const sp<LayerBase>& layer(previousLayers[i]);
- if (currentLayers.indexOf( layer ) < 0) {
- // this layer is not visible anymore
- mDirtyRegionRemovedLayer.orSelf(layer->visibleRegionScreen);
- }
+ /*
+ * Perform our own transaction if needed
+ */
+
+ const LayerVector& previousLayers(mDrawingState.layersSortedByZ);
+ if (currentLayers.size() > previousLayers.size()) {
+ // layers have been added
+ mVisibleRegionsDirty = true;
+ }
+
+ // some layers might have been removed, so
+ // we need to update the regions they're exposing.
+ if (mLayersRemoved) {
+ mLayersRemoved = false;
+ mVisibleRegionsDirty = true;
+ const size_t count = previousLayers.size();
+ for (size_t i=0 ; i<count ; i++) {
+ const sp<LayerBase>& layer(previousLayers[i]);
+ if (currentLayers.indexOf(layer) < 0) {
+ // this layer is not visible anymore
+ // TODO: we could traverse the tree from front to back and
+ // compute the actual visible region
+ // TODO: we could cache the transformed region
+ Layer::State front(layer->drawingState());
+ Region visibleReg = front.transform.transform(
+ Region(Rect(front.active.w, front.active.h)));
+ invalidateLayerStack(front.layerStack, visibleReg);
}
}
}
@@ -577,30 +1026,44 @@ void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
commitTransaction();
}
+void SurfaceFlinger::commitTransaction()
+{
+ if (!mLayersPendingRemoval.isEmpty()) {
+ // Notify removed layers now that they can't be drawn from
+ for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
+ mLayersPendingRemoval[i]->onRemoved();
+ }
+ mLayersPendingRemoval.clear();
+ }
+
+ mDrawingState = mCurrentState;
+ mTransationPending = false;
+ mTransactionCV.broadcast();
+}
+
void SurfaceFlinger::computeVisibleRegions(
- const LayerVector& currentLayers, Region& dirtyRegion, Region& opaqueRegion)
+ const LayerVector& currentLayers, uint32_t layerStack,
+ Region& outDirtyRegion, Region& outOpaqueRegion)
{
ATRACE_CALL();
- const GraphicPlane& plane(graphicPlane(0));
- const Transform& planeTransform(plane.transform());
- const DisplayHardware& hw(plane.displayHardware());
- const Region screenRegion(hw.bounds());
-
Region aboveOpaqueLayers;
Region aboveCoveredLayers;
Region dirty;
- bool secureFrameBuffer = false;
+ outDirtyRegion.clear();
size_t i = currentLayers.size();
while (i--) {
const sp<LayerBase>& layer = currentLayers[i];
- layer->validateVisibility(planeTransform);
// start with the whole surface at its current location
const Layer::State& s(layer->drawingState());
+ // only consider the layers on the given later stack
+ if (s.layerStack != layerStack)
+ continue;
+
/*
* opaqueRegion: area of a surface that is fully opaque.
*/
@@ -622,19 +1085,32 @@ void SurfaceFlinger::computeVisibleRegions(
// handle hidden surfaces by setting the visible region to empty
- if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) {
+ if (CC_LIKELY(!(s.flags & layer_state_t::eLayerHidden) && s.alpha)) {
const bool translucent = !layer->isOpaque();
- const Rect bounds(layer->visibleBounds());
+ Rect bounds(layer->computeBounds());
visibleRegion.set(bounds);
- visibleRegion.andSelf(screenRegion);
if (!visibleRegion.isEmpty()) {
// Remove the transparent area from the visible region
if (translucent) {
- visibleRegion.subtractSelf(layer->transparentRegionScreen);
+ Region transparentRegionScreen;
+ const Transform tr(s.transform);
+ if (tr.transformed()) {
+ if (tr.preserveRects()) {
+ // transform the transparent region
+ transparentRegionScreen = tr.transform(s.transparentRegion);
+ } else {
+ // transformation too complex, can't do the
+ // transparent region optimization.
+ transparentRegionScreen.clear();
+ }
+ } else {
+ transparentRegionScreen = s.transparentRegion;
+ }
+ visibleRegion.subtractSelf(transparentRegionScreen);
}
// compute the opaque region
- const int32_t layerOrientation = layer->getOrientation();
+ const int32_t layerOrientation = s.transform.getOrientation();
if (s.alpha==255 && !translucent &&
((layerOrientation & Transform::ROT_INVALID) == false)) {
// the opaque region is the layer's footprint
@@ -657,7 +1133,7 @@ void SurfaceFlinger::computeVisibleRegions(
// we need to invalidate the whole region
dirty = visibleRegion;
// as well, as the old visible region
- dirty.orSelf(layer->visibleRegionScreen);
+ dirty.orSelf(layer->visibleRegion);
layer->contentDirty = false;
} else {
/* compute the exposed region:
@@ -673,15 +1149,15 @@ void SurfaceFlinger::computeVisibleRegions(
* exposed because of a resize.
*/
const Region newExposed = visibleRegion - coveredRegion;
- const Region oldVisibleRegion = layer->visibleRegionScreen;
- const Region oldCoveredRegion = layer->coveredRegionScreen;
+ const Region oldVisibleRegion = layer->visibleRegion;
+ const Region oldCoveredRegion = layer->coveredRegion;
const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
}
dirty.subtractSelf(aboveOpaqueLayers);
// accumulate to the screen dirty region
- dirtyRegion.orSelf(dirty);
+ outDirtyRegion.orSelf(dirty);
// Update aboveOpaqueLayers for next (lower) layer
aboveOpaqueLayers.orSelf(opaqueRegion);
@@ -689,225 +1165,107 @@ void SurfaceFlinger::computeVisibleRegions(
// Store the visible region is screen space
layer->setVisibleRegion(visibleRegion);
layer->setCoveredRegion(coveredRegion);
-
- // If a secure layer is partially visible, lock-down the screen!
- if (layer->isSecure() && !visibleRegion.isEmpty()) {
- secureFrameBuffer = true;
- }
}
- // invalidate the areas where a layer was removed
- dirtyRegion.orSelf(mDirtyRegionRemovedLayer);
- mDirtyRegionRemovedLayer.clear();
-
- mSecureFrameBuffer = secureFrameBuffer;
- opaqueRegion = aboveOpaqueLayers;
+ outOpaqueRegion = aboveOpaqueLayers;
}
-
-void SurfaceFlinger::commitTransaction()
-{
- if (!mLayersPendingRemoval.isEmpty()) {
- // Notify removed layers now that they can't be drawn from
- for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
- mLayersPendingRemoval[i]->onRemoved();
+void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
+ const Region& dirty) {
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ const sp<DisplayDevice>& hw(mDisplays[dpy]);
+ if (hw->getLayerStack() == layerStack) {
+ hw->dirtyRegion.orSelf(dirty);
}
- mLayersPendingRemoval.clear();
}
-
- mDrawingState = mCurrentState;
- mTransationPending = false;
- mTransactionCV.broadcast();
}
void SurfaceFlinger::handlePageFlip()
{
- ATRACE_CALL();
- const DisplayHardware& hw = graphicPlane(0).displayHardware();
- const Region screenRegion(hw.bounds());
+ Region dirtyRegion;
- const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
- const bool visibleRegions = lockPageFlip(currentLayers);
-
- if (visibleRegions || mVisibleRegionsDirty) {
- Region opaqueRegion;
- computeVisibleRegions(currentLayers, mDirtyRegion, opaqueRegion);
-
- /*
- * rebuild the visible layer list
- */
- const size_t count = currentLayers.size();
- mVisibleLayersSortedByZ.clear();
- mVisibleLayersSortedByZ.setCapacity(count);
- for (size_t i=0 ; i<count ; i++) {
- if (!currentLayers[i]->visibleRegionScreen.isEmpty())
- mVisibleLayersSortedByZ.add(currentLayers[i]);
- }
-
- mWormholeRegion = screenRegion.subtract(opaqueRegion);
- mVisibleRegionsDirty = false;
- invalidateHwcGeometry();
- }
-
- unlockPageFlip(currentLayers);
-
- mDirtyRegion.orSelf(getAndClearInvalidateRegion());
- mDirtyRegion.andSelf(screenRegion);
-}
-
-void SurfaceFlinger::invalidateHwcGeometry()
-{
- mHwWorkListDirty = true;
-}
-
-bool SurfaceFlinger::lockPageFlip(const LayerVector& currentLayers)
-{
- bool recomputeVisibleRegions = false;
- size_t count = currentLayers.size();
- sp<LayerBase> const* layers = currentLayers.array();
- for (size_t i=0 ; i<count ; i++) {
- const sp<LayerBase>& layer(layers[i]);
- layer->lockPageFlip(recomputeVisibleRegions);
- }
- return recomputeVisibleRegions;
-}
-
-void SurfaceFlinger::unlockPageFlip(const LayerVector& currentLayers)
-{
- const GraphicPlane& plane(graphicPlane(0));
- const Transform& planeTransform(plane.transform());
- const size_t count = currentLayers.size();
- sp<LayerBase> const* layers = currentLayers.array();
- for (size_t i=0 ; i<count ; i++) {
- const sp<LayerBase>& layer(layers[i]);
- layer->unlockPageFlip(planeTransform, mDirtyRegion);
- }
-}
-
-void SurfaceFlinger::handleRefresh()
-{
- bool needInvalidate = false;
+ bool visibleRegions = false;
const LayerVector& currentLayers(mDrawingState.layersSortedByZ);
const size_t count = currentLayers.size();
for (size_t i=0 ; i<count ; i++) {
const sp<LayerBase>& layer(currentLayers[i]);
- if (layer->onPreComposition()) {
- needInvalidate = true;
- }
- }
- if (needInvalidate) {
- signalLayerUpdate();
+ const Region dirty(layer->latchBuffer(visibleRegions));
+ Layer::State s(layer->drawingState());
+ invalidateLayerStack(s.layerStack, dirty);
}
-}
+ mVisibleRegionsDirty |= visibleRegions;
+}
-void SurfaceFlinger::handleWorkList()
+void SurfaceFlinger::invalidateHwcGeometry()
{
- mHwWorkListDirty = false;
- HWComposer& hwc(graphicPlane(0).displayHardware().getHwComposer());
- if (hwc.initCheck() == NO_ERROR) {
- const Vector< sp<LayerBase> >& currentLayers(mVisibleLayersSortedByZ);
- const size_t count = currentLayers.size();
- hwc.createWorkList(count);
- hwc_layer_t* const cur(hwc.getLayers());
- for (size_t i=0 ; cur && i<count ; i++) {
- currentLayers[i]->setGeometry(&cur[i]);
- if (mDebugDisableHWC || mDebugRegion) {
- cur[i].compositionType = HWC_FRAMEBUFFER;
- cur[i].flags |= HWC_SKIP_LAYER;
- }
- }
- }
+ mHwWorkListDirty = true;
}
-void SurfaceFlinger::handleRepaint()
+
+void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
+ const Region& inDirtyRegion)
{
- ATRACE_CALL();
+ Region dirtyRegion(inDirtyRegion);
// compute the invalid region
- mSwapRegion.orSelf(mDirtyRegion);
+ hw->swapRegion.orSelf(dirtyRegion);
- if (CC_UNLIKELY(mDebugRegion)) {
- debugFlashRegions();
- }
-
- // set the frame buffer
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- uint32_t flags = hw.getFlags();
- if (flags & DisplayHardware::SWAP_RECTANGLE) {
+ uint32_t flags = hw->getFlags();
+ if (flags & DisplayDevice::SWAP_RECTANGLE) {
// we can redraw only what's dirty, but since SWAP_RECTANGLE only
// takes a rectangle, we must make sure to update that whole
// rectangle in that case
- mDirtyRegion.set(mSwapRegion.bounds());
+ dirtyRegion.set(hw->swapRegion.bounds());
} else {
- if (flags & DisplayHardware::PARTIAL_UPDATES) {
+ if (flags & DisplayDevice::PARTIAL_UPDATES) {
// We need to redraw the rectangle that will be updated
// (pushed to the framebuffer).
// This is needed because PARTIAL_UPDATES only takes one
- // rectangle instead of a region (see DisplayHardware::flip())
- mDirtyRegion.set(mSwapRegion.bounds());
+ // rectangle instead of a region (see DisplayDevice::flip())
+ dirtyRegion.set(hw->swapRegion.bounds());
} else {
// we need to redraw everything (the whole screen)
- mDirtyRegion.set(hw.bounds());
- mSwapRegion = mDirtyRegion;
+ dirtyRegion.set(hw->bounds());
+ hw->swapRegion = dirtyRegion;
}
}
- setupHardwareComposer();
- composeSurfaces(mDirtyRegion);
+ doComposeSurfaces(hw, dirtyRegion);
+
+ // FIXME: we need to call eglSwapBuffers() on displays that have
+ // GL composition and only on those.
+ // however, currently hwc.commit() already does that for the main
+ // display and never for the other ones
+ if (hw->getDisplayId() >= DisplayDevice::DISPLAY_ID_COUNT) {
+ // FIXME: EGL spec says:
+ // "surface must be bound to the calling thread's current context,
+ // for the current rendering API."
+ eglSwapBuffers(mEGLDisplay, hw->getEGLSurface());
+ }
// update the swap region and clear the dirty region
- mSwapRegion.orSelf(mDirtyRegion);
- mDirtyRegion.clear();
+ hw->swapRegion.orSelf(dirtyRegion);
}
-void SurfaceFlinger::setupHardwareComposer()
+void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
{
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- HWComposer& hwc(hw.getHwComposer());
- hwc_layer_t* const cur(hwc.getLayers());
- if (!cur) {
- return;
- }
-
- const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
- size_t count = layers.size();
+ HWComposer& hwc(getHwComposer());
+ int32_t id = hw->getDisplayId();
+ HWComposer::LayerListIterator cur = hwc.begin(id);
+ const HWComposer::LayerListIterator end = hwc.end(id);
- ALOGE_IF(hwc.getNumLayers() != count,
- "HAL number of layers (%d) doesn't match surfaceflinger (%d)",
- hwc.getNumLayers(), count);
-
- // just to be extra-safe, use the smallest count
- if (hwc.initCheck() == NO_ERROR) {
- count = count < hwc.getNumLayers() ? count : hwc.getNumLayers();
- }
+ const size_t fbLayerCount = hwc.getLayerCount(id, HWC_FRAMEBUFFER);
+ if (cur==end || fbLayerCount) {
- /*
- * update the per-frame h/w composer data for each layer
- * and build the transparent region of the FB
- */
- for (size_t i=0 ; i<count ; i++) {
- const sp<LayerBase>& layer(layers[i]);
- layer->setPerFrameData(&cur[i]);
- }
- status_t err = hwc.prepare();
- ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
-}
+ DisplayDevice::makeCurrent(hw, mEGLContext);
-void SurfaceFlinger::composeSurfaces(const Region& dirty)
-{
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- HWComposer& hwc(hw.getHwComposer());
- hwc_layer_t* const cur(hwc.getLayers());
+ // set the frame buffer
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
- const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER);
- if (!cur || fbLayerCount) {
// Never touch the framebuffer if we don't have any framebuffer layers
-
- if (hwc.getLayerCount(HWC_OVERLAY)) {
+ if (hwc.getLayerCount(id, HWC_OVERLAY)) {
// when using overlays, we assume a fully transparent framebuffer
// NOTE: we could reduce how much we need to clear, for instance
// remove where there are opaque FB layers. however, on some
@@ -916,10 +1274,11 @@ void SurfaceFlinger::composeSurfaces(const Region& dirty)
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
} else {
+ const Region region(hw->undefinedRegion.intersect(dirty));
// screen is already cleared here
- if (!mWormholeRegion.isEmpty()) {
+ if (!region.isEmpty()) {
// can happen with SurfaceView
- drawWormhole();
+ drawWormhole(region);
}
}
@@ -927,82 +1286,40 @@ void SurfaceFlinger::composeSurfaces(const Region& dirty)
* and then, render the layers targeted at the framebuffer
*/
- const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
+ const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
const size_t count = layers.size();
-
- for (size_t i=0 ; i<count ; i++) {
+ const Transform& tr = hw->getTransform();
+ for (size_t i=0 ; i<count ; ++i) {
const sp<LayerBase>& layer(layers[i]);
- const Region clip(dirty.intersect(layer->visibleRegionScreen));
- if (!clip.isEmpty()) {
- if (cur && (cur[i].compositionType == HWC_OVERLAY)) {
- if (i && (cur[i].hints & HWC_HINT_CLEAR_FB)
- && layer->isOpaque()) {
- // never clear the very first layer since we're
- // guaranteed the FB is already cleared
- layer->clearWithOpenGL(clip);
+ const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
+ if (cur != end) {
+ // we're using h/w composer
+ if (!clip.isEmpty()) {
+ if (cur->getCompositionType() == HWC_OVERLAY) {
+ if (i && (cur->getHints() & HWC_HINT_CLEAR_FB)
+ && layer->isOpaque()) {
+ // never clear the very first layer since we're
+ // guaranteed the FB is already cleared
+ layer->clearWithOpenGL(hw, clip);
+ }
+ } else {
+ layer->draw(hw, clip);
}
- continue;
+ layer->setAcquireFence(hw, *cur);
+ }
+ ++cur;
+ } else {
+ // we're not using h/w composer
+ if (!clip.isEmpty()) {
+ layer->draw(hw, clip);
}
- // render the layer
- layer->draw(clip);
}
}
}
}
-void SurfaceFlinger::debugFlashRegions()
-{
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- const uint32_t flags = hw.getFlags();
- const int32_t height = hw.getHeight();
- if (mSwapRegion.isEmpty()) {
- return;
- }
-
- if (!(flags & DisplayHardware::SWAP_RECTANGLE)) {
- const Region repaint((flags & DisplayHardware::PARTIAL_UPDATES) ?
- mDirtyRegion.bounds() : hw.bounds());
- composeSurfaces(repaint);
- }
-
- glDisable(GL_TEXTURE_EXTERNAL_OES);
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
-
- static int toggle = 0;
- toggle = 1 - toggle;
- if (toggle) {
- glColor4f(1, 0, 1, 1);
- } else {
- glColor4f(1, 1, 0, 1);
- }
-
- Region::const_iterator it = mDirtyRegion.begin();
- Region::const_iterator const end = mDirtyRegion.end();
- while (it != end) {
- const Rect& r = *it++;
- GLfloat vertices[][2] = {
- { r.left, height - r.top },
- { r.left, height - r.bottom },
- { r.right, height - r.bottom },
- { r.right, height - r.top }
- };
- glVertexPointer(2, GL_FLOAT, 0, vertices);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- }
-
- hw.flip(mSwapRegion);
-
- if (mDebugRegion > 1)
- usleep(mDebugRegion * 1000);
-}
-
-void SurfaceFlinger::drawWormhole() const
+void SurfaceFlinger::drawWormhole(const Region& region) const
{
- const Region region(mWormholeRegion.intersect(mDirtyRegion));
- if (region.isEmpty())
- return;
-
glDisable(GL_TEXTURE_EXTERNAL_OES);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
@@ -1026,30 +1343,15 @@ void SurfaceFlinger::drawWormhole() const
}
}
-status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
-{
- Mutex::Autolock _l(mStateLock);
- addLayer_l(layer);
- setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
- return NO_ERROR;
-}
-
-status_t SurfaceFlinger::addLayer_l(const sp<LayerBase>& layer)
-{
- ssize_t i = mCurrentState.layersSortedByZ.add(layer);
- return (i < 0) ? status_t(i) : status_t(NO_ERROR);
-}
-
ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
const sp<LayerBaseClient>& lbc)
{
// attach this layer to the client
size_t name = client->attachLayer(lbc);
- Mutex::Autolock _l(mStateLock);
-
// add this layer to the current state list
- addLayer_l(lbc);
+ Mutex::Autolock _l(mStateLock);
+ mCurrentState.layersSortedByZ.add(lbc);
return ssize_t(name);
}
@@ -1065,10 +1367,6 @@ status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer)
status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase)
{
- sp<LayerBaseClient> lbc(layerBase->getLayerBaseClient());
- if (lbc != 0) {
- mLayerMap.removeItem( lbc->getSurfaceBinder() );
- }
ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase);
if (index >= 0) {
mLayersRemoved = true;
@@ -1095,13 +1393,6 @@ status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase)
return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err;
}
-status_t SurfaceFlinger::invalidateLayerVisibility(const sp<LayerBase>& layer)
-{
- layer->forceVisibilityTransaction();
- setTransactionFlags(eTraversalNeeded);
- return NO_ERROR;
-}
-
uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
{
return android_atomic_release_load(&mTransactionFlags);
@@ -1121,23 +1412,21 @@ uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
return old;
}
-
-void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
- int orientation, uint32_t flags) {
+void SurfaceFlinger::setTransactionState(
+ const Vector<ComposerState>& state,
+ const Vector<DisplayState>& displays,
+ uint32_t flags)
+{
Mutex::Autolock _l(mStateLock);
-
uint32_t transactionFlags = 0;
- if (mCurrentState.orientation != orientation) {
- if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
- mCurrentState.orientation = orientation;
- transactionFlags |= eTransactionNeeded;
- } else if (orientation != eOrientationUnchanged) {
- ALOGW("setTransactionState: ignoring unrecognized orientation: %d",
- orientation);
- }
+
+ size_t count = displays.size();
+ for (size_t i=0 ; i<count ; i++) {
+ const DisplayState& s(displays[i]);
+ transactionFlags |= setDisplayStateLocked(s);
}
- const size_t count = state.size();
+ count = state.size();
for (size_t i=0 ; i<count ; i++) {
const ComposerState& s(state[i]);
sp<Client> client( static_cast<Client *>(s.client.get()) );
@@ -1166,7 +1455,110 @@ void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,
}
}
-sp<ISurface> SurfaceFlinger::createSurface(
+uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
+{
+ uint32_t flags = 0;
+ DisplayDeviceState& disp(mCurrentState.displays.editValueFor(s.token));
+ if (disp.id >= 0) {
+ const uint32_t what = s.what;
+ if (what & DisplayState::eSurfaceChanged) {
+ if (disp.surface->asBinder() != s.surface->asBinder()) {
+ disp.surface = s.surface;
+ flags |= eDisplayTransactionNeeded;
+ }
+ }
+ if (what & DisplayState::eLayerStackChanged) {
+ if (disp.layerStack != s.layerStack) {
+ disp.layerStack = s.layerStack;
+ flags |= eDisplayTransactionNeeded;
+ }
+ }
+ if (what & DisplayState::eOrientationChanged) {
+ if (disp.orientation != s.orientation) {
+ disp.orientation = s.orientation;
+ flags |= eDisplayTransactionNeeded;
+ }
+ }
+ if (what & DisplayState::eFrameChanged) {
+ if (disp.frame != s.frame) {
+ disp.frame = s.frame;
+ flags |= eDisplayTransactionNeeded;
+ }
+ }
+ if (what & DisplayState::eViewportChanged) {
+ if (disp.viewport != s.viewport) {
+ disp.viewport = s.viewport;
+ flags |= eDisplayTransactionNeeded;
+ }
+ }
+ }
+ return flags;
+}
+
+uint32_t SurfaceFlinger::setClientStateLocked(
+ const sp<Client>& client,
+ const layer_state_t& s)
+{
+ uint32_t flags = 0;
+ sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
+ if (layer != 0) {
+ const uint32_t what = s.what;
+ if (what & layer_state_t::ePositionChanged) {
+ if (layer->setPosition(s.x, s.y))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eLayerChanged) {
+ // NOTE: index needs to be calculated before we update the state
+ ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
+ if (layer->setLayer(s.z)) {
+ mCurrentState.layersSortedByZ.removeAt(idx);
+ mCurrentState.layersSortedByZ.add(layer);
+ // we need traversal (state changed)
+ // AND transaction (list changed)
+ flags |= eTransactionNeeded|eTraversalNeeded;
+ }
+ }
+ if (what & layer_state_t::eSizeChanged) {
+ if (layer->setSize(s.w, s.h)) {
+ flags |= eTraversalNeeded;
+ }
+ }
+ if (what & layer_state_t::eAlphaChanged) {
+ if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eMatrixChanged) {
+ if (layer->setMatrix(s.matrix))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eTransparentRegionChanged) {
+ if (layer->setTransparentRegionHint(s.transparentRegion))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eVisibilityChanged) {
+ if (layer->setFlags(s.flags, s.mask))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eCropChanged) {
+ if (layer->setCrop(s.crop))
+ flags |= eTraversalNeeded;
+ }
+ if (what & layer_state_t::eLayerStackChanged) {
+ // NOTE: index needs to be calculated before we update the state
+ ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
+ if (layer->setLayerStack(s.layerStack)) {
+ mCurrentState.layersSortedByZ.removeAt(idx);
+ mCurrentState.layersSortedByZ.add(layer);
+ // we need traversal (state changed)
+ // AND transaction (list changed)
+ flags |= eTransactionNeeded|eTraversalNeeded;
+ }
+ }
+ }
+ return flags;
+}
+
+sp<ISurface> SurfaceFlinger::createLayer(
ISurfaceComposerClient::surface_data_t* params,
const String8& name,
const sp<Client>& client,
@@ -1177,26 +1569,22 @@ sp<ISurface> SurfaceFlinger::createSurface(
sp<ISurface> surfaceHandle;
if (int32_t(w|h) < 0) {
- ALOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",
+ ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
int(w), int(h));
return surfaceHandle;
}
- //ALOGD("createSurface for (%d x %d), name=%s", w, h, name.string());
- sp<Layer> normalLayer;
- switch (flags & eFXSurfaceMask) {
- case eFXSurfaceNormal:
- normalLayer = createNormalSurface(client, d, w, h, flags, format);
- layer = normalLayer;
+ //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
+ switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
+ case ISurfaceComposerClient::eFXSurfaceNormal:
+ layer = createNormalLayer(client, d, w, h, flags, format);
break;
- case eFXSurfaceBlur:
- // for now we treat Blur as Dim, until we can implement it
- // efficiently.
- case eFXSurfaceDim:
- layer = createDimSurface(client, d, w, h, flags);
+ case ISurfaceComposerClient::eFXSurfaceBlur:
+ case ISurfaceComposerClient::eFXSurfaceDim:
+ layer = createDimLayer(client, d, w, h, flags);
break;
- case eFXSurfaceScreenshot:
- layer = createScreenshotSurface(client, d, w, h, flags);
+ case ISurfaceComposerClient::eFXSurfaceScreenshot:
+ layer = createScreenshotLayer(client, d, w, h, flags);
break;
}
@@ -1204,30 +1592,24 @@ sp<ISurface> SurfaceFlinger::createSurface(
layer->initStates(w, h, flags);
layer->setName(name);
ssize_t token = addClientLayer(client, layer);
-
surfaceHandle = layer->getSurface();
if (surfaceHandle != 0) {
params->token = token;
params->identity = layer->getIdentity();
- if (normalLayer != 0) {
- Mutex::Autolock _l(mStateLock);
- mLayerMap.add(layer->getSurfaceBinder(), normalLayer);
- }
}
-
setTransactionFlags(eTransactionNeeded);
}
return surfaceHandle;
}
-sp<Layer> SurfaceFlinger::createNormalSurface(
+sp<Layer> SurfaceFlinger::createNormalLayer(
const sp<Client>& client, DisplayID display,
uint32_t w, uint32_t h, uint32_t flags,
PixelFormat& format)
{
// initialize the surfaces
- switch (format) { // TODO: take h/w into account
+ switch (format) {
case PIXEL_FORMAT_TRANSPARENT:
case PIXEL_FORMAT_TRANSLUCENT:
format = PIXEL_FORMAT_RGBA_8888;
@@ -1249,13 +1631,13 @@ sp<Layer> SurfaceFlinger::createNormalSurface(
sp<Layer> layer = new Layer(this, display, client);
status_t err = layer->setBuffers(w, h, format, flags);
if (CC_LIKELY(err != NO_ERROR)) {
- ALOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));
+ ALOGE("createNormalLayer() failed (%s)", strerror(-err));
layer.clear();
}
return layer;
}
-sp<LayerDim> SurfaceFlinger::createDimSurface(
+sp<LayerDim> SurfaceFlinger::createDimLayer(
const sp<Client>& client, DisplayID display,
uint32_t w, uint32_t h, uint32_t flags)
{
@@ -1263,7 +1645,7 @@ sp<LayerDim> SurfaceFlinger::createDimSurface(
return layer;
}
-sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
+sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer(
const sp<Client>& client, DisplayID display,
uint32_t w, uint32_t h, uint32_t flags)
{
@@ -1271,7 +1653,7 @@ sp<LayerScreenshot> SurfaceFlinger::createScreenshotSurface(
return layer;
}
-status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
+status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid)
{
/*
* called by the window manager, when a surface should be marked for
@@ -1295,7 +1677,7 @@ status_t SurfaceFlinger::removeSurface(const sp<Client>& client, SurfaceID sid)
return err;
}
-status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
+status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer)
{
// called by ~ISurface() when all references are gone
status_t err = NO_ERROR;
@@ -1317,80 +1699,30 @@ status_t SurfaceFlinger::destroySurface(const wp<LayerBaseClient>& layer)
return err;
}
-uint32_t SurfaceFlinger::setClientStateLocked(
- const sp<Client>& client,
- const layer_state_t& s)
-{
- uint32_t flags = 0;
- sp<LayerBaseClient> layer(client->getLayerUser(s.surface));
- if (layer != 0) {
- const uint32_t what = s.what;
- if (what & ePositionChanged) {
- if (layer->setPosition(s.x, s.y))
- flags |= eTraversalNeeded;
- }
- if (what & eLayerChanged) {
- ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
- if (layer->setLayer(s.z)) {
- mCurrentState.layersSortedByZ.removeAt(idx);
- mCurrentState.layersSortedByZ.add(layer);
- // we need traversal (state changed)
- // AND transaction (list changed)
- flags |= eTransactionNeeded|eTraversalNeeded;
- }
- }
- if (what & eSizeChanged) {
- if (layer->setSize(s.w, s.h)) {
- flags |= eTraversalNeeded;
- }
- }
- if (what & eAlphaChanged) {
- if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
- flags |= eTraversalNeeded;
- }
- if (what & eMatrixChanged) {
- if (layer->setMatrix(s.matrix))
- flags |= eTraversalNeeded;
- }
- if (what & eTransparentRegionChanged) {
- if (layer->setTransparentRegionHint(s.transparentRegion))
- flags |= eTraversalNeeded;
- }
- if (what & eVisibilityChanged) {
- if (layer->setFlags(s.flags, s.mask))
- flags |= eTraversalNeeded;
- }
- if (what & eCropChanged) {
- if (layer->setCrop(s.crop))
- flags |= eTraversalNeeded;
- }
- }
- return flags;
-}
-
// ---------------------------------------------------------------------------
void SurfaceFlinger::onScreenAcquired() {
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- hw.acquireScreen();
+ ALOGD("Screen about to return, flinger = %p", this);
+ sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
+ getHwComposer().acquire();
+ hw->acquireScreen();
mEventThread->onScreenAcquired();
- // this is a temporary work-around, eventually this should be called
- // by the power-manager
- SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode);
- // from this point on, SF will process updates again
+ mVisibleRegionsDirty = true;
repaintEverything();
}
void SurfaceFlinger::onScreenReleased() {
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- if (hw.isScreenAcquired()) {
+ ALOGD("About to give-up screen, flinger = %p", this);
+ sp<const DisplayDevice> hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice
+ if (hw->isScreenAcquired()) {
mEventThread->onScreenReleased();
- hw.releaseScreen();
+ hw->releaseScreen();
+ getHwComposer().release();
// from this point on, SF will stop drawing
}
}
-void SurfaceFlinger::screenAcquired() {
+void SurfaceFlinger::unblank() {
class MessageScreenAcquired : public MessageBase {
SurfaceFlinger* flinger;
public:
@@ -1404,7 +1736,7 @@ void SurfaceFlinger::screenAcquired() {
postMessageSync(msg);
}
-void SurfaceFlinger::screenReleased() {
+void SurfaceFlinger::blank() {
class MessageScreenReleased : public MessageBase {
SurfaceFlinger* flinger;
public:
@@ -1575,12 +1907,34 @@ void SurfaceFlinger::dumpAllLocked(
}
/*
+ * Dump Display state
+ */
+
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ const sp<const DisplayDevice>& hw(mDisplays[dpy]);
+ snprintf(buffer, SIZE,
+ "+ DisplayDevice[%u]\n"
+ " id=%x, layerStack=%u, (%4dx%4d), orient=%2d, tr=%08x, "
+ "flips=%u, secure=%d, numLayers=%u\n",
+ dpy,
+ hw->getDisplayId(), hw->getLayerStack(),
+ hw->getWidth(), hw->getHeight(),
+ hw->getOrientation(), hw->getTransform().getType(),
+ hw->getPageFlipCount(),
+ hw->getSecureLayerVisible(),
+ hw->getVisibleLayersSortedByZ().size());
+ result.append(buffer);
+ }
+
+ /*
* Dump SurfaceFlinger global state
*/
snprintf(buffer, SIZE, "SurfaceFlinger global state:\n");
result.append(buffer);
+ HWComposer& hwc(getHwComposer());
+ sp<const DisplayDevice> hw(getDefaultDisplayDevice());
const GLExtensions& extensions(GLExtensions::getInstance());
snprintf(buffer, SIZE, "GLES: %s, %s, %s\n",
extensions.getVendor(),
@@ -1589,18 +1943,16 @@ void SurfaceFlinger::dumpAllLocked(
result.append(buffer);
snprintf(buffer, SIZE, "EGL : %s\n",
- eglQueryString(graphicPlane(0).getEGLDisplay(),
- EGL_VERSION_HW_ANDROID));
+ eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID));
result.append(buffer);
snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension());
result.append(buffer);
- mWormholeRegion.dump(result, "WormholeRegion");
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
+ hw->undefinedRegion.dump(result, "undefinedRegion");
snprintf(buffer, SIZE,
" orientation=%d, canDraw=%d\n",
- mCurrentState.orientation, hw.canDraw());
+ hw->getOrientation(), hw->canDraw());
result.append(buffer);
snprintf(buffer, SIZE,
" last eglSwapBuffers() time: %f us\n"
@@ -1608,15 +1960,13 @@ void SurfaceFlinger::dumpAllLocked(
" transaction-flags : %08x\n"
" refresh-rate : %f fps\n"
" x-dpi : %f\n"
- " y-dpi : %f\n"
- " density : %f\n",
+ " y-dpi : %f\n",
mLastSwapBufferTime/1000.0,
mLastTransactionTime/1000.0,
mTransactionFlags,
- hw.getRefreshRate(),
- hw.getDpiX(),
- hw.getDpiY(),
- hw.getDensity());
+ 1e9 / hwc.getRefreshPeriod(),
+ hwc.getDpiX(),
+ hwc.getDpiY());
result.append(buffer);
snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n",
@@ -1635,21 +1985,20 @@ void SurfaceFlinger::dumpAllLocked(
/*
* Dump HWComposer state
*/
- HWComposer& hwc(hw.getHwComposer());
snprintf(buffer, SIZE, "h/w composer state:\n");
result.append(buffer);
snprintf(buffer, SIZE, " h/w composer %s and %s\n",
hwc.initCheck()==NO_ERROR ? "present" : "not present",
(mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
result.append(buffer);
- hwc.dump(result, buffer, SIZE, mVisibleLayersSortedByZ);
+ hwc.dump(result, buffer, SIZE, hw->getVisibleLayersSortedByZ());
/*
* Dump gralloc state
*/
const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
alloc.dump(result);
- hw.dump(result);
+ hw->dump(result);
}
status_t SurfaceFlinger::onTransact(
@@ -1658,10 +2007,9 @@ status_t SurfaceFlinger::onTransact(
switch (code) {
case CREATE_CONNECTION:
case SET_TRANSACTION_STATE:
- case SET_ORIENTATION:
case BOOT_FINISHED:
- case TURN_ELECTRON_BEAM_OFF:
- case TURN_ELECTRON_BEAM_ON:
+ case BLANK:
+ case UNBLANK:
{
// codes that require permission check
IPCThreadState* ipc = IPCThreadState::self();
@@ -1718,7 +2066,10 @@ status_t SurfaceFlinger::onTransact(
return NO_ERROR;
}
case 1005:{ // force transaction
- setTransactionFlags(eTransactionNeeded|eTraversalNeeded);
+ setTransactionFlags(
+ eTransactionNeeded|
+ eDisplayTransactionNeeded|
+ eTraversalNeeded);
return NO_ERROR;
}
case 1006:{ // send empty update
@@ -1746,8 +2097,8 @@ status_t SurfaceFlinger::onTransact(
return NO_ERROR;
case 1013: {
Mutex::Autolock _l(mStateLock);
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- reply->writeInt32(hw.getPageFlipCount());
+ sp<const DisplayDevice> hw(getDefaultDisplayDevice());
+ reply->writeInt32(hw->getPageFlipCount());
}
return NO_ERROR;
}
@@ -1756,24 +2107,10 @@ status_t SurfaceFlinger::onTransact(
}
void SurfaceFlinger::repaintEverything() {
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- const Rect bounds(hw.getBounds());
- setInvalidateRegion(Region(bounds));
+ android_atomic_or(1, &mRepaintEverything);
signalTransaction();
}
-void SurfaceFlinger::setInvalidateRegion(const Region& reg) {
- Mutex::Autolock _l(mInvalidateLock);
- mInvalidateRegion = reg;
-}
-
-Region SurfaceFlinger::getAndClearInvalidateRegion() {
- Mutex::Autolock _l(mInvalidateLock);
- Region reg(mInvalidateRegion);
- mInvalidateRegion.clear();
- return reg;
-}
-
// ---------------------------------------------------------------------------
status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy,
@@ -1792,9 +2129,9 @@ status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
return INVALID_OPERATION;
// get screen geometry
- const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
- const uint32_t hw_w = hw.getWidth();
- const uint32_t hw_h = hw.getHeight();
+ sp<const DisplayDevice> hw(getDisplayDevice(dpy));
+ const uint32_t hw_w = hw->getWidth();
+ const uint32_t hw_h = hw->getHeight();
GLfloat u = 1;
GLfloat v = 1;
@@ -1830,14 +2167,14 @@ status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
+ const Vector< sp<LayerBase> >& layers(hw->getVisibleLayersSortedByZ());
const size_t count = layers.size();
for (size_t i=0 ; i<count ; ++i) {
const sp<LayerBase>& layer(layers[i]);
- layer->drawForSreenShot();
+ layer->draw(hw);
}
- hw.compositionComplete();
+ hw->compositionComplete();
// back to main framebuffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
@@ -1851,471 +2188,6 @@ status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy,
// ---------------------------------------------------------------------------
-class VSyncWaiter {
- DisplayEventReceiver::Event buffer[4];
- sp<Looper> looper;
- sp<IDisplayEventConnection> events;
- sp<BitTube> eventTube;
-public:
- VSyncWaiter(const sp<EventThread>& eventThread) {
- looper = new Looper(true);
- events = eventThread->createEventConnection();
- eventTube = events->getDataChannel();
- looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0);
- events->requestNextVsync();
- }
-
- void wait() {
- ssize_t n;
-
- looper->pollOnce(-1);
- // we don't handle any errors here, it doesn't matter
- // and we don't want to take the risk to get stuck.
-
- // drain the events...
- while ((n = DisplayEventReceiver::getEvents(
- eventTube, buffer, 4)) > 0) ;
-
- events->requestNextVsync();
- }
-};
-
-status_t SurfaceFlinger::electronBeamOffAnimationImplLocked()
-{
- // get screen geometry
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- const uint32_t hw_w = hw.getWidth();
- const uint32_t hw_h = hw.getHeight();
- const Region screenBounds(hw.getBounds());
-
- GLfloat u, v;
- GLuint tname;
- status_t result = renderScreenToTextureLocked(0, &tname, &u, &v);
- if (result != NO_ERROR) {
- return result;
- }
-
- GLfloat vtx[8];
- const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} };
- glBindTexture(GL_TEXTURE_2D, tname);
- glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(2, GL_FLOAT, 0, vtx);
-
- /*
- * Texture coordinate mapping
- *
- * u
- * 1 +----------+---+
- * | | | | image is inverted
- * | V | | w.r.t. the texture
- * 1-v +----------+ | coordinates
- * | |
- * | |
- * | |
- * 0 +--------------+
- * 0 1
- *
- */
-
- class s_curve_interpolator {
- const float nbFrames, s, v;
- public:
- s_curve_interpolator(int nbFrames, float s)
- : nbFrames(1.0f / (nbFrames-1)), s(s),
- v(1.0f + expf(-s + 0.5f*s)) {
- }
- float operator()(int f) {
- const float x = f * nbFrames;
- return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
- }
- };
-
- class v_stretch {
- const GLfloat hw_w, hw_h;
- public:
- v_stretch(uint32_t hw_w, uint32_t hw_h)
- : hw_w(hw_w), hw_h(hw_h) {
- }
- void operator()(GLfloat* vtx, float v) {
- const GLfloat w = hw_w + (hw_w * v);
- const GLfloat h = hw_h - (hw_h * v);
- const GLfloat x = (hw_w - w) * 0.5f;
- const GLfloat y = (hw_h - h) * 0.5f;
- vtx[0] = x; vtx[1] = y;
- vtx[2] = x; vtx[3] = y + h;
- vtx[4] = x + w; vtx[5] = y + h;
- vtx[6] = x + w; vtx[7] = y;
- }
- };
-
- class h_stretch {
- const GLfloat hw_w, hw_h;
- public:
- h_stretch(uint32_t hw_w, uint32_t hw_h)
- : hw_w(hw_w), hw_h(hw_h) {
- }
- void operator()(GLfloat* vtx, float v) {
- const GLfloat w = hw_w - (hw_w * v);
- const GLfloat h = 1.0f;
- const GLfloat x = (hw_w - w) * 0.5f;
- const GLfloat y = (hw_h - h) * 0.5f;
- vtx[0] = x; vtx[1] = y;
- vtx[2] = x; vtx[3] = y + h;
- vtx[4] = x + w; vtx[5] = y + h;
- vtx[6] = x + w; vtx[7] = y;
- }
- };
-
- VSyncWaiter vsync(mEventThread);
-
- // the full animation is 24 frames
- char value[PROPERTY_VALUE_MAX];
- property_get("debug.sf.electron_frames", value, "24");
- int nbFrames = (atoi(value) + 1) >> 1;
- if (nbFrames <= 0) // just in case
- nbFrames = 24;
-
- s_curve_interpolator itr(nbFrames, 7.5f);
- s_curve_interpolator itg(nbFrames, 8.0f);
- s_curve_interpolator itb(nbFrames, 8.5f);
-
- v_stretch vverts(hw_w, hw_h);
-
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ONE);
- for (int i=0 ; i<nbFrames ; i++) {
- float x, y, w, h;
- const float vr = itr(i);
- const float vg = itg(i);
- const float vb = itb(i);
-
- // wait for vsync
- vsync.wait();
-
- // clear screen
- glColorMask(1,1,1,1);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_TEXTURE_2D);
-
- // draw the red plane
- vverts(vtx, vr);
- glColorMask(1,0,0,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- // draw the green plane
- vverts(vtx, vg);
- glColorMask(0,1,0,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- // draw the blue plane
- vverts(vtx, vb);
- glColorMask(0,0,1,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- // draw the white highlight (we use the last vertices)
- glDisable(GL_TEXTURE_2D);
- glColorMask(1,1,1,1);
- glColor4f(vg, vg, vg, 1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- hw.flip(screenBounds);
- }
-
- h_stretch hverts(hw_w, hw_h);
- glDisable(GL_BLEND);
- glDisable(GL_TEXTURE_2D);
- glColorMask(1,1,1,1);
- for (int i=0 ; i<nbFrames ; i++) {
- const float v = itg(i);
- hverts(vtx, v);
-
- // wait for vsync
- vsync.wait();
-
- glClear(GL_COLOR_BUFFER_BIT);
- glColor4f(1-v, 1-v, 1-v, 1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- hw.flip(screenBounds);
- }
-
- glColorMask(1,1,1,1);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glDeleteTextures(1, &tname);
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
- return NO_ERROR;
-}
-
-status_t SurfaceFlinger::electronBeamOnAnimationImplLocked()
-{
- status_t result = PERMISSION_DENIED;
-
- if (!GLExtensions::getInstance().haveFramebufferObject())
- return INVALID_OPERATION;
-
-
- // get screen geometry
- const DisplayHardware& hw(graphicPlane(0).displayHardware());
- const uint32_t hw_w = hw.getWidth();
- const uint32_t hw_h = hw.getHeight();
- const Region screenBounds(hw.bounds());
-
- GLfloat u, v;
- GLuint tname;
- result = renderScreenToTextureLocked(0, &tname, &u, &v);
- if (result != NO_ERROR) {
- return result;
- }
-
- GLfloat vtx[8];
- const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} };
- glBindTexture(GL_TEXTURE_2D, tname);
- glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(2, GL_FLOAT, 0, vtx);
-
- class s_curve_interpolator {
- const float nbFrames, s, v;
- public:
- s_curve_interpolator(int nbFrames, float s)
- : nbFrames(1.0f / (nbFrames-1)), s(s),
- v(1.0f + expf(-s + 0.5f*s)) {
- }
- float operator()(int f) {
- const float x = f * nbFrames;
- return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f;
- }
- };
-
- class v_stretch {
- const GLfloat hw_w, hw_h;
- public:
- v_stretch(uint32_t hw_w, uint32_t hw_h)
- : hw_w(hw_w), hw_h(hw_h) {
- }
- void operator()(GLfloat* vtx, float v) {
- const GLfloat w = hw_w + (hw_w * v);
- const GLfloat h = hw_h - (hw_h * v);
- const GLfloat x = (hw_w - w) * 0.5f;
- const GLfloat y = (hw_h - h) * 0.5f;
- vtx[0] = x; vtx[1] = y;
- vtx[2] = x; vtx[3] = y + h;
- vtx[4] = x + w; vtx[5] = y + h;
- vtx[6] = x + w; vtx[7] = y;
- }
- };
-
- class h_stretch {
- const GLfloat hw_w, hw_h;
- public:
- h_stretch(uint32_t hw_w, uint32_t hw_h)
- : hw_w(hw_w), hw_h(hw_h) {
- }
- void operator()(GLfloat* vtx, float v) {
- const GLfloat w = hw_w - (hw_w * v);
- const GLfloat h = 1.0f;
- const GLfloat x = (hw_w - w) * 0.5f;
- const GLfloat y = (hw_h - h) * 0.5f;
- vtx[0] = x; vtx[1] = y;
- vtx[2] = x; vtx[3] = y + h;
- vtx[4] = x + w; vtx[5] = y + h;
- vtx[6] = x + w; vtx[7] = y;
- }
- };
-
- VSyncWaiter vsync(mEventThread);
-
- // the full animation is 12 frames
- int nbFrames = 8;
- s_curve_interpolator itr(nbFrames, 7.5f);
- s_curve_interpolator itg(nbFrames, 8.0f);
- s_curve_interpolator itb(nbFrames, 8.5f);
-
- h_stretch hverts(hw_w, hw_h);
- glDisable(GL_BLEND);
- glDisable(GL_TEXTURE_2D);
- glColorMask(1,1,1,1);
- for (int i=nbFrames-1 ; i>=0 ; i--) {
- const float v = itg(i);
- hverts(vtx, v);
-
- // wait for vsync
- vsync.wait();
-
- glClear(GL_COLOR_BUFFER_BIT);
- glColor4f(1-v, 1-v, 1-v, 1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- hw.flip(screenBounds);
- }
-
- nbFrames = 4;
- v_stretch vverts(hw_w, hw_h);
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ONE);
- for (int i=nbFrames-1 ; i>=0 ; i--) {
- float x, y, w, h;
- const float vr = itr(i);
- const float vg = itg(i);
- const float vb = itb(i);
-
- // wait for vsync
- vsync.wait();
-
- // clear screen
- glColorMask(1,1,1,1);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_TEXTURE_2D);
-
- // draw the red plane
- vverts(vtx, vr);
- glColorMask(1,0,0,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- // draw the green plane
- vverts(vtx, vg);
- glColorMask(0,1,0,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- // draw the blue plane
- vverts(vtx, vb);
- glColorMask(0,0,1,1);
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
- hw.flip(screenBounds);
- }
-
- glColorMask(1,1,1,1);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glDeleteTextures(1, &tname);
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_BLEND);
-
- return NO_ERROR;
-}
-
-// ---------------------------------------------------------------------------
-
-status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode)
-{
- ATRACE_CALL();
-
- DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
- if (!hw.canDraw()) {
- // we're already off
- return NO_ERROR;
- }
-
- // turn off hwc while we're doing the animation
- hw.getHwComposer().disable();
- // and make sure to turn it back on (if needed) next time we compose
- invalidateHwcGeometry();
-
- if (mode & ISurfaceComposer::eElectronBeamAnimationOff) {
- electronBeamOffAnimationImplLocked();
- }
-
- // always clear the whole screen at the end of the animation
- glClearColor(0,0,0,1);
- glClear(GL_COLOR_BUFFER_BIT);
- hw.flip( Region(hw.bounds()) );
-
- return NO_ERROR;
-}
-
-status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode)
-{
- class MessageTurnElectronBeamOff : public MessageBase {
- SurfaceFlinger* flinger;
- int32_t mode;
- status_t result;
- public:
- MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode)
- : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
- }
- status_t getResult() const {
- return result;
- }
- virtual bool handler() {
- Mutex::Autolock _l(flinger->mStateLock);
- result = flinger->turnElectronBeamOffImplLocked(mode);
- return true;
- }
- };
-
- sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode);
- status_t res = postMessageSync(msg);
- if (res == NO_ERROR) {
- res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult();
-
- // work-around: when the power-manager calls us we activate the
- // animation. eventually, the "on" animation will be called
- // by the power-manager itself
- mElectronBeamAnimationMode = mode;
- }
- return res;
-}
-
-// ---------------------------------------------------------------------------
-
-status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode)
-{
- DisplayHardware& hw(graphicPlane(0).editDisplayHardware());
- if (hw.canDraw()) {
- // we're already on
- return NO_ERROR;
- }
- if (mode & ISurfaceComposer::eElectronBeamAnimationOn) {
- electronBeamOnAnimationImplLocked();
- }
-
- // make sure to redraw the whole screen when the animation is done
- mDirtyRegion.set(hw.bounds());
- signalTransaction();
-
- return NO_ERROR;
-}
-
-status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode)
-{
- class MessageTurnElectronBeamOn : public MessageBase {
- SurfaceFlinger* flinger;
- int32_t mode;
- status_t result;
- public:
- MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode)
- : flinger(flinger), mode(mode), result(PERMISSION_DENIED) {
- }
- status_t getResult() const {
- return result;
- }
- virtual bool handler() {
- Mutex::Autolock _l(flinger->mStateLock);
- result = flinger->turnElectronBeamOnImplLocked(mode);
- return true;
- }
- };
-
- postMessageAsync( new MessageTurnElectronBeamOn(this, mode) );
- return NO_ERROR;
-}
-
-// ---------------------------------------------------------------------------
-
status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
sp<IMemoryHeap>* heap,
uint32_t* w, uint32_t* h, PixelFormat* f,
@@ -2327,26 +2199,38 @@ status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
status_t result = PERMISSION_DENIED;
// only one display supported for now
- if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
+ if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
+ ALOGE("invalid display %d", dpy);
return BAD_VALUE;
+ }
- if (!GLExtensions::getInstance().haveFramebufferObject())
+ if (!GLExtensions::getInstance().haveFramebufferObject()) {
return INVALID_OPERATION;
+ }
// get screen geometry
- const DisplayHardware& hw(graphicPlane(dpy).displayHardware());
- const uint32_t hw_w = hw.getWidth();
- const uint32_t hw_h = hw.getHeight();
+ sp<const DisplayDevice> hw(getDisplayDevice(dpy));
+ const uint32_t hw_w = hw->getWidth();
+ const uint32_t hw_h = hw->getHeight();
+
+ // if we have secure windows on this display, never allow the screen capture
+ if (hw->getSecureLayerVisible()) {
+ ALOGW("FB is protected: PERMISSION_DENIED");
+ return PERMISSION_DENIED;
+ }
- if ((sw > hw_w) || (sh > hw_h))
+ if ((sw > hw_w) || (sh > hw_h)) {
+ ALOGE("size mismatch (%d, %d) > (%d, %d)", sw, sh, hw_w, hw_h);
return BAD_VALUE;
+ }
sw = (!sw) ? hw_w : sw;
sh = (!sh) ? hw_h : sh;
const size_t size = sw * sh * 4;
+ const bool filtering = sw != hw_w || sh != hw_h;
- //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
- // sw, sh, minLayerZ, maxLayerZ);
+// ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d",
+// sw, sh, minLayerZ, maxLayerZ);
// make sure to clear all GL error flags
while ( glGetError() != GL_NO_ERROR ) ;
@@ -2383,10 +2267,12 @@ status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
for (size_t i=0 ; i<count ; ++i) {
const sp<LayerBase>& layer(layers[i]);
const uint32_t flags = layer->drawingState().flags;
- if (!(flags & ISurfaceComposer::eLayerHidden)) {
+ if (!(flags & layer_state_t::eLayerHidden)) {
const uint32_t z = layer->drawingState().z;
if (z >= minLayerZ && z <= maxLayerZ) {
- layer->drawForSreenShot();
+ if (filtering) layer->setFiltering(true);
+ layer->draw(hw);
+ if (filtering) layer->setFiltering(false);
}
}
}
@@ -2429,9 +2315,9 @@ status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
glDeleteRenderbuffersOES(1, &tname);
glDeleteFramebuffersOES(1, &name);
- hw.compositionComplete();
+ hw->compositionComplete();
- // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
+// ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK");
return result;
}
@@ -2478,14 +2364,8 @@ status_t SurfaceFlinger::captureScreen(DisplayID dpy,
}
virtual bool handler() {
Mutex::Autolock _l(flinger->mStateLock);
-
- // if we have secure windows, never allow the screen capture
- if (flinger->mSecureFrameBuffer)
- return true;
-
result = flinger->captureScreenImplLocked(dpy,
heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
-
return true;
}
};
@@ -2501,136 +2381,40 @@ status_t SurfaceFlinger::captureScreen(DisplayID dpy,
// ---------------------------------------------------------------------------
-sp<Layer> SurfaceFlinger::getLayer(const sp<ISurface>& sur) const
-{
- sp<Layer> result;
- Mutex::Autolock _l(mStateLock);
- result = mLayerMap.valueFor( sur->asBinder() ).promote();
- return result;
+SurfaceFlinger::LayerVector::LayerVector() {
}
-// ---------------------------------------------------------------------------
-
-Client::Client(const sp<SurfaceFlinger>& flinger)
- : mFlinger(flinger), mNameGenerator(1)
-{
+SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
+ : SortedVector<sp<LayerBase> >(rhs) {
}
-Client::~Client()
+int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
+ const void* rhs) const
{
- const size_t count = mLayers.size();
- for (size_t i=0 ; i<count ; i++) {
- sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
- if (layer != 0) {
- mFlinger->removeLayer(layer);
- }
- }
-}
+ // sort layers per layer-stack, then by z-order and finally by sequence
+ const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs));
+ const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs));
-status_t Client::initCheck() const {
- return NO_ERROR;
-}
+ uint32_t ls = l->currentState().layerStack;
+ uint32_t rs = r->currentState().layerStack;
+ if (ls != rs)
+ return ls - rs;
-size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
-{
- Mutex::Autolock _l(mLock);
- size_t name = mNameGenerator++;
- mLayers.add(name, layer);
- return name;
-}
+ uint32_t lz = l->currentState().z;
+ uint32_t rz = r->currentState().z;
+ if (lz != rz)
+ return lz - rz;
-void Client::detachLayer(const LayerBaseClient* layer)
-{
- Mutex::Autolock _l(mLock);
- // we do a linear search here, because this doesn't happen often
- const size_t count = mLayers.size();
- for (size_t i=0 ; i<count ; i++) {
- if (mLayers.valueAt(i) == layer) {
- mLayers.removeItemsAt(i, 1);
- break;
- }
- }
-}
-sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
-{
- Mutex::Autolock _l(mLock);
- sp<LayerBaseClient> lbc;
- wp<LayerBaseClient> layer(mLayers.valueFor(i));
- if (layer != 0) {
- lbc = layer.promote();
- ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
- }
- return lbc;
+ return l->sequence - r->sequence;
}
+// ---------------------------------------------------------------------------
-status_t Client::onTransact(
- uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
-{
- // these must be checked
- IPCThreadState* ipc = IPCThreadState::self();
- const int pid = ipc->getCallingPid();
- const int uid = ipc->getCallingUid();
- const int self_pid = getpid();
- if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
- // we're called from a different process, do the real check
- if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
- {
- ALOGE("Permission Denial: "
- "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
- return PERMISSION_DENIED;
- }
- }
- return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
+SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() : id(-1) {
}
-
-sp<ISurface> Client::createSurface(
- ISurfaceComposerClient::surface_data_t* params,
- const String8& name,
- DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
- uint32_t flags)
-{
- /*
- * createSurface must be called from the GL thread so that it can
- * have access to the GL context.
- */
-
- class MessageCreateSurface : public MessageBase {
- sp<ISurface> result;
- SurfaceFlinger* flinger;
- ISurfaceComposerClient::surface_data_t* params;
- Client* client;
- const String8& name;
- DisplayID display;
- uint32_t w, h;
- PixelFormat format;
- uint32_t flags;
- public:
- MessageCreateSurface(SurfaceFlinger* flinger,
- ISurfaceComposerClient::surface_data_t* params,
- const String8& name, Client* client,
- DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
- uint32_t flags)
- : flinger(flinger), params(params), client(client), name(name),
- display(display), w(w), h(h), format(format), flags(flags)
- {
- }
- sp<ISurface> getResult() const { return result; }
- virtual bool handler() {
- result = flinger->createSurface(params, name, client,
- display, w, h, format, flags);
- return true;
- }
- };
-
- sp<MessageBase> msg = new MessageCreateSurface(mFlinger.get(),
- params, name, this, display, w, h, format, flags);
- mFlinger->postMessageSync(msg);
- return static_cast<MessageCreateSurface*>( msg.get() )->getResult();
-}
-status_t Client::destroySurface(SurfaceID sid) {
- return mFlinger->removeSurface(this, sid);
+SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(int32_t id)
+ : id(id), layerStack(0), orientation(0) {
}
// ---------------------------------------------------------------------------
@@ -2658,126 +2442,4 @@ sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h
// ---------------------------------------------------------------------------
-GraphicPlane::GraphicPlane()
- : mHw(0)
-{
-}
-
-GraphicPlane::~GraphicPlane() {
- delete mHw;
-}
-
-bool GraphicPlane::initialized() const {
- return mHw ? true : false;
-}
-
-int GraphicPlane::getWidth() const {
- return mWidth;
-}
-
-int GraphicPlane::getHeight() const {
- return mHeight;
-}
-
-void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
-{
- mHw = hw;
-
- // initialize the display orientation transform.
- // it's a constant that should come from the display driver.
- int displayOrientation = ISurfaceComposer::eOrientationDefault;
- char property[PROPERTY_VALUE_MAX];
- if (property_get("ro.sf.hwrotation", property, NULL) > 0) {
- //displayOrientation
- switch (atoi(property)) {
- case 90:
- displayOrientation = ISurfaceComposer::eOrientation90;
- break;
- case 270:
- displayOrientation = ISurfaceComposer::eOrientation270;
- break;
- }
- }
-
- const float w = hw->getWidth();
- const float h = hw->getHeight();
- GraphicPlane::orientationToTransfrom(displayOrientation, w, h,
- &mDisplayTransform);
- if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) {
- mDisplayWidth = h;
- mDisplayHeight = w;
- } else {
- mDisplayWidth = w;
- mDisplayHeight = h;
- }
-
- setOrientation(ISurfaceComposer::eOrientationDefault);
-}
-
-status_t GraphicPlane::orientationToTransfrom(
- int orientation, int w, int h, Transform* tr)
-{
- uint32_t flags = 0;
- switch (orientation) {
- case ISurfaceComposer::eOrientationDefault:
- flags = Transform::ROT_0;
- break;
- case ISurfaceComposer::eOrientation90:
- flags = Transform::ROT_90;
- break;
- case ISurfaceComposer::eOrientation180:
- flags = Transform::ROT_180;
- break;
- case ISurfaceComposer::eOrientation270:
- flags = Transform::ROT_270;
- break;
- default:
- return BAD_VALUE;
- }
- tr->set(flags, w, h);
- return NO_ERROR;
-}
-
-status_t GraphicPlane::setOrientation(int orientation)
-{
- // If the rotation can be handled in hardware, this is where
- // the magic should happen.
-
- const DisplayHardware& hw(displayHardware());
- const float w = mDisplayWidth;
- const float h = mDisplayHeight;
- mWidth = int(w);
- mHeight = int(h);
-
- Transform orientationTransform;
- GraphicPlane::orientationToTransfrom(orientation, w, h,
- &orientationTransform);
- if (orientation & ISurfaceComposer::eOrientationSwapMask) {
- mWidth = int(h);
- mHeight = int(w);
- }
-
- mOrientation = orientation;
- mGlobalTransform = mDisplayTransform * orientationTransform;
- return NO_ERROR;
-}
-
-const DisplayHardware& GraphicPlane::displayHardware() const {
- return *mHw;
-}
-
-DisplayHardware& GraphicPlane::editDisplayHardware() {
- return *mHw;
-}
-
-const Transform& GraphicPlane::transform() const {
- return mGlobalTransform;
-}
-
-EGLDisplay GraphicPlane::getEGLDisplay() const {
- return mHw->getEGLDisplay();
-}
-
-// ---------------------------------------------------------------------------
-
}; // namespace android