From 5804a76ac5f9f3c311f1bbbcc5ebdc8f8568ae14 Mon Sep 17 00:00:00 2001 From: Lajos Molnar Date: Wed, 4 Mar 2015 17:00:10 -0800 Subject: stagefright: use handler instead of handler-id in AMessage This avoids locking gLooperRoster mutex on post() and deliver(). Bug: 19607784 Change-Id: If6d9d7884dbb08fc390983bda896d223803476ba --- include/media/stagefright/foundation/AHandler.h | 24 +++++++++++++++++++--- include/media/stagefright/foundation/ALooper.h | 8 ++++++-- .../media/stagefright/foundation/ALooperRoster.h | 6 ++---- include/media/stagefright/foundation/AMessage.h | 18 +++++++++++++--- 4 files changed, 44 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/media/stagefright/foundation/AHandler.h b/include/media/stagefright/foundation/AHandler.h index 41ade77..fe02a86 100644 --- a/include/media/stagefright/foundation/AHandler.h +++ b/include/media/stagefright/foundation/AHandler.h @@ -29,6 +29,7 @@ struct AMessage; struct AHandler : public RefBase { AHandler() : mID(0), + mVerboseStats(false), mMessageCounter(0) { } @@ -36,23 +37,40 @@ struct AHandler : public RefBase { return mID; } - sp looper(); + sp looper() const { + return mLooper.promote(); + } + + wp getLooper() const { + return mLooper; + } + + wp getHandler() const { + // allow getting a weak reference to a const handler + return const_cast(this); + } protected: virtual void onMessageReceived(const sp &msg) = 0; private: - friend struct ALooperRoster; + friend struct AMessage; // deliverMessage() + friend struct ALooperRoster; // setID() ALooper::handler_id mID; + wp mLooper; - void setID(ALooper::handler_id id) { + inline void setID(ALooper::handler_id id, wp looper) { mID = id; + mLooper = looper; } + bool mVerboseStats; uint32_t mMessageCounter; KeyedVector mMessages; + void deliverMessage(const sp &msg); + DISALLOW_EVIL_CONSTRUCTORS(AHandler); }; diff --git a/include/media/stagefright/foundation/ALooper.h b/include/media/stagefright/foundation/ALooper.h index 70e0c5e..150cdba 100644 --- a/include/media/stagefright/foundation/ALooper.h +++ b/include/media/stagefright/foundation/ALooper.h @@ -53,11 +53,15 @@ struct ALooper : public RefBase { static int64_t GetNowUs(); + const char *getName() const { + return mName.c_str(); + } + protected: virtual ~ALooper(); private: - friend struct ALooperRoster; + friend struct AMessage; // post() struct Event { int64_t mWhenUs; @@ -81,6 +85,6 @@ private: DISALLOW_EVIL_CONSTRUCTORS(ALooper); }; -} // namespace android +} // namespace android #endif // A_LOOPER_H_ diff --git a/include/media/stagefright/foundation/ALooperRoster.h b/include/media/stagefright/foundation/ALooperRoster.h index a0be8eb..63d52d9 100644 --- a/include/media/stagefright/foundation/ALooperRoster.h +++ b/include/media/stagefright/foundation/ALooperRoster.h @@ -33,15 +33,13 @@ struct ALooperRoster { void unregisterHandler(ALooper::handler_id handlerID); void unregisterStaleHandlers(); - status_t postMessage(const sp &msg, int64_t delayUs = 0); - void deliverMessage(const sp &msg); - status_t postAndAwaitResponse( const sp &msg, sp *response); void postReply(uint32_t replyID, const sp &reply); - sp findLooper(ALooper::handler_id handlerID); + void getHandlerAndLooper( + ALooper::handler_id handlerID, wp *handler, wp *looper); void dump(int fd, const Vector& args); diff --git a/include/media/stagefright/foundation/AMessage.h b/include/media/stagefright/foundation/AMessage.h index a9e235b..beaefdd 100644 --- a/include/media/stagefright/foundation/AMessage.h +++ b/include/media/stagefright/foundation/AMessage.h @@ -26,11 +26,14 @@ namespace android { struct ABuffer; +struct AHandler; struct AString; struct Parcel; struct AMessage : public RefBase { - AMessage(uint32_t what = 0, ALooper::handler_id target = 0); + AMessage(); + AMessage(uint32_t what, ALooper::handler_id target = 0); + AMessage(uint32_t what, const sp &handler); static sp FromParcel(const Parcel &parcel); void writeToParcel(Parcel *parcel) const; @@ -39,7 +42,7 @@ struct AMessage : public RefBase { uint32_t what() const; void setTarget(ALooper::handler_id target); - ALooper::handler_id target() const; + void setTarget(const sp &handler); void clear(); @@ -76,7 +79,7 @@ struct AMessage : public RefBase { const char *name, int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const; - void post(int64_t delayUs = 0); + status_t post(int64_t delayUs = 0); // Posts the message to its target and waits for a response (or error) // before returning. @@ -117,9 +120,16 @@ protected: virtual ~AMessage(); private: + friend struct ALooper; // deliver() + uint32_t mWhat; + + // used only for debugging ALooper::handler_id mTarget; + wp mHandler; + wp mLooper; + struct Rect { int32_t mLeft, mTop, mRight, mBottom; }; @@ -157,6 +167,8 @@ private: size_t findItemIndex(const char *name, size_t len) const; + void deliver(); + DISALLOW_EVIL_CONSTRUCTORS(AMessage); }; -- cgit v1.1