大家使用
SurfaceTexture.OnFrameAvailableListener mOnFrameAvailableListener = (surfaceTexture -> {
}
对应---
/**
* Construct a new SurfaceTexture to stream images to a given OpenGL texture.
* <p>
* In single buffered mode the application is responsible for serializing access to the image
* content buffer. Each time the image content is to be updated, the
* {@link #releaseTexImage()} method must be called before the image content producer takes
* ownership of the buffer. For example, when producing image content with the NDK
* ANativeWindow_lock and ANativeWindow_unlockAndPost functions, {@link #releaseTexImage()}
* must be called before each ANativeWindow_lock, or that call will fail. When producing
* image content with OpenGL ES, {@link #releaseTexImage()} must be called before the first
* OpenGL ES function call each frame.
* <p>
* Unlike {@link #SurfaceTexture(int, boolean)}, which takes an OpenGL texture object name,
* this constructor creates the SurfaceTexture in detached mode. A texture name must be passed
* in using {@link #attachToGLContext} before calling {@link #releaseTexImage()} and producing
* image content using OpenGL ES.
*
* @param singleBufferMode whether the SurfaceTexture will be in single buffered mode.
*
* @throws android.view.Surface.OutOfResourcesException If the SurfaceTexture cannot be created.
*/
public SurfaceTexture(boolean singleBufferMode) {
mCreatorLooper = Looper.myLooper();
mIsSingleBuffered = singleBufferMode;
nativeInit(true, 0, singleBufferMode, new WeakReference<SurfaceTexture>(this));
}
private native void nativeInit(boolean isDetached, int texName,
boolean singleBufferMode, WeakReference<SurfaceTexture> weakSelf)
throws Surface.OutOfResourcesException;
为什么要 new WeakReference<SurfaceTexture>(this) ???
全局搜索 nayiveInit 搜索到映射方法
/frameworks/base/core/jni/android_graphics_SurfaceTexture.cpp (映射第一行)
static const JNINativeMethod gSurfaceTextureMethods[] = {
{"nativeInit", "(ZIZLjava/lang/ref/WeakReference;)V", (void*)SurfaceTexture_init},
{"nativeFinalize", "()V", (void*)SurfaceTexture_finalize},
{"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize},
{"nativeUpdateTexImage", "()V", (void*)SurfaceTexture_updateTexImage},
{"nativeReleaseTexImage", "()V", (void*)SurfaceTexture_releaseTexImage},
{"nativeDetachFromGLContext", "()I", (void*)SurfaceTexture_detachFromGLContext},
{"nativeAttachToGLContext", "(I)I", (void*)SurfaceTexture_attachToGLContext},
{"nativeGetTransformMatrix", "([F)V", (void*)SurfaceTexture_getTransformMatrix},
{"nativeGetTimestamp", "()J", (void*)SurfaceTexture_getTimestamp},
{"nativeGetDataSpace", "()I", (void*)SurfaceTexture_getDataSpace},
{"nativeRelease", "()V", (void*)SurfaceTexture_release},
{"nativeIsReleased", "()Z", (void*)SurfaceTexture_isReleased},
};
----------------------------------------------------------------------------------
static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jboolean isDetached,
jint texName, jboolean singleBufferMode, jobject weakThiz)
{
sp<IGraphicBufferProducer> producer; // 创建生产者
sp<IGraphicBufferConsumer> consumer; // 创建消费者
BufferQueue::createBufferQueue(&producer, &consumer);//创建BufferQueue
if (singleBufferMode) {
consumer->setMaxBufferCount(1); //如果是单buffer模式设置 count=1
}
//定义SurfaceTexture
sp<SurfaceTexture> surfaceTexture;
//创建SurfaceTexture =
if (isDetached) {
surfaceTexture = new SurfaceTexture(consumer, GL_TEXTURE_EXTERNAL_OES,
true, !singleBufferMode);
} else {
surfaceTexture = new SurfaceTexture(consumer, texName,
GL_TEXTURE_EXTERNAL_OES, true, !singleBufferMode);
}
if (surfaceTexture == 0) {
jniThrowException(env, OutOfResourcesException,
"Unable to create native SurfaceTexture");
return;
}
surfaceTexture->setName(String8::format("SurfaceTexture-%d-%d-%d",
(isDetached ? 0 : texName),
getpid(),
createProcessUniqueId()));
// If the current context is protected, inform the producer.
consumer->setConsumerIsProtected(isProtectedContext());
//将Native SurfaceTexture指针(long)设给 Java SurfaceTexture的mSurfaceTexture
SurfaceTexture_setSurfaceTexture(env, thiz, surfaceTexture);
//将Native producer指针(long)设给 Java SurfaceTexture的mProducer
SurfaceTexture_setProducer(env, thiz, producer);
//获取java SurfaceTexture 对象
jclass clazz = env->GetObjectClass(thiz);
//空指针判断-初始化不成功
if (clazz == NULL) {
jniThrowRuntimeException(env,
"Can't find android/graphics/SurfaceTexture");
return;
}
// weakThiz表示Java层SurfaceTexture对象的弱引用,JNISurfaceTextureContext是JNI封装类,负责回调Java层SurfaceTexture.postEventFromNative方法
sp<JNISurfaceTextureContext> ctx(new JNISurfaceTextureContext(env, weakThiz,
clazz));
//
surfaceTexture->setFrameAvailableListener(ctx);
SurfaceTexture_setFrameAvailableListener(env, thiz, ctx);
}
static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz,
const sp<SurfaceTexture>& surfaceTexture)
{
SurfaceTexture* const p =
(SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
if (surfaceTexture.get()) {
surfaceTexture->incStrong((void*)SurfaceTexture_setSurfaceTexture);
}
if (p) {
p->decStrong((void*)SurfaceTexture_setSurfaceTexture);
}
//将Native SurfaceTexture指针(long)设给 Java SurfaceTexture 的 mSurfaceTexture对象
env->SetLongField(thiz, fields.surfaceTexture, (jlong)surfaceTexture.get());
}
static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
jobject thiz, sp<SurfaceTexture::FrameAvailableListener> listener)
{
SurfaceTexture::FrameAvailableListener* const p =
(SurfaceTexture::FrameAvailableListener*)
env->GetLongField(thiz, fields.frameAvailableListener);
if (listener.get()) {
listener->incStrong((void*)SurfaceTexture_setSurfaceTexture);
}
if (p) {
p->decStrong((void*)SurfaceTexture_setSurfaceTexture);
}
//将Native listener指针(long)设给 Java SurfaceTexture的mFrameAvailableListener
env->SetLongField(thiz, fields.frameAvailableListener, (jlong)listener.get());
}