blob: 1990828fa03a9b8c00f731a0c59fc64bfb920f5b [file] [log] [blame]
Magnus Jedvert6781ea42015-10-02 13:56:04 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Magnus Jedvert6781ea42015-10-02 13:56:04 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
Magnus Jedvert6781ea42015-10-02 13:56:04 +02009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/java/jni/native_handle_impl.h"
Magnus Jedvert6781ea42015-10-02 13:56:04 +020012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/java/jni/jni_helpers.h"
nissec490e012015-12-10 06:23:33 -080016#include "webrtc/base/bind.h"
Magnus Jedvert6781ea42015-10-02 13:56:04 +020017#include "webrtc/base/checks.h"
perkj14f41442015-11-30 22:15:45 -080018#include "webrtc/base/keep_ref_until_done.h"
kjellandera96e2d72016-02-04 23:52:28 -080019#include "webrtc/base/logging.h"
perkj14f41442015-11-30 22:15:45 -080020#include "webrtc/base/scoped_ref_ptr.h"
Pera3c20bb2015-11-26 13:41:44 +010021
Pera3c20bb2015-11-26 13:41:44 +010022using webrtc::NativeHandleBuffer;
Magnus Jedvert6781ea42015-10-02 13:56:04 +020023
Per71f5a9a2015-12-11 09:32:37 +010024namespace {
25
26void RotateMatrix(float a[16], webrtc::VideoRotation rotation) {
27 // Texture coordinates are in the range 0 to 1. The transformation of the last
28 // row in each rotation matrix is needed for proper translation, e.g, to
29 // mirror x, we don't replace x by -x, but by 1-x.
30 switch (rotation) {
31 case webrtc::kVideoRotation_0:
32 break;
33 case webrtc::kVideoRotation_90: {
34 const float ROTATE_90[16] =
35 { a[4], a[5], a[6], a[7],
36 -a[0], -a[1], -a[2], -a[3],
37 a[8], a[9], a[10], a[11],
38 a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]};
39 memcpy(a, ROTATE_90, sizeof(ROTATE_90));
40 } break;
41 case webrtc::kVideoRotation_180: {
42 const float ROTATE_180[16] =
43 { -a[0], -a[1], -a[2], -a[3],
44 -a[4], -a[5], -a[6], -a[7],
45 a[8], a[9], a[10], a[11],
46 a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14],
47 a[3] + a[11]+ a[15]};
48 memcpy(a, ROTATE_180, sizeof(ROTATE_180));
49 }
50 break;
51 case webrtc::kVideoRotation_270: {
52 const float ROTATE_270[16] =
53 { -a[4], -a[5], -a[6], -a[7],
54 a[0], a[1], a[2], a[3],
55 a[8], a[9], a[10], a[11],
56 a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]};
57 memcpy(a, ROTATE_270, sizeof(ROTATE_270));
58 } break;
59 }
60}
61
62} // anonymouse namespace
63
Magnus Jedvert6781ea42015-10-02 13:56:04 +020064namespace webrtc_jni {
65
nissec490e012015-12-10 06:23:33 -080066// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
67static const int kBufferAlignment = 64;
68
Per488e75f2015-11-19 10:43:36 +010069NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
70 jint j_oes_texture_id,
71 jfloatArray j_transform_matrix)
nissec490e012015-12-10 06:23:33 -080072 : oes_texture_id(j_oes_texture_id) {
Magnus Jedvert91b348c2015-10-07 22:57:06 +020073 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix));
74 jfloat* transform_matrix_ptr =
75 jni->GetFloatArrayElements(j_transform_matrix, nullptr);
76 for (int i = 0; i < 16; ++i) {
77 sampling_matrix[i] = transform_matrix_ptr[i];
78 }
79 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0);
Magnus Jedvert6781ea42015-10-02 13:56:04 +020080}
81
magjed52a30e32015-10-12 06:53:20 -070082AndroidTextureBuffer::AndroidTextureBuffer(
83 int width,
84 int height,
Per488e75f2015-11-19 10:43:36 +010085 const NativeHandleImpl& native_handle,
nissec490e012015-12-10 06:23:33 -080086 jobject surface_texture_helper,
magjed52a30e32015-10-12 06:53:20 -070087 const rtc::Callback0<void>& no_longer_used)
88 : webrtc::NativeHandleBuffer(&native_handle_, width, height),
89 native_handle_(native_handle),
nissec490e012015-12-10 06:23:33 -080090 surface_texture_helper_(surface_texture_helper),
magjed52a30e32015-10-12 06:53:20 -070091 no_longer_used_cb_(no_longer_used) {}
92
93AndroidTextureBuffer::~AndroidTextureBuffer() {
94 no_longer_used_cb_();
95}
96
97rtc::scoped_refptr<webrtc::VideoFrameBuffer>
98AndroidTextureBuffer::NativeToI420Buffer() {
nissec490e012015-12-10 06:23:33 -080099 int uv_width = (width()+7) / 8;
100 int stride = 8 * uv_width;
101 int uv_height = (height()+1)/2;
102 size_t size = stride * (height() + uv_height);
103 // The data is owned by the frame, and the normal case is that the
104 // data is deleted by the frame's destructor callback.
105 //
106 // TODO(nisse): Use an I420BufferPool. We then need to extend that
107 // class, and I420Buffer, to support our memory layout.
kwibergd1fe2812016-04-27 06:47:29 -0700108 std::unique_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data(
nissec490e012015-12-10 06:23:33 -0800109 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment)));
110 // See SurfaceTextureHelper.java for the required layout.
111 uint8_t* y_data = yuv_data.get();
112 uint8_t* u_data = y_data + height() * stride;
113 uint8_t* v_data = u_data + stride/2;
114
115 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy =
116 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
117 width(), height(),
118 y_data, stride,
119 u_data, stride,
120 v_data, stride,
121 rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
122
123 JNIEnv* jni = AttachCurrentThreadIfNeeded();
124 ScopedLocalRefFrame local_ref_frame(jni);
125
126 jmethodID transform_mid = GetMethodID(
127 jni,
128 GetObjectClass(jni, surface_texture_helper_),
129 "textureToYUV",
130 "(Ljava/nio/ByteBuffer;IIII[F)V");
131
132 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size);
133
134 // TODO(nisse): Keep java transform matrix around.
135 jfloatArray sampling_matrix = jni->NewFloatArray(16);
136 jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
137 native_handle_.sampling_matrix);
138
139 jni->CallVoidMethod(surface_texture_helper_,
140 transform_mid,
141 byte_buffer, width(), height(), stride,
142 native_handle_.oes_texture_id, sampling_matrix);
143 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception";
144
145 return copy;
magjed52a30e32015-10-12 06:53:20 -0700146}
147
Per71f5a9a2015-12-11 09:32:37 +0100148rtc::scoped_refptr<AndroidTextureBuffer>
149AndroidTextureBuffer::ScaleAndRotate(int dst_widht,
150 int dst_height,
151 webrtc::VideoRotation rotation) {
152 if (width() == dst_widht && height() == dst_height &&
153 rotation == webrtc::kVideoRotation_0) {
154 return this;
155 }
156 int rotated_width = (rotation % 180 == 0) ? dst_widht : dst_height;
157 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_widht;
Pera3c20bb2015-11-26 13:41:44 +0100158
159 // Here we use Bind magic to add a reference count to |this| until the newly
Per71f5a9a2015-12-11 09:32:37 +0100160 // created AndroidTextureBuffer is destructed
161 rtc::scoped_refptr<AndroidTextureBuffer> buffer(
162 new rtc::RefCountedObject<AndroidTextureBuffer>(
163 rotated_width, rotated_height, native_handle_,
164 surface_texture_helper_, rtc::KeepRefUntilDone(this)));
165
166 RotateMatrix(buffer->native_handle_.sampling_matrix, rotation);
167 return buffer;
Pera3c20bb2015-11-26 13:41:44 +0100168}
169
Magnus Jedvert6781ea42015-10-02 13:56:04 +0200170} // namespace webrtc_jni