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; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 14 | import android.os.Handler; |
Artem Titarenko | 69540f4 | 2018-12-10 12:30:46 +0100 | [diff] [blame] | 15 | import android.support.annotation.Nullable; |
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 { |
Sami Kalliomäki | 44bd29a | 2019-08-27 15:09:15 +0200 | [diff] [blame^] | 22 | private static final int RELEASE_TIMEOUT_MS = 10000; |
| 23 | |
Magnus Jedvert | 169e042 | 2018-09-10 09:42:28 +0200 | [diff] [blame] | 24 | // This is the full resolution the texture has in memory after applying the transformation matrix |
| 25 | // that might include cropping. This resolution is useful to know when sampling the texture to |
| 26 | // avoid downscaling artifacts. |
| 27 | private final int unscaledWidth; |
| 28 | private final int unscaledHeight; |
| 29 | // This is the resolution that has been applied after cropAndScale(). |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 30 | private final int width; |
| 31 | private final int height; |
| 32 | private final Type type; |
| 33 | private final int id; |
| 34 | private final Matrix transformMatrix; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 35 | private final Handler toI420Handler; |
| 36 | private final YuvConverter yuvConverter; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 37 | private final RefCountDelegate refCountDelegate; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 38 | |
| 39 | public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix, |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 40 | Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) { |
Magnus Jedvert | 169e042 | 2018-09-10 09:42:28 +0200 | [diff] [blame] | 41 | this.unscaledWidth = width; |
| 42 | this.unscaledHeight = height; |
| 43 | this.width = width; |
| 44 | this.height = height; |
| 45 | this.type = type; |
| 46 | this.id = id; |
| 47 | this.transformMatrix = transformMatrix; |
| 48 | this.toI420Handler = toI420Handler; |
| 49 | this.yuvConverter = yuvConverter; |
| 50 | this.refCountDelegate = new RefCountDelegate(releaseCallback); |
| 51 | } |
| 52 | |
| 53 | private TextureBufferImpl(int unscaledWidth, int unscaledHeight, int width, int height, Type type, |
| 54 | int id, Matrix transformMatrix, Handler toI420Handler, YuvConverter yuvConverter, |
| 55 | @Nullable Runnable releaseCallback) { |
| 56 | this.unscaledWidth = unscaledWidth; |
| 57 | this.unscaledHeight = unscaledHeight; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 58 | this.width = width; |
| 59 | this.height = height; |
| 60 | this.type = type; |
| 61 | this.id = id; |
| 62 | this.transformMatrix = transformMatrix; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 63 | this.toI420Handler = toI420Handler; |
| 64 | this.yuvConverter = yuvConverter; |
Sami Kalliomäki | 44bd29a | 2019-08-27 15:09:15 +0200 | [diff] [blame^] | 65 | this.refCountDelegate = new RefCountDelegate(releaseCallback, RELEASE_TIMEOUT_MS); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public VideoFrame.TextureBuffer.Type getType() { |
| 70 | return type; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int getTextureId() { |
| 75 | return id; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Matrix getTransformMatrix() { |
| 80 | return transformMatrix; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int getWidth() { |
| 85 | return width; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int getHeight() { |
| 90 | return height; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public VideoFrame.I420Buffer toI420() { |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 95 | return ThreadUtils.invokeAtFrontUninterruptibly( |
| 96 | toI420Handler, () -> yuvConverter.convert(this)); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void retain() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 101 | refCountDelegate.retain(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void release() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 106 | refCountDelegate.release(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public VideoFrame.Buffer cropAndScale( |
| 111 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 112 | final Matrix cropAndScaleMatrix = new Matrix(); |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 113 | // In WebRTC, Y=0 is the top row, while in OpenGL Y=0 is the bottom row. This means that the Y |
| 114 | // direction is effectively reversed. |
| 115 | final int cropYFromBottom = height - (cropY + cropHeight); |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 116 | cropAndScaleMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height); |
| 117 | cropAndScaleMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height); |
Sami Kalliomäki | 64051d4 | 2018-04-16 13:37:07 +0000 | [diff] [blame] | 118 | |
Magnus Jedvert | 169e042 | 2018-09-10 09:42:28 +0200 | [diff] [blame] | 119 | return applyTransformMatrix(cropAndScaleMatrix, |
| 120 | (int) Math.round(unscaledWidth * cropWidth / (float) width), |
| 121 | (int) Math.round(unscaledHeight * cropHeight / (float) height), scaleWidth, scaleHeight); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the width of the texture in memory. This should only be used for downscaling, and you |
| 126 | * should still respect the width from getWidth(). |
| 127 | */ |
| 128 | public int getUnscaledWidth() { |
| 129 | return unscaledWidth; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the height of the texture in memory. This should only be used for downscaling, and you |
| 134 | * should still respect the height from getHeight(). |
| 135 | */ |
| 136 | public int getUnscaledHeight() { |
| 137 | return unscaledHeight; |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 140 | public Handler getToI420Handler() { |
| 141 | return toI420Handler; |
| 142 | } |
| 143 | |
| 144 | public YuvConverter getYuvConverter() { |
| 145 | return yuvConverter; |
| 146 | } |
| 147 | |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 148 | /** |
| 149 | * Create a new TextureBufferImpl with an applied transform matrix and a new size. The |
| 150 | * existing buffer is unchanged. The given transform matrix is applied first when texture |
| 151 | * coordinates are still in the unmodified [0, 1] range. |
| 152 | */ |
| 153 | public TextureBufferImpl applyTransformMatrix( |
| 154 | Matrix transformMatrix, int newWidth, int newHeight) { |
Magnus Jedvert | 169e042 | 2018-09-10 09:42:28 +0200 | [diff] [blame] | 155 | return applyTransformMatrix(transformMatrix, /* unscaledWidth= */ newWidth, |
| 156 | /* unscaledHeight= */ newHeight, /* scaledWidth= */ newWidth, |
| 157 | /* scaledHeight= */ newHeight); |
| 158 | } |
| 159 | |
| 160 | private TextureBufferImpl applyTransformMatrix(Matrix transformMatrix, int unscaledWidth, |
| 161 | int unscaledHeight, int scaledWidth, int scaledHeight) { |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 162 | final Matrix newMatrix = new Matrix(this.transformMatrix); |
| 163 | newMatrix.preConcat(transformMatrix); |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 164 | retain(); |
Magnus Jedvert | 169e042 | 2018-09-10 09:42:28 +0200 | [diff] [blame] | 165 | return new TextureBufferImpl(unscaledWidth, unscaledHeight, scaledWidth, scaledHeight, type, id, |
| 166 | newMatrix, toI420Handler, yuvConverter, this ::release); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 167 | } |
| 168 | } |