Bug 1116473 - [1.1] Handle AndroidSurfaceTexture mapping in thread-safe class. r=snorp

This commit is contained in:
Eugen Sawin
2016-02-18 23:55:15 +01:00
parent 2ee257f416
commit dbcc7f2e9c
2 changed files with 55 additions and 22 deletions

View File

@@ -6,7 +6,6 @@
#ifdef MOZ_WIDGET_ANDROID #ifdef MOZ_WIDGET_ANDROID
#include <set>
#include <map> #include <map>
#include <android/log.h> #include <android/log.h>
#include "AndroidSurfaceTexture.h" #include "AndroidSurfaceTexture.h"
@@ -27,9 +26,57 @@ using namespace mozilla::widget::sdk;
namespace mozilla { namespace mozilla {
namespace gl { namespace gl {
// UGH // Maintains a mapping between AndroidSurfaceTexture instances and their
static std::map<int, AndroidSurfaceTexture*> sInstances; // unique numerical IDs. [thread-safe]
static int sNextID = 0; class InstanceMap
{
typedef AndroidSurfaceTexture* InstancePtr;
typedef std::map<int, InstancePtr> MapType;
public:
InstanceMap()
: mNextId(0)
, mMonitor("AndroidSurfaceTexture::InstanceMap::mMonitor")
{}
int Add(InstancePtr aInstance)
{
MonitorAutoLock lock(mMonitor);
mInstances.insert({++mNextId, aInstance});
return mNextId;
}
void Remove(int aId)
{
MonitorAutoLock lock(mMonitor);
mInstances.erase(aId);
}
InstancePtr Get(int aId) const
{
MonitorAutoLock lock(mMonitor);
auto it = mInstances.find(aId);
if (it == mInstances.end()) {
return nullptr;
}
return it->second;
}
private:
MapType mInstances;
int mNextId;
mutable Monitor mMonitor;
};
static InstanceMap sInstances;
AndroidSurfaceTexture*
AndroidSurfaceTexture::Find(int aId)
{
return sInstances.Get(aId);
}
static bool static bool
IsSTSupported() IsSTSupported()
@@ -59,19 +106,6 @@ AndroidSurfaceTexture::Create(GLContext* aContext, GLuint aTexture)
return st.forget(); return st.forget();
} }
AndroidSurfaceTexture*
AndroidSurfaceTexture::Find(int id)
{
std::map<int, AndroidSurfaceTexture*>::iterator it;
it = sInstances.find(id);
if (it == sInstances.end())
return nullptr;
return it->second;
}
nsresult nsresult
AndroidSurfaceTexture::Attach(GLContext* aContext, PRIntervalTime aTimeout) AndroidSurfaceTexture::Attach(GLContext* aContext, PRIntervalTime aTimeout)
{ {
@@ -170,8 +204,7 @@ AndroidSurfaceTexture::Init(GLContext* aContext, GLuint aTexture)
mSurface.Get()); mSurface.Get());
MOZ_ASSERT(mNativeWindow, "Failed to create native window from surface"); MOZ_ASSERT(mNativeWindow, "Failed to create native window from surface");
mID = ++sNextID; mID = sInstances.Add(this);
sInstances.insert(std::pair<int, AndroidSurfaceTexture*>(mID, this));
return true; return true;
} }
@@ -180,15 +213,15 @@ AndroidSurfaceTexture::AndroidSurfaceTexture()
: mTexture(0) : mTexture(0)
, mSurfaceTexture() , mSurfaceTexture()
, mSurface() , mSurface()
, mMonitor("AndroidSurfaceTexture::mContextMonitor")
, mAttachedContext(nullptr) , mAttachedContext(nullptr)
, mCanDetach(false) , mCanDetach(false)
, mMonitor("AndroidSurfaceTexture::mContextMonitor")
{ {
} }
AndroidSurfaceTexture::~AndroidSurfaceTexture() AndroidSurfaceTexture::~AndroidSurfaceTexture()
{ {
sInstances.erase(mID); sInstances.Remove(mID);
mFrameAvailableCallback = nullptr; mFrameAvailableCallback = nullptr;

View File

@@ -44,7 +44,7 @@ public:
// Android Jelly Bean. // Android Jelly Bean.
static already_AddRefed<AndroidSurfaceTexture> Create(); static already_AddRefed<AndroidSurfaceTexture> Create();
static AndroidSurfaceTexture* Find(int id); static AndroidSurfaceTexture* Find(int aId);
// If we are on Jelly Bean, the SurfaceTexture can be detached and reattached // If we are on Jelly Bean, the SurfaceTexture can be detached and reattached
// to allow consumption from different GLContexts. It is recommended to only // to allow consumption from different GLContexts. It is recommended to only