Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: webrtc/api/java/jni/native_handle_impl.cc

Issue 1973873003: Delete AndroidVideoCapturer::FrameFactory. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Move code to AndroidVideoCapturerJni. Make Matrix a class. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/api/java/jni/native_handle_impl.h" 11 #include "webrtc/api/java/jni/native_handle_impl.h"
12 12
13 #include <memory> 13 #include <memory>
14 14
15 #include "webrtc/api/java/jni/jni_helpers.h" 15 #include "webrtc/api/java/jni/jni_helpers.h"
16 #include "webrtc/base/bind.h" 16 #include "webrtc/base/bind.h"
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/keep_ref_until_done.h" 18 #include "webrtc/base/keep_ref_until_done.h"
19 #include "webrtc/base/logging.h" 19 #include "webrtc/base/logging.h"
20 #include "webrtc/base/scoped_ref_ptr.h" 20 #include "webrtc/base/scoped_ref_ptr.h"
21 21
22 using webrtc::NativeHandleBuffer; 22 using webrtc::NativeHandleBuffer;
23 23
24 namespace { 24 namespace webrtc_jni {
25 25
26 void RotateMatrix(float a[16], webrtc::VideoRotation rotation) { 26 Matrix::Matrix(JNIEnv* jni, jfloatArray a) {
27 RTC_CHECK_EQ(16, jni->GetArrayLength(a));
28 jfloat* ptr = jni->GetFloatArrayElements(a, nullptr);
29 for (int i = 0; i < 16; ++i) {
30 elem_[i] = ptr[i];
31 }
32 jni->ReleaseFloatArrayElements(a, ptr, 0);
33 }
34
35 jfloatArray Matrix::ToJava(JNIEnv* jni) {
36 jfloatArray matrix = jni->NewFloatArray(16);
37 jni->SetFloatArrayRegion(matrix, 0, 16, elem_);
38 return matrix;
39 }
40
41 void Matrix::Rotate(webrtc::VideoRotation rotation) {
27 // Texture coordinates are in the range 0 to 1. The transformation of the last 42 // 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 43 // 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. 44 // mirror x, we don't replace x by -x, but by 1-x.
30 switch (rotation) { 45 switch (rotation) {
31 case webrtc::kVideoRotation_0: 46 case webrtc::kVideoRotation_0:
32 break; 47 break;
33 case webrtc::kVideoRotation_90: { 48 case webrtc::kVideoRotation_90: {
34 const float ROTATE_90[16] = 49 const float ROTATE_90[16] =
35 { a[4], a[5], a[6], a[7], 50 { elem_[4], elem_[5], elem_[6], elem_[7],
36 -a[0], -a[1], -a[2], -a[3], 51 -elem_[0], -elem_[1], -elem_[2], -elem_[3],
37 a[8], a[9], a[10], a[11], 52 elem_[8], elem_[9], elem_[10], elem_[11],
38 a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]}; 53 elem_[0] + elem_[12], elem_[1] + elem_[13],
39 memcpy(a, ROTATE_90, sizeof(ROTATE_90)); 54 elem_[2] + elem_[14], elem_[3] + elem_[15]};
55 memcpy(elem_, ROTATE_90, sizeof(elem_));
40 } break; 56 } break;
41 case webrtc::kVideoRotation_180: { 57 case webrtc::kVideoRotation_180: {
42 const float ROTATE_180[16] = 58 const float ROTATE_180[16] =
43 { -a[0], -a[1], -a[2], -a[3], 59 { -elem_[0], -elem_[1], -elem_[2], -elem_[3],
44 -a[4], -a[5], -a[6], -a[7], 60 -elem_[4], -elem_[5], -elem_[6], -elem_[7],
45 a[8], a[9], a[10], a[11], 61 elem_[8], elem_[9], elem_[10], elem_[11],
46 a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14], 62 elem_[0] + elem_[4] + elem_[12], elem_[1] + elem_[5] + elem_[13],
47 a[3] + a[11]+ a[15]}; 63 elem_[2] + elem_[6] + elem_[14], elem_[3] + elem_[11]+ elem_[15]};
48 memcpy(a, ROTATE_180, sizeof(ROTATE_180)); 64 memcpy(elem_, ROTATE_180, sizeof(elem_));
49 } 65 } break;
50 break;
51 case webrtc::kVideoRotation_270: { 66 case webrtc::kVideoRotation_270: {
52 const float ROTATE_270[16] = 67 const float ROTATE_270[16] =
53 { -a[4], -a[5], -a[6], -a[7], 68 { -elem_[4], -elem_[5], -elem_[6], -elem_[7],
54 a[0], a[1], a[2], a[3], 69 elem_[0], elem_[1], elem_[2], elem_[3],
55 a[8], a[9], a[10], a[11], 70 elem_[8], elem_[9], elem_[10], elem_[11],
56 a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]}; 71 elem_[4] + elem_[12], elem_[5] + elem_[13],
57 memcpy(a, ROTATE_270, sizeof(ROTATE_270)); 72 elem_[6] + elem_[14], elem_[7] + elem_[15]};
73 memcpy(elem_, ROTATE_270, sizeof(elem_));
58 } break; 74 } break;
59 } 75 }
60 } 76 }
61 77
62 // Calculates result = a * b, in column-major order. 78 // Calculates result = a * b, in column-major order.
63 void MultiplyMatrix(const float a[16], const float b[16], float result[16]) { 79 void Matrix::Multiply(const float a[16], const float b[16], float result[16]) {
64 for (int i = 0; i < 4; ++i) { 80 for (int i = 0; i < 4; ++i) {
65 for (int j = 0; j < 4; ++j) { 81 for (int j = 0; j < 4; ++j) {
66 float sum = 0; 82 float sum = 0;
67 for (int k = 0; k < 4; ++k) { 83 for (int k = 0; k < 4; ++k) {
68 sum += a[k * 4 + j] * b[i * 4 + k]; 84 sum += a[k * 4 + j] * b[i * 4 + k];
69 } 85 }
70 result[i * 4 + j] = sum; 86 result[i * 4 + j] = sum;
71 } 87 }
72 } 88 }
73 } 89 }
74 90
75 // Center crop by keeping xFraction of the width and yFraction of the height, 91 // Center crop by keeping xFraction of the width and yFraction of the height,
76 // so e.g. cropping from 640x480 to 640x360 would use 92 // so e.g. cropping from 640x480 to 640x360 would use
77 // xFraction=1, yFraction=360/480. 93 // xFraction=1, yFraction=360/480.
78 void CropMatrix(float a[16], float xFraction, float yFraction) { 94 void Matrix::Crop(float xFraction,
79 // Move cropped area to the center of the frame by offsetting half the 95 float yFraction,
80 // removed area. 96 float xOffset,
81 const float xOffset = (1 - xFraction) / 2; 97 float yOffset) {
82 const float yOffset = (1 - yFraction) / 2; 98 const float crop_matrix[16] =
83 const float crop_matrix[16] = { 99 {xFraction, 0, 0, 0,
84 xFraction, 0, 0, 0, 100 0, yFraction, 0, 0,
85 0, yFraction, 0, 0, 101 0, 0, 1, 0,
86 0, 0, 1, 0, 102 xOffset, yOffset, 0, 1};
87 xOffset, yOffset, 0, 1}; 103 const Matrix old = *this;
88 float mul_result[16]; 104 Multiply(crop_matrix, old.elem_, this->elem_);
89 MultiplyMatrix(crop_matrix, a, mul_result);
90 memcpy(a, mul_result, sizeof(mul_result));
91 } 105 }
92 106
93 } // anonymouse namespace
94
95 namespace webrtc_jni {
96
97 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. 107 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
98 static const int kBufferAlignment = 64; 108 static const int kBufferAlignment = 64;
99 109
110 NativeHandleImpl::NativeHandleImpl(int id, const Matrix& matrix)
111 : oes_texture_id(id), sampling_matrix(matrix) {}
112
100 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, 113 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
101 jint j_oes_texture_id, 114 jint j_oes_texture_id,
102 jfloatArray j_transform_matrix) 115 jfloatArray j_transform_matrix)
103 : oes_texture_id(j_oes_texture_id) { 116 : oes_texture_id(j_oes_texture_id),
104 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); 117 sampling_matrix(jni, j_transform_matrix) {}
105 jfloat* transform_matrix_ptr =
106 jni->GetFloatArrayElements(j_transform_matrix, nullptr);
107 for (int i = 0; i < 16; ++i) {
108 sampling_matrix[i] = transform_matrix_ptr[i];
109 }
110 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0);
111 }
112 118
113 AndroidTextureBuffer::AndroidTextureBuffer( 119 AndroidTextureBuffer::AndroidTextureBuffer(
114 int width, 120 int width,
115 int height, 121 int height,
116 const NativeHandleImpl& native_handle, 122 const NativeHandleImpl& native_handle,
117 jobject surface_texture_helper, 123 jobject surface_texture_helper,
118 const rtc::Callback0<void>& no_longer_used) 124 const rtc::Callback0<void>& no_longer_used)
119 : webrtc::NativeHandleBuffer(&native_handle_, width, height), 125 : webrtc::NativeHandleBuffer(&native_handle_, width, height),
120 native_handle_(native_handle), 126 native_handle_(native_handle),
121 surface_texture_helper_(surface_texture_helper), 127 surface_texture_helper_(surface_texture_helper),
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 ScopedLocalRefFrame local_ref_frame(jni); 161 ScopedLocalRefFrame local_ref_frame(jni);
156 162
157 jmethodID transform_mid = GetMethodID( 163 jmethodID transform_mid = GetMethodID(
158 jni, 164 jni,
159 GetObjectClass(jni, surface_texture_helper_), 165 GetObjectClass(jni, surface_texture_helper_),
160 "textureToYUV", 166 "textureToYUV",
161 "(Ljava/nio/ByteBuffer;IIII[F)V"); 167 "(Ljava/nio/ByteBuffer;IIII[F)V");
162 168
163 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size); 169 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size);
164 170
165 // TODO(nisse): Keep java transform matrix around. 171 jfloatArray sampling_matrix = native_handle_.sampling_matrix.ToJava(jni);
166 jfloatArray sampling_matrix = jni->NewFloatArray(16);
167 jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
168 native_handle_.sampling_matrix);
169
170 jni->CallVoidMethod(surface_texture_helper_, 172 jni->CallVoidMethod(surface_texture_helper_,
171 transform_mid, 173 transform_mid,
172 byte_buffer, width(), height(), stride, 174 byte_buffer, width(), height(), stride,
173 native_handle_.oes_texture_id, sampling_matrix); 175 native_handle_.oes_texture_id, sampling_matrix);
174 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; 176 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception";
175 177
176 return copy; 178 return copy;
177 } 179 }
178 180
179 rtc::scoped_refptr<AndroidTextureBuffer> 181 rtc::scoped_refptr<AndroidTextureBuffer>
180 AndroidTextureBuffer::CropScaleAndRotate(int cropped_width, 182 AndroidTextureBuffer::CropScaleAndRotate(int cropped_width,
181 int cropped_height, 183 int cropped_height,
184 int crop_x,
185 int crop_y,
182 int dst_width, 186 int dst_width,
183 int dst_height, 187 int dst_height,
184 webrtc::VideoRotation rotation) { 188 webrtc::VideoRotation rotation) {
185 if (cropped_width == dst_width && cropped_height == dst_height && 189 if (cropped_width == dst_width && cropped_height == dst_height &&
186 width() == dst_width && height() == dst_height && 190 width() == dst_width && height() == dst_height &&
187 rotation == webrtc::kVideoRotation_0) { 191 rotation == webrtc::kVideoRotation_0) {
188 return this; 192 return this;
189 } 193 }
190 int rotated_width = (rotation % 180 == 0) ? dst_width : dst_height; 194 int rotated_width = (rotation % 180 == 0) ? dst_width : dst_height;
191 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_width; 195 int rotated_height = (rotation % 180 == 0) ? dst_height : dst_width;
192 196
193 // Here we use Bind magic to add a reference count to |this| until the newly 197 // Here we use Bind magic to add a reference count to |this| until the newly
194 // created AndroidTextureBuffer is destructed 198 // created AndroidTextureBuffer is destructed
195 rtc::scoped_refptr<AndroidTextureBuffer> buffer( 199 rtc::scoped_refptr<AndroidTextureBuffer> buffer(
196 new rtc::RefCountedObject<AndroidTextureBuffer>( 200 new rtc::RefCountedObject<AndroidTextureBuffer>(
197 rotated_width, rotated_height, native_handle_, 201 rotated_width, rotated_height, native_handle_,
198 surface_texture_helper_, rtc::KeepRefUntilDone(this))); 202 surface_texture_helper_, rtc::KeepRefUntilDone(this)));
199 203
200 if (cropped_width != width() || cropped_height != height()) { 204 if (cropped_width != width() || cropped_height != height()) {
201 CropMatrix(buffer->native_handle_.sampling_matrix, 205 buffer->native_handle_.sampling_matrix.Crop(
202 cropped_width / static_cast<float>(width()), 206 cropped_width / static_cast<float>(width()),
203 cropped_height / static_cast<float>(height())); 207 cropped_height / static_cast<float>(height()),
208 crop_x / static_cast<float>(width()),
209 crop_y / static_cast<float>(height()));
204 } 210 }
205 RotateMatrix(buffer->native_handle_.sampling_matrix, rotation); 211 buffer->native_handle_.sampling_matrix.Rotate(rotation);
206 return buffer; 212 return buffer;
207 } 213 }
208 214
209 } // namespace webrtc_jni 215 } // namespace webrtc_jni
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698