| Index: webrtc/api/java/jni/native_handle_impl.cc
 | 
| diff --git a/webrtc/api/java/jni/native_handle_impl.cc b/webrtc/api/java/jni/native_handle_impl.cc
 | 
| index 1f180ade9cc79ff33bcd6f1174d6817e44187b4a..2ee5b262f2c11634d63c3cd5163fb006ccbd8ab3 100644
 | 
| --- a/webrtc/api/java/jni/native_handle_impl.cc
 | 
| +++ b/webrtc/api/java/jni/native_handle_impl.cc
 | 
| @@ -21,9 +21,9 @@
 | 
|  
 | 
|  using webrtc::NativeHandleBuffer;
 | 
|  
 | 
| -namespace {
 | 
| +namespace webrtc_jni {
 | 
|  
 | 
| -void RotateMatrix(float a[16], webrtc::VideoRotation rotation) {
 | 
| +void NativeHandleImpl::Matrix::Rotate(webrtc::VideoRotation rotation) {
 | 
|    // Texture coordinates are in the range 0 to 1. The transformation of the last
 | 
|    // row in each rotation matrix is needed for proper translation, e.g, to
 | 
|    // mirror x, we don't replace x by -x, but by 1-x.
 | 
| @@ -31,43 +31,47 @@ void RotateMatrix(float a[16], webrtc::VideoRotation rotation) {
 | 
|      case webrtc::kVideoRotation_0:
 | 
|        break;
 | 
|      case webrtc::kVideoRotation_90: {
 | 
| -      const float ROTATE_90[16] =
 | 
| -          { a[4], a[5], a[6], a[7],
 | 
| -            -a[0], -a[1], -a[2], -a[3],
 | 
| -            a[8], a[9], a[10], a[11],
 | 
| -            a[0] + a[12], a[1] + a[13], a[2] + a[14], a[3] + a[15]};
 | 
| -      memcpy(a, ROTATE_90, sizeof(ROTATE_90));
 | 
| +      const Matrix ROTATE_90 = {
 | 
| +          {elem[4], elem[5], elem[6], elem[7],
 | 
| +           -elem[0], -elem[1], -elem[2], -elem[3],
 | 
| +           elem[8], elem[9], elem[10], elem[11],
 | 
| +           elem[0] + elem[12], elem[1] + elem[13],
 | 
| +           elem[2] + elem[14], elem[3] + elem[15]}};
 | 
| +      *this = ROTATE_90;
 | 
|      } break;
 | 
|      case webrtc::kVideoRotation_180: {
 | 
| -      const float ROTATE_180[16] =
 | 
| -          { -a[0], -a[1], -a[2], -a[3],
 | 
| -            -a[4], -a[5], -a[6], -a[7],
 | 
| -            a[8], a[9], a[10], a[11],
 | 
| -            a[0] + a[4] + a[12], a[1] +a[5] + a[13], a[2] + a[6] + a[14],
 | 
| -            a[3] + a[11]+ a[15]};
 | 
| -        memcpy(a, ROTATE_180, sizeof(ROTATE_180));
 | 
| +      const Matrix ROTATE_180 = {
 | 
| +          {-elem[0], -elem[1], -elem[2], -elem[3],
 | 
| +           -elem[4], -elem[5], -elem[6], -elem[7],
 | 
| +           elem[8], elem[9], elem[10], elem[11],
 | 
| +           elem[0] + elem[4] + elem[12], elem[1] + elem[5] + elem[13],
 | 
| +           elem[2] + elem[6] + elem[14], elem[3] + elem[11] + elem[15]}};
 | 
| +      *this = ROTATE_180;
 | 
|        }
 | 
|        break;
 | 
|      case webrtc::kVideoRotation_270: {
 | 
| -      const float ROTATE_270[16] =
 | 
| -          { -a[4], -a[5], -a[6], -a[7],
 | 
| -            a[0], a[1], a[2], a[3],
 | 
| -            a[8], a[9], a[10], a[11],
 | 
| -            a[4] + a[12], a[5] + a[13], a[6] + a[14], a[7] + a[15]};
 | 
| -        memcpy(a, ROTATE_270, sizeof(ROTATE_270));
 | 
| +      const Matrix ROTATE_270 = {
 | 
| +          {-elem[4], -elem[5], -elem[6], -elem[7],
 | 
| +           elem[0], elem[1], elem[2], elem[3],
 | 
| +           elem[8], elem[9], elem[10], elem[11],
 | 
| +           elem[4] + elem[12], elem[5] + elem[13],
 | 
| +           elem[6] + elem[14], elem[7] + elem[15]}};
 | 
| +      *this = ROTATE_270;
 | 
|      } break;
 | 
|    }
 | 
|  }
 | 
|  
 | 
|  // Calculates result = a * b, in column-major order.
 | 
| -void MultiplyMatrix(const float a[16], const float b[16], float result[16]) {
 | 
| +void NativeHandleImpl::Matrix::Multiply(const Matrix& a,
 | 
| +                                        const Matrix& b,
 | 
| +                                        Matrix* result) {
 | 
|    for (int i = 0; i < 4; ++i) {
 | 
|      for (int j = 0; j < 4; ++j) {
 | 
|        float sum = 0;
 | 
|        for (int k = 0; k < 4; ++k) {
 | 
| -        sum += a[k * 4 + j] * b[i * 4 + k];
 | 
| +        sum += a.elem[k * 4 + j] * b.elem[i * 4 + k];
 | 
|        }
 | 
| -      result[i * 4 + j] = sum;
 | 
| +      result->elem[i * 4 + j] = sum;
 | 
|      }
 | 
|    }
 | 
|  }
 | 
| @@ -75,28 +79,26 @@ void MultiplyMatrix(const float a[16], const float b[16], float result[16]) {
 | 
|  // Center crop by keeping xFraction of the width and yFraction of the height,
 | 
|  // so e.g. cropping from 640x480 to 640x360 would use
 | 
|  // xFraction=1, yFraction=360/480.
 | 
| -void CropMatrix(float a[16], float xFraction, float yFraction) {
 | 
| -  // Move cropped area to the center of the frame by offsetting half the
 | 
| -  // removed area.
 | 
| -  const float xOffset = (1 - xFraction) / 2;
 | 
| -  const float yOffset = (1 - yFraction) / 2;
 | 
| -  const float crop_matrix[16] = {
 | 
| -    xFraction, 0, 0, 0,
 | 
| -    0, yFraction, 0, 0,
 | 
| -    0, 0, 1, 0,
 | 
| -    xOffset, yOffset, 0, 1};
 | 
| -  float mul_result[16];
 | 
| -  MultiplyMatrix(crop_matrix, a, mul_result);
 | 
| -  memcpy(a, mul_result, sizeof(mul_result));
 | 
| +void NativeHandleImpl::Matrix::Crop(float xFraction,
 | 
| +                                    float yFraction,
 | 
| +                                    float xOffset,
 | 
| +                                    float yOffset) {
 | 
| +  const Matrix crop_matrix = {
 | 
| +      {xFraction, 0, 0, 0,
 | 
| +       0, yFraction, 0, 0,
 | 
| +       0, 0, 1, 0,
 | 
| +       xOffset, yOffset, 0, 1}};
 | 
| +  Matrix result;
 | 
| +  Multiply(crop_matrix, *this, &result);
 | 
| +  *this = result;
 | 
|  }
 | 
|  
 | 
| -}  // anonymouse namespace
 | 
| -
 | 
| -namespace webrtc_jni {
 | 
| -
 | 
|  // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
 | 
|  static const int kBufferAlignment = 64;
 | 
|  
 | 
| +NativeHandleImpl::NativeHandleImpl(int id, const Matrix& matrix)
 | 
| +    : oes_texture_id(id), sampling_matrix(matrix) {}
 | 
| +
 | 
|  NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
 | 
|                                     jint j_oes_texture_id,
 | 
|                                     jfloatArray j_transform_matrix)
 | 
| @@ -105,10 +107,33 @@ NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
 | 
|    jfloat* transform_matrix_ptr =
 | 
|        jni->GetFloatArrayElements(j_transform_matrix, nullptr);
 | 
|    for (int i = 0; i < 16; ++i) {
 | 
| -    sampling_matrix[i] = transform_matrix_ptr[i];
 | 
| +    sampling_matrix.elem[i] = transform_matrix_ptr[i];
 | 
|    }
 | 
|    jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0);
 | 
|  }
 | 
| +#if 0
 | 
| +NativeHandleImpl NativeHandleImpl::Crop(
 | 
| +    float cropped_width,
 | 
| +    float cropped_height,
 | 
| +    float crop_x,
 | 
| +    float crop_y) const {
 | 
| +  float matrix[16];
 | 
| +  memcpy(matrix, sampling_matrix, sizeof(matrix));
 | 
| +
 | 
| +  CropMatrix(matrix, cropped_width, cropped_height, crop_x, crop_y);
 | 
| +
 | 
| +  return NativeHandleImpl(oes_texture_id, matrix);
 | 
| +}
 | 
| +
 | 
| +NativeHandleImpl NativeHandleImpl::Rotate(
 | 
| +    webrtc::VideoRotation rotation) const {
 | 
| +  float matrix[16];
 | 
| +  memcpy(matrix, sampling_matrix, sizeof(matrix));
 | 
| +
 | 
| +  RotateMatrix(matrix, rotation);
 | 
| +  return NativeHandleImpl(oes_texture_id, matrix);
 | 
| +}
 | 
| +#endif
 | 
|  
 | 
|  AndroidTextureBuffer::AndroidTextureBuffer(
 | 
|      int width,
 | 
| @@ -165,7 +190,7 @@ AndroidTextureBuffer::NativeToI420Buffer() {
 | 
|    // TODO(nisse): Keep java transform matrix around.
 | 
|    jfloatArray sampling_matrix = jni->NewFloatArray(16);
 | 
|    jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
 | 
| -                           native_handle_.sampling_matrix);
 | 
| +                           native_handle_.sampling_matrix.elem);
 | 
|  
 | 
|    jni->CallVoidMethod(surface_texture_helper_,
 | 
|                        transform_mid,
 | 
| @@ -179,6 +204,8 @@ AndroidTextureBuffer::NativeToI420Buffer() {
 | 
|  rtc::scoped_refptr<AndroidTextureBuffer>
 | 
|  AndroidTextureBuffer::CropScaleAndRotate(int cropped_width,
 | 
|                                           int cropped_height,
 | 
| +                                         int crop_x,
 | 
| +                                         int crop_y,
 | 
|                                           int dst_width,
 | 
|                                           int dst_height,
 | 
|                                           webrtc::VideoRotation rotation) {
 | 
| @@ -198,11 +225,13 @@ AndroidTextureBuffer::CropScaleAndRotate(int cropped_width,
 | 
|            surface_texture_helper_, rtc::KeepRefUntilDone(this)));
 | 
|  
 | 
|    if (cropped_width != width() || cropped_height != height()) {
 | 
| -    CropMatrix(buffer->native_handle_.sampling_matrix,
 | 
| -               cropped_width / static_cast<float>(width()),
 | 
| -               cropped_height / static_cast<float>(height()));
 | 
| +    buffer->native_handle_.sampling_matrix.Crop(
 | 
| +        cropped_width / static_cast<float>(width()),
 | 
| +        cropped_height / static_cast<float>(height()),
 | 
| +        crop_x / static_cast<float>(width()),
 | 
| +        crop_y / static_cast<float>(height()));
 | 
|    }
 | 
| -  RotateMatrix(buffer->native_handle_.sampling_matrix, rotation);
 | 
| +  buffer->native_handle_.sampling_matrix.Rotate(rotation);
 | 
|    return buffer;
 | 
|  }
 | 
|  
 | 
| 
 |