From cb9732a951d20cacb7ebe2dab132b5738226b1b6 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Tue, 3 Apr 2012 17:48:03 -0700 Subject: [PATCH] refactor / simplify EventThread Change-Id: I3981c6fba93b7b985174b2a7045e24db2c0b4428 --- services/surfaceflinger/Android.mk | 1 - .../surfaceflinger/DisplayEventConnection.cpp | 71 ----------- .../surfaceflinger/DisplayEventConnection.h | 62 ---------- services/surfaceflinger/EventThread.cpp | 113 +++++++++++------- services/surfaceflinger/EventThread.h | 63 +++++----- services/surfaceflinger/SurfaceFlinger.cpp | 1 - services/surfaceflinger/SurfaceFlinger.h | 5 +- 7 files changed, 102 insertions(+), 214 deletions(-) delete mode 100644 services/surfaceflinger/DisplayEventConnection.cpp delete mode 100644 services/surfaceflinger/DisplayEventConnection.h diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk index 702b52bd0..f93db75c9 100644 --- a/services/surfaceflinger/Android.mk +++ b/services/surfaceflinger/Android.mk @@ -11,7 +11,6 @@ LOCAL_SRC_FILES:= \ DisplayHardware/DisplayHardwareBase.cpp \ DisplayHardware/HWComposer.cpp \ DisplayHardware/VSyncBarrier.cpp \ - DisplayEventConnection.cpp \ GLExtensions.cpp \ MessageQueue.cpp \ SurfaceFlinger.cpp \ diff --git a/services/surfaceflinger/DisplayEventConnection.cpp b/services/surfaceflinger/DisplayEventConnection.cpp deleted file mode 100644 index 67381ef05..000000000 --- a/services/surfaceflinger/DisplayEventConnection.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -#include -#include -#include - -#include - -#include "SurfaceFlinger.h" -#include "DisplayEventConnection.h" -#include "EventThread.h" - -// --------------------------------------------------------------------------- - -namespace android { - -// --------------------------------------------------------------------------- - -DisplayEventConnection::DisplayEventConnection( - const sp& eventThread) - : mEventThread(eventThread), mChannel(new BitTube()) -{ -} - -DisplayEventConnection::~DisplayEventConnection() { - mEventThread->unregisterDisplayEventConnection(this); -} - -void DisplayEventConnection::onFirstRef() { - // NOTE: mEventThread doesn't hold a strong reference on us - mEventThread->registerDisplayEventConnection(this); -} - -sp DisplayEventConnection::getDataChannel() const { - return mChannel; -} - -void DisplayEventConnection::setVsyncRate(uint32_t count) { - mEventThread->setVsyncRate(count, this); -} - -void DisplayEventConnection::requestNextVsync() { - mEventThread->requestNextVsync(this); -} - -status_t DisplayEventConnection::postEvent( - const DisplayEventReceiver::Event& event) { - ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1); - return size < 0 ? status_t(size) : status_t(NO_ERROR); -} - -// --------------------------------------------------------------------------- - -}; // namespace android diff --git a/services/surfaceflinger/DisplayEventConnection.h b/services/surfaceflinger/DisplayEventConnection.h deleted file mode 100644 index cc3ee3659..000000000 --- a/services/surfaceflinger/DisplayEventConnection.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ANDROID_SURFACE_FLINGER_DISPLAY_EVENT_CONNECTION_H -#define ANDROID_SURFACE_FLINGER_DISPLAY_EVENT_CONNECTION_H - -#include -#include - -#include - -#include -#include - -// --------------------------------------------------------------------------- - -namespace android { - -// --------------------------------------------------------------------------- - -class BitTube; -class EventThread; - -// --------------------------------------------------------------------------- - -class DisplayEventConnection : public BnDisplayEventConnection { -public: - DisplayEventConnection(const sp& flinger); - - status_t postEvent(const DisplayEventReceiver::Event& event); - -private: - virtual ~DisplayEventConnection(); - virtual void onFirstRef(); - virtual sp getDataChannel() const; - virtual void setVsyncRate(uint32_t count); - virtual void requestNextVsync(); // asynchronous - - sp const mEventThread; - sp const mChannel; -}; - -// --------------------------------------------------------------------------- - -}; // namespace android - -// --------------------------------------------------------------------------- - -#endif /* ANDROID_SURFACE_FLINGER_DISPLAY_EVENT_CONNECTION_H */ diff --git a/services/surfaceflinger/EventThread.cpp b/services/surfaceflinger/EventThread.cpp index 3c045d71e..97afb5fa3 100644 --- a/services/surfaceflinger/EventThread.cpp +++ b/services/surfaceflinger/EventThread.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -26,7 +27,6 @@ #include #include "DisplayHardware/DisplayHardware.h" -#include "DisplayEventConnection.h" #include "EventThread.h" #include "SurfaceFlinger.h" @@ -48,8 +48,8 @@ void EventThread::onFirstRef() { run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); } -sp EventThread::createEventConnection() const { - return new DisplayEventConnection(const_cast(this)); +sp EventThread::createEventConnection() const { + return new Connection(const_cast(this)); } nsecs_t EventThread::getLastVSyncTimestamp() const { @@ -63,56 +63,44 @@ nsecs_t EventThread::getVSyncPeriod() const { } status_t EventThread::registerDisplayEventConnection( - const sp& connection) { + const sp& connection) { Mutex::Autolock _l(mLock); - ConnectionInfo info; - mDisplayEventConnections.add(connection, info); + mDisplayEventConnections.add(connection); mCondition.signal(); return NO_ERROR; } status_t EventThread::unregisterDisplayEventConnection( - const wp& connection) { + const wp& connection) { Mutex::Autolock _l(mLock); - mDisplayEventConnections.removeItem(connection); + mDisplayEventConnections.remove(connection); mCondition.signal(); return NO_ERROR; } void EventThread::removeDisplayEventConnection( - const wp& connection) { + const wp& connection) { Mutex::Autolock _l(mLock); - mDisplayEventConnections.removeItem(connection); -} - -EventThread::ConnectionInfo* EventThread::getConnectionInfoLocked( - const wp& connection) { - ssize_t index = mDisplayEventConnections.indexOfKey(connection); - if (index < 0) return NULL; - return &mDisplayEventConnections.editValueAt(index); + mDisplayEventConnections.remove(connection); } void EventThread::setVsyncRate(uint32_t count, - const wp& connection) { + const sp& connection) { if (int32_t(count) >= 0) { // server must protect against bad params Mutex::Autolock _l(mLock); - ConnectionInfo* info = getConnectionInfoLocked(connection); - if (info) { - const int32_t new_count = (count == 0) ? -1 : count; - if (info->count != new_count) { - info->count = new_count; - mCondition.signal(); - } + const int32_t new_count = (count == 0) ? -1 : count; + if (connection->count != new_count) { + connection->count = new_count; + mCondition.signal(); } } } void EventThread::requestNextVsync( - const wp& connection) { + const sp& connection) { Mutex::Autolock _l(mLock); - ConnectionInfo* info = getConnectionInfoLocked(connection); - if (info && info->count < 0) { - info->count = 0; + if (connection->count < 0) { + connection->count = 0; mCondition.signal(); } } @@ -121,7 +109,7 @@ bool EventThread::threadLoop() { nsecs_t timestamp; DisplayEventReceiver::Event vsync; - Vector< wp > displayEventConnections; + Vector< wp > displayEventConnections; { // scope for the lock Mutex::Autolock _l(mLock); @@ -131,9 +119,9 @@ bool EventThread::threadLoop() { bool waitForNextVsync = false; size_t count = mDisplayEventConnections.size(); for (size_t i=0 ; i= 0) { + sp connection = + mDisplayEventConnections.itemAt(i).promote(); + if (connection!=0 && connection->count >= 0) { // at least one continuous mode or active one-shot event waitForNextVsync = true; break; @@ -158,24 +146,26 @@ bool EventThread::threadLoop() { const size_t count = mDisplayEventConnections.size(); for (size_t i=0 ; i= 1) { - if (info.count==1 || (mDeliveredEvents % info.count) == 0) { + sp connection = + mDisplayEventConnections.itemAt(i).promote(); + if (connection == 0) + continue; + + const int32_t count = connection->count; + if (count >= 1) { + if (count==1 || (mDeliveredEvents % count) == 0) { // continuous event, and time to report it reportVsync = true; } - } else if (info.count >= -1) { - ConnectionInfo& info( - mDisplayEventConnections.editValueAt(i)); - if (info.count == 0) { + } else if (count >= -1) { + if (count == 0) { // fired this time around reportVsync = true; } - info.count--; + connection->count--; } if (reportVsync) { - displayEventConnections.add(mDisplayEventConnections.keyAt(i)); + displayEventConnections.add(connection); } } } while (!displayEventConnections.size()); @@ -188,7 +178,7 @@ bool EventThread::threadLoop() { const size_t count = displayEventConnections.size(); for (size_t i=0 ; i conn(displayEventConnections[i].promote()); + sp conn(displayEventConnections[i].promote()); // make sure the connection didn't die if (conn != NULL) { status_t err = conn->postEvent(vsync); @@ -232,4 +222,39 @@ void EventThread::dump(String8& result, char* buffer, size_t SIZE) const { // --------------------------------------------------------------------------- +EventThread::Connection::Connection( + const sp& eventThread) + : count(-1), mEventThread(eventThread), mChannel(new BitTube()) +{ +} + +EventThread::Connection::~Connection() { + mEventThread->unregisterDisplayEventConnection(this); +} + +void EventThread::Connection::onFirstRef() { + // NOTE: mEventThread doesn't hold a strong reference on us + mEventThread->registerDisplayEventConnection(this); +} + +sp EventThread::Connection::getDataChannel() const { + return mChannel; +} + +void EventThread::Connection::setVsyncRate(uint32_t count) { + mEventThread->setVsyncRate(count, this); +} + +void EventThread::Connection::requestNextVsync() { + mEventThread->requestNextVsync(this); +} + +status_t EventThread::Connection::postEvent( + const DisplayEventReceiver::Event& event) { + ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1); + return size < 0 ? status_t(size) : status_t(NO_ERROR); +} + +// --------------------------------------------------------------------------- + }; // namespace android diff --git a/services/surfaceflinger/EventThread.h b/services/surfaceflinger/EventThread.h index 3a3071ef8..65217cb72 100644 --- a/services/surfaceflinger/EventThread.h +++ b/services/surfaceflinger/EventThread.h @@ -20,13 +20,12 @@ #include #include +#include #include #include #include -#include - -#include "DisplayEventConnection.h" +#include // --------------------------------------------------------------------------- @@ -36,31 +35,43 @@ namespace android { class SurfaceFlinger; class DisplayHardware; -class DisplayEventConnection; // --------------------------------------------------------------------------- class EventThread : public Thread { - friend class DisplayEventConnection; + class Connection : public BnDisplayEventConnection { + public: + Connection(const sp& eventThread); + status_t postEvent(const DisplayEventReceiver::Event& event); + + // count >= 1 : continuous event. count is the vsync rate + // count == 0 : one-shot event that has not fired + // count ==-1 : one-shot event that fired this round / disabled + // count ==-2 : one-shot event that fired the round before + int32_t count; + + private: + virtual ~Connection(); + virtual void onFirstRef(); + virtual sp getDataChannel() const; + virtual void setVsyncRate(uint32_t count); + virtual void requestNextVsync(); // asynchronous + sp const mEventThread; + sp const mChannel; + }; public: + EventThread(const sp& flinger); - sp createEventConnection() const; + sp createEventConnection() const; + status_t registerDisplayEventConnection(const sp& connection); + status_t unregisterDisplayEventConnection(const wp& connection); - status_t registerDisplayEventConnection( - const sp& connection); - - status_t unregisterDisplayEventConnection( - const wp& connection); - - void setVsyncRate(uint32_t count, - const wp& connection); - - void requestNextVsync(const wp& connection); + void setVsyncRate(uint32_t count, const sp& connection); + void requestNextVsync(const sp& connection); nsecs_t getLastVSyncTimestamp() const; - nsecs_t getVSyncPeriod() const; void dump(String8& result, char* buffer, size_t SIZE) const; @@ -70,21 +81,7 @@ private: virtual status_t readyToRun(); virtual void onFirstRef(); - struct ConnectionInfo { - ConnectionInfo() : count(-1) { } - - // count >= 1 : continuous event. count is the vsync rate - // count == 0 : one-shot event that has not fired - // count ==-1 : one-shot event that fired this round / disabled - // count ==-2 : one-shot event that fired the round before - int32_t count; - }; - - void removeDisplayEventConnection( - const wp& connection); - - ConnectionInfo* getConnectionInfoLocked( - const wp& connection); + void removeDisplayEventConnection(const wp& connection); // constants sp mFlinger; @@ -94,7 +91,7 @@ private: mutable Condition mCondition; // protected by mLock - KeyedVector< wp, ConnectionInfo > mDisplayEventConnections; + SortedVector< wp > mDisplayEventConnections; nsecs_t mLastVSyncTimestamp; // main thread only diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index 0b68aa319..7023e0f10 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -50,7 +50,6 @@ #include "clz.h" #include "DdmConnection.h" -#include "DisplayEventConnection.h" #include "EventThread.h" #include "GLExtensions.h" #include "Layer.h" diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index b50787758..6fa81db4c 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -270,8 +270,9 @@ private: }; struct State { - State() { - orientation = ISurfaceComposer::eOrientationDefault; + State() + : orientation(ISurfaceComposer::eOrientationDefault), + orientationFlags(0) { } LayerVector layersSortedByZ; uint8_t orientation;