sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 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. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import android.graphics.Matrix; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 14 | import javax.annotation.Nullable; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 15 | import android.os.Handler; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 18 | * Android texture buffer that glues together the necessary information together with a generic |
| 19 | * release callback. ToI420() is implemented by providing a Handler and a YuvConverter. |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 20 | */ |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 21 | public class TextureBufferImpl implements VideoFrame.TextureBuffer { |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 22 | private final int width; |
| 23 | private final int height; |
| 24 | private final Type type; |
| 25 | private final int id; |
| 26 | private final Matrix transformMatrix; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 27 | private final Handler toI420Handler; |
| 28 | private final YuvConverter yuvConverter; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 29 | private final RefCountDelegate refCountDelegate; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 30 | |
| 31 | public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix, |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 32 | Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) { |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 33 | this.width = width; |
| 34 | this.height = height; |
| 35 | this.type = type; |
| 36 | this.id = id; |
| 37 | this.transformMatrix = transformMatrix; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 38 | this.toI420Handler = toI420Handler; |
| 39 | this.yuvConverter = yuvConverter; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 40 | this.refCountDelegate = new RefCountDelegate(releaseCallback); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public VideoFrame.TextureBuffer.Type getType() { |
| 45 | return type; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int getTextureId() { |
| 50 | return id; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Matrix getTransformMatrix() { |
| 55 | return transformMatrix; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public int getWidth() { |
| 60 | return width; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int getHeight() { |
| 65 | return height; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public VideoFrame.I420Buffer toI420() { |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 70 | return ThreadUtils.invokeAtFrontUninterruptibly( |
| 71 | toI420Handler, () -> yuvConverter.convert(this)); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void retain() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 76 | refCountDelegate.retain(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void release() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 81 | refCountDelegate.release(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public VideoFrame.Buffer cropAndScale( |
| 86 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 87 | final Matrix newMatrix = new Matrix(transformMatrix); |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame^] | 88 | // In WebRTC, Y=0 is the top row, while in OpenGL Y=0 is the bottom row. This means that the Y |
| 89 | // direction is effectively reversed. |
| 90 | final int cropYFromBottom = height - (cropY + cropHeight); |
| 91 | newMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height); |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 92 | newMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height); |
Sami Kalliomäki | 64051d4 | 2018-04-16 13:37:07 +0000 | [diff] [blame] | 93 | |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 94 | retain(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 95 | return new TextureBufferImpl( |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 96 | scaleWidth, scaleHeight, type, id, newMatrix, toI420Handler, yuvConverter, this ::release); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 97 | } |
| 98 | } |