1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/android/surface_texture_bridge.h"
#include "base/android/jni_android.h"
#include "base/logging.h"
#include "content/common/android/surface_texture_listener.h"
#include "jni/SurfaceTexture_jni.h"
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::GetClass;
using base::android::ScopedJavaLocalRef;
namespace {
bool g_jni_initialized = false;
void RegisterNativesIfNeeded(JNIEnv* env) {
if (!g_jni_initialized) {
JNI_SurfaceTexture::RegisterNativesImpl(env);
g_jni_initialized = true;
}
}
} // namespace
namespace content {
SurfaceTextureBridge::SurfaceTextureBridge(int texture_id)
: texture_id_(texture_id) {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
RegisterNativesIfNeeded(env);
ScopedJavaLocalRef<jobject> tmp(
JNI_SurfaceTexture::Java_SurfaceTexture_Constructor(
env, texture_id));
DCHECK(!tmp.is_null());
j_surface_texture_.Reset(tmp);
}
SurfaceTextureBridge::~SurfaceTextureBridge() {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
// Release the listener.
JNI_SurfaceTexture::Java_SurfaceTexture_setOnFrameAvailableListener(
env, j_surface_texture_.obj(), NULL);
// Release graphics memory.
JNI_SurfaceTexture::Java_SurfaceTexture_release(
env, j_surface_texture_.obj());
}
void SurfaceTextureBridge::SetFrameAvailableCallback(
const base::Closure& callback) {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
// Since the listener is owned by the Java SurfaceTexture object, setting
// a new listener here will release an existing one at the same time.
ScopedJavaLocalRef<jobject> j_listener(
env,
SurfaceTextureListener::CreateSurfaceTextureListener(env, callback));
DCHECK(!j_listener.is_null());
// Set it as the onFrameAvailableListener for our SurfaceTexture instance.
JNI_SurfaceTexture::Java_SurfaceTexture_setOnFrameAvailableListener(
env, j_surface_texture_.obj(), j_listener.obj());
}
void SurfaceTextureBridge::UpdateTexImage() {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
JNI_SurfaceTexture::Java_SurfaceTexture_updateTexImage(
env, j_surface_texture_.obj());
}
void SurfaceTextureBridge::GetTransformMatrix(float mtx[16]) {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
ScopedJavaLocalRef<jfloatArray> jmatrix(env, env->NewFloatArray(16));
JNI_SurfaceTexture::Java_SurfaceTexture_getTransformMatrix(
env, j_surface_texture_.obj(), jmatrix.obj());
jboolean is_copy;
jfloat* elements = env->GetFloatArrayElements(jmatrix.obj(), &is_copy);
for (int i = 0; i < 16; ++i) {
mtx[i] = static_cast<float>(elements[i]);
}
env->ReleaseFloatArrayElements(jmatrix.obj(), elements, JNI_ABORT);
}
void SurfaceTextureBridge::SetDefaultBufferSize(int width, int height) {
JNIEnv* env = AttachCurrentThread();
CHECK(env);
JNI_SurfaceTexture::Java_SurfaceTexture_setDefaultBufferSize(
env, j_surface_texture_.obj(), static_cast<jint>(width),
static_cast<jint>(height));
}
} // namespace content
|