2012-07-20 12:20:51 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:set ts=2 sts=2 sw=2 et cin:
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include "nsSurfaceTexture.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "AndroidBridge.h"
|
2013-09-24 13:45:13 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
// UGH
|
|
|
|
static std::map<int, nsSurfaceTexture*> sInstances;
|
|
|
|
static int sNextID = 0;
|
|
|
|
|
|
|
|
static class JNIFunctions {
|
|
|
|
public:
|
|
|
|
|
|
|
|
JNIFunctions() : mInitialized(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EnsureInitialized()
|
|
|
|
{
|
|
|
|
if (mInitialized)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AutoLocalJNIFrame jniFrame(env);
|
|
|
|
|
|
|
|
jSurfaceTextureClass = (jclass)env->NewGlobalRef(env->FindClass("android/graphics/SurfaceTexture"));
|
|
|
|
jSurfaceTexture_Ctor = env->GetMethodID(jSurfaceTextureClass, "<init>", "(I)V");
|
|
|
|
jSurfaceTexture_updateTexImage = env->GetMethodID(jSurfaceTextureClass, "updateTexImage", "()V");
|
|
|
|
jSurfaceTexture_getTransformMatrix = env->GetMethodID(jSurfaceTextureClass, "getTransformMatrix", "([F)V");
|
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
jobject CreateSurfaceTexture(GLuint aTexture)
|
|
|
|
{
|
|
|
|
if (!EnsureInitialized())
|
2013-07-31 08:44:31 -07:00
|
|
|
return nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
2013-07-31 08:44:31 -07:00
|
|
|
return nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
AutoLocalJNIFrame jniFrame(env);
|
|
|
|
|
|
|
|
return env->NewGlobalRef(env->NewObject(jSurfaceTextureClass, jSurfaceTexture_Ctor, (int) aTexture));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseSurfaceTexture(jobject aSurfaceTexture)
|
|
|
|
{
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return;
|
|
|
|
|
|
|
|
env->DeleteGlobalRef(aSurfaceTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTexImage(jobject aSurfaceTexture)
|
|
|
|
{
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AutoLocalJNIFrame jniFrame(env);
|
|
|
|
env->CallObjectMethod(aSurfaceTexture, jSurfaceTexture_updateTexImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetTransformMatrix(jobject aSurfaceTexture, gfx3DMatrix& aMatrix)
|
|
|
|
{
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AutoLocalJNIFrame jniFrame(env);
|
|
|
|
|
|
|
|
jfloatArray jarray = env->NewFloatArray(16);
|
|
|
|
env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_getTransformMatrix, jarray);
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
jfloat* array = env->GetFloatArrayElements(jarray, nullptr);
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
aMatrix._11 = array[0];
|
|
|
|
aMatrix._12 = array[1];
|
|
|
|
aMatrix._13 = array[2];
|
|
|
|
aMatrix._14 = array[3];
|
|
|
|
|
|
|
|
aMatrix._21 = array[4];
|
|
|
|
aMatrix._22 = array[5];
|
|
|
|
aMatrix._23 = array[6];
|
|
|
|
aMatrix._24 = array[7];
|
|
|
|
|
|
|
|
aMatrix._31 = array[8];
|
|
|
|
aMatrix._32 = array[9];
|
|
|
|
aMatrix._33 = array[10];
|
|
|
|
aMatrix._34 = array[11];
|
|
|
|
|
|
|
|
aMatrix._41 = array[12];
|
|
|
|
aMatrix._42 = array[13];
|
|
|
|
aMatrix._43 = array[14];
|
|
|
|
aMatrix._44 = array[15];
|
|
|
|
|
|
|
|
env->ReleaseFloatArrayElements(jarray, array, 0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mInitialized;
|
|
|
|
|
|
|
|
jclass jSurfaceTextureClass;
|
|
|
|
jmethodID jSurfaceTexture_Ctor;
|
|
|
|
jmethodID jSurfaceTexture_updateTexImage;
|
|
|
|
jmethodID jSurfaceTexture_getTransformMatrix;
|
|
|
|
|
|
|
|
} sJNIFunctions;
|
|
|
|
|
|
|
|
nsSurfaceTexture*
|
|
|
|
nsSurfaceTexture::Create(GLuint aTexture)
|
|
|
|
{
|
|
|
|
// Right now we only support creating this on the main thread because
|
|
|
|
// of the JNIEnv assumptions in JNIHelper and elsewhere
|
|
|
|
if (!NS_IsMainThread())
|
2013-07-31 08:44:31 -07:00
|
|
|
return nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
nsSurfaceTexture* st = new nsSurfaceTexture();
|
|
|
|
if (!st->Init(aTexture)) {
|
2013-11-27 05:03:04 -08:00
|
|
|
printf_stderr("Failed to initialize nsSurfaceTexture");
|
2012-07-20 12:20:51 -07:00
|
|
|
delete st;
|
2013-07-31 08:44:31 -07:00
|
|
|
st = nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSurfaceTexture*
|
|
|
|
nsSurfaceTexture::Find(int id)
|
|
|
|
{
|
|
|
|
std::map<int, nsSurfaceTexture*>::iterator it;
|
|
|
|
|
|
|
|
it = sInstances.find(id);
|
|
|
|
if (it == sInstances.end())
|
2013-07-31 08:44:31 -07:00
|
|
|
return nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsSurfaceTexture::Check()
|
|
|
|
{
|
|
|
|
return sJNIFunctions.EnsureInitialized();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsSurfaceTexture::Init(GLuint aTexture)
|
|
|
|
{
|
|
|
|
if (!sJNIFunctions.EnsureInitialized())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mSurfaceTexture = sJNIFunctions.CreateSurfaceTexture(aTexture);
|
|
|
|
if (!mSurfaceTexture)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mNativeWindow = AndroidBridge::Bridge()->AcquireNativeWindowFromSurfaceTexture(env, mSurfaceTexture);
|
|
|
|
|
|
|
|
mID = ++sNextID;
|
|
|
|
sInstances.insert(std::pair<int, nsSurfaceTexture*>(mID, this));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSurfaceTexture::nsSurfaceTexture()
|
2013-07-31 08:44:31 -07:00
|
|
|
: mSurfaceTexture(nullptr), mNativeWindow(nullptr)
|
2012-07-20 12:20:51 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSurfaceTexture::~nsSurfaceTexture()
|
|
|
|
{
|
|
|
|
sInstances.erase(mID);
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
mFrameAvailableCallback = nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
if (mNativeWindow) {
|
|
|
|
AndroidBridge::Bridge()->ReleaseNativeWindowForSurfaceTexture(mSurfaceTexture);
|
2013-07-31 08:44:31 -07:00
|
|
|
mNativeWindow = nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* env = GetJNIForThread();
|
|
|
|
if (!env)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mSurfaceTexture && env) {
|
2013-11-12 10:41:01 -08:00
|
|
|
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture);
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
env->DeleteGlobalRef(mSurfaceTexture);
|
2013-07-31 08:44:31 -07:00
|
|
|
mSurfaceTexture = nullptr;
|
2012-07-20 12:20:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
nsSurfaceTexture::GetNativeWindow()
|
|
|
|
{
|
|
|
|
return mNativeWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSurfaceTexture::UpdateTexImage()
|
|
|
|
{
|
|
|
|
sJNIFunctions.UpdateTexImage(mSurfaceTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsSurfaceTexture::GetTransformMatrix(gfx3DMatrix& aMatrix)
|
|
|
|
{
|
|
|
|
return sJNIFunctions.GetTransformMatrix(mSurfaceTexture, aMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSurfaceTexture::SetFrameAvailableCallback(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
if (aRunnable)
|
2013-11-12 10:41:01 -08:00
|
|
|
GeckoAppShell::RegisterSurfaceTextureFrameListener(mSurfaceTexture, mID);
|
2012-07-20 12:20:51 -07:00
|
|
|
else
|
2013-11-12 10:41:01 -08:00
|
|
|
GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture);
|
2012-07-20 12:20:51 -07:00
|
|
|
|
|
|
|
mFrameAvailableCallback = aRunnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSurfaceTexture::NotifyFrameAvailable()
|
|
|
|
{
|
|
|
|
if (mFrameAvailableCallback) {
|
|
|
|
// Proxy to main thread if we aren't on it
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// Proxy to main thread
|
|
|
|
nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &nsSurfaceTexture::NotifyFrameAvailable);
|
|
|
|
NS_DispatchToCurrentThread(event);
|
|
|
|
} else {
|
|
|
|
mFrameAvailableCallback->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 18:02:14 -08:00
|
|
|
#endif // MOZ_WIDGET_ANDROID
|