Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import android.graphics.Point; |
| 14 | import android.opengl.Matrix; |
Magnus Jedvert | 62b1c35 | 2016-10-05 15:56:06 +0200 | [diff] [blame] | 15 | import android.view.View; |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 16 | |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 17 | /** |
| 18 | * Static helper functions for renderer implementations. |
| 19 | */ |
| 20 | public class RendererCommon { |
| 21 | /** Interface for reporting rendering events. */ |
| 22 | public static interface RendererEvents { |
| 23 | /** |
| 24 | * Callback fired once first frame is rendered. |
| 25 | */ |
| 26 | public void onFirstFrameRendered(); |
| 27 | |
| 28 | /** |
| 29 | * Callback fired when rendered frame resolution or rotation has changed. |
| 30 | */ |
| 31 | public void onFrameResolutionChanged(int videoWidth, int videoHeight, int rotation); |
| 32 | } |
| 33 | |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 34 | /** Interface for rendering frames on an EGLSurface. */ |
| 35 | public static interface GlDrawer { |
| 36 | /** |
| 37 | * Functions for drawing frames with different sources. The rendering surface target is |
| 38 | * implied by the current EGL context of the calling thread and requires no explicit argument. |
| 39 | * The coordinates specify the viewport location on the surface target. |
| 40 | */ |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 41 | void drawOes(int oesTextureId, float[] texMatrix, int frameWidth, int frameHeight, |
| 42 | int viewportX, int viewportY, int viewportWidth, int viewportHeight); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 43 | void drawRgb(int textureId, float[] texMatrix, int frameWidth, int frameHeight, int viewportX, |
| 44 | int viewportY, int viewportWidth, int viewportHeight); |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 45 | void drawYuv(int[] yuvTextures, float[] texMatrix, int frameWidth, int frameHeight, |
| 46 | int viewportX, int viewportY, int viewportWidth, int viewportHeight); |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 47 | |
sakal | 160e32c | 2017-08-28 02:41:38 -0700 | [diff] [blame] | 48 | /** |
sakal | 9bc599f | 2017-09-08 04:46:33 -0700 | [diff] [blame] | 49 | * Release all GL resources. This needs to be done manually, otherwise resources may leak. |
sakal | 160e32c | 2017-08-28 02:41:38 -0700 | [diff] [blame] | 50 | */ |
sakal | 9bc599f | 2017-09-08 04:46:33 -0700 | [diff] [blame] | 51 | void release(); |
| 52 | } |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 53 | |
sakal | 9bc599f | 2017-09-08 04:46:33 -0700 | [diff] [blame] | 54 | /** |
Magnus Jedvert | 62b1c35 | 2016-10-05 15:56:06 +0200 | [diff] [blame] | 55 | * Helper class for determining layout size based on layout requirements, scaling type, and video |
| 56 | * aspect ratio. |
| 57 | */ |
| 58 | public static class VideoLayoutMeasure { |
| 59 | // The scaling type determines how the video will fill the allowed layout area in measure(). It |
| 60 | // can be specified separately for the case when video has matched orientation with layout size |
| 61 | // and when there is an orientation mismatch. |
| 62 | private ScalingType scalingTypeMatchOrientation = ScalingType.SCALE_ASPECT_BALANCED; |
| 63 | private ScalingType scalingTypeMismatchOrientation = ScalingType.SCALE_ASPECT_BALANCED; |
| 64 | |
| 65 | public void setScalingType(ScalingType scalingType) { |
| 66 | this.scalingTypeMatchOrientation = scalingType; |
| 67 | this.scalingTypeMismatchOrientation = scalingType; |
| 68 | } |
| 69 | |
| 70 | public void setScalingType( |
| 71 | ScalingType scalingTypeMatchOrientation, ScalingType scalingTypeMismatchOrientation) { |
| 72 | this.scalingTypeMatchOrientation = scalingTypeMatchOrientation; |
| 73 | this.scalingTypeMismatchOrientation = scalingTypeMismatchOrientation; |
| 74 | } |
| 75 | |
| 76 | public Point measure(int widthSpec, int heightSpec, int frameWidth, int frameHeight) { |
| 77 | // Calculate max allowed layout size. |
| 78 | final int maxWidth = View.getDefaultSize(Integer.MAX_VALUE, widthSpec); |
| 79 | final int maxHeight = View.getDefaultSize(Integer.MAX_VALUE, heightSpec); |
| 80 | if (frameWidth == 0 || frameHeight == 0 || maxWidth == 0 || maxHeight == 0) { |
| 81 | return new Point(maxWidth, maxHeight); |
| 82 | } |
| 83 | // Calculate desired display size based on scaling type, video aspect ratio, |
| 84 | // and maximum layout size. |
| 85 | final float frameAspect = frameWidth / (float) frameHeight; |
| 86 | final float displayAspect = maxWidth / (float) maxHeight; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 87 | final ScalingType scalingType = (frameAspect > 1.0f) == (displayAspect > 1.0f) |
Magnus Jedvert | 62b1c35 | 2016-10-05 15:56:06 +0200 | [diff] [blame] | 88 | ? scalingTypeMatchOrientation |
| 89 | : scalingTypeMismatchOrientation; |
magjed | df494b0 | 2016-10-07 05:32:35 -0700 | [diff] [blame] | 90 | final Point layoutSize = getDisplaySize(scalingType, frameAspect, maxWidth, maxHeight); |
Magnus Jedvert | 62b1c35 | 2016-10-05 15:56:06 +0200 | [diff] [blame] | 91 | |
| 92 | // If the measure specification is forcing a specific size - yield. |
| 93 | if (View.MeasureSpec.getMode(widthSpec) == View.MeasureSpec.EXACTLY) { |
| 94 | layoutSize.x = maxWidth; |
| 95 | } |
| 96 | if (View.MeasureSpec.getMode(heightSpec) == View.MeasureSpec.EXACTLY) { |
| 97 | layoutSize.y = maxHeight; |
| 98 | } |
| 99 | return layoutSize; |
| 100 | } |
| 101 | } |
| 102 | |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 103 | // Types of video scaling: |
| 104 | // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by |
| 105 | // maintaining the aspect ratio (black borders may be displayed). |
| 106 | // SCALE_ASPECT_FILL - video frame is scaled to fill the size of the view by |
| 107 | // maintaining the aspect ratio. Some portion of the video frame may be |
| 108 | // clipped. |
| 109 | // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will fill as much as |
| 110 | // possible of the view while maintaining aspect ratio, under the constraint that at least |
| 111 | // |BALANCED_VISIBLE_FRACTION| of the frame content will be shown. |
| 112 | public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_ASPECT_BALANCED } |
| 113 | // The minimum fraction of the frame content that will be shown for |SCALE_ASPECT_BALANCED|. |
| 114 | // This limits excessive cropping when adjusting display size. |
Magnus Jedvert | 7230a21 | 2015-08-25 12:31:21 +0200 | [diff] [blame] | 115 | private static float BALANCED_VISIBLE_FRACTION = 0.5625f; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 116 | // clang-format off |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 117 | public static final float[] identityMatrix() { |
| 118 | return new float[] { |
| 119 | 1, 0, 0, 0, |
| 120 | 0, 1, 0, 0, |
| 121 | 0, 0, 1, 0, |
| 122 | 0, 0, 0, 1}; |
| 123 | } |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 124 | // Matrix with transform y' = 1 - y. |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 125 | public static final float[] verticalFlipMatrix() { |
| 126 | return new float[] { |
| 127 | 1, 0, 0, 0, |
| 128 | 0, -1, 0, 0, |
| 129 | 0, 0, 1, 0, |
| 130 | 0, 1, 0, 1}; |
| 131 | } |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 132 | |
perkj | 3d06eca | 2015-10-08 12:53:33 +0200 | [diff] [blame] | 133 | // Matrix with transform x' = 1 - x. |
| 134 | public static final float[] horizontalFlipMatrix() { |
| 135 | return new float[] { |
| 136 | -1, 0, 0, 0, |
| 137 | 0, 1, 0, 0, |
| 138 | 0, 0, 1, 0, |
| 139 | 1, 0, 0, 1}; |
| 140 | } |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 141 | // clang-format on |
perkj | 3d06eca | 2015-10-08 12:53:33 +0200 | [diff] [blame] | 142 | |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 143 | /** |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 144 | * Returns texture matrix that will have the effect of rotating the frame |rotationDegree| |
| 145 | * clockwise when rendered. |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 146 | */ |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 147 | public static float[] rotateTextureMatrix(float[] textureMatrix, float rotationDegree) { |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 148 | final float[] rotationMatrix = new float[16]; |
Magnus Jedvert | 8ce0bd5 | 2015-09-09 18:51:06 +0200 | [diff] [blame] | 149 | Matrix.setRotateM(rotationMatrix, 0, rotationDegree, 0, 0, 1); |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 150 | adjustOrigin(rotationMatrix); |
Magnus Jedvert | 27551c9 | 2015-09-30 18:24:54 +0200 | [diff] [blame] | 151 | return multiplyMatrices(textureMatrix, rotationMatrix); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns new matrix with the result of a * b. |
| 156 | */ |
| 157 | public static float[] multiplyMatrices(float[] a, float[] b) { |
| 158 | final float[] resultMatrix = new float[16]; |
| 159 | Matrix.multiplyMM(resultMatrix, 0, a, 0, b, 0); |
| 160 | return resultMatrix; |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns layout transformation matrix that applies an optional mirror effect and compensates |
| 165 | * for video vs display aspect ratio. |
| 166 | */ |
| 167 | public static float[] getLayoutMatrix( |
| 168 | boolean mirror, float videoAspectRatio, float displayAspectRatio) { |
| 169 | float scaleX = 1; |
| 170 | float scaleY = 1; |
| 171 | // Scale X or Y dimension so that video and display size have same aspect ratio. |
| 172 | if (displayAspectRatio > videoAspectRatio) { |
| 173 | scaleY = videoAspectRatio / displayAspectRatio; |
| 174 | } else { |
| 175 | scaleX = displayAspectRatio / videoAspectRatio; |
| 176 | } |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 177 | // Apply optional horizontal flip. |
| 178 | if (mirror) { |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 179 | scaleX *= -1; |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 180 | } |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 181 | final float matrix[] = new float[16]; |
| 182 | Matrix.setIdentityM(matrix, 0); |
| 183 | Matrix.scaleM(matrix, 0, scaleX, scaleY, 1); |
| 184 | adjustOrigin(matrix); |
| 185 | return matrix; |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 188 | /** Converts a float[16] matrix array to android.graphics.Matrix. */ |
| 189 | public static android.graphics.Matrix convertMatrixToAndroidGraphicsMatrix(float[] matrix4x4) { |
| 190 | // clang-format off |
| 191 | float[] values = { |
| 192 | matrix4x4[0 * 4 + 0], matrix4x4[1 * 4 + 0], matrix4x4[3 * 4 + 0], |
| 193 | matrix4x4[0 * 4 + 1], matrix4x4[1 * 4 + 1], matrix4x4[3 * 4 + 1], |
| 194 | matrix4x4[0 * 4 + 3], matrix4x4[1 * 4 + 3], matrix4x4[3 * 4 + 3], |
| 195 | }; |
| 196 | // clang-format on |
| 197 | |
| 198 | android.graphics.Matrix matrix = new android.graphics.Matrix(); |
| 199 | matrix.setValues(values); |
| 200 | return matrix; |
| 201 | } |
| 202 | |
Bjorn Mellem | 0cf9a4a | 2017-07-18 13:19:26 -0700 | [diff] [blame] | 203 | /** Converts android.graphics.Matrix to a float[16] matrix array. */ |
| 204 | public static float[] convertMatrixFromAndroidGraphicsMatrix(android.graphics.Matrix matrix) { |
| 205 | float[] values = new float[9]; |
| 206 | matrix.getValues(values); |
| 207 | |
| 208 | // The android.graphics.Matrix looks like this: |
| 209 | // [x1 y1 w1] |
| 210 | // [x2 y2 w2] |
| 211 | // [x3 y3 w3] |
| 212 | // We want to contruct a matrix that looks like this: |
| 213 | // [x1 y1 0 w1] |
| 214 | // [x2 y2 0 w2] |
| 215 | // [ 0 0 1 0] |
| 216 | // [x3 y3 0 w3] |
| 217 | // Since it is stored in column-major order, it looks like this: |
| 218 | // [x1 x2 0 x3 |
| 219 | // y1 y2 0 y3 |
| 220 | // 0 0 1 0 |
| 221 | // w1 w2 0 w3] |
| 222 | // clang-format off |
| 223 | float[] matrix4x4 = { |
| 224 | values[0 * 3 + 0], values[1 * 3 + 0], 0, values[2 * 3 + 0], |
| 225 | values[0 * 3 + 1], values[1 * 3 + 1], 0, values[2 * 3 + 1], |
| 226 | 0, 0, 1, 0, |
| 227 | values[0 * 3 + 2], values[1 * 3 + 2], 0, values[2 * 3 + 2], |
| 228 | }; |
| 229 | // clang-format on |
| 230 | return matrix4x4; |
| 231 | } |
| 232 | |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 233 | /** |
| 234 | * Calculate display size based on scaling type, video aspect ratio, and maximum display size. |
| 235 | */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 236 | public static Point getDisplaySize( |
| 237 | ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) { |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 238 | return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio, |
| 239 | maxDisplayWidth, maxDisplayHeight); |
| 240 | } |
| 241 | |
| 242 | /** |
Magnus Jedvert | 529528c | 2015-09-09 10:59:46 +0200 | [diff] [blame] | 243 | * Move |matrix| transformation origin to (0.5, 0.5). This is the origin for texture coordinates |
| 244 | * that are in the range 0 to 1. |
| 245 | */ |
| 246 | private static void adjustOrigin(float[] matrix) { |
| 247 | // Note that OpenGL is using column-major order. |
| 248 | // Pre translate with -0.5 to move coordinates to range [-0.5, 0.5]. |
| 249 | matrix[12] -= 0.5f * (matrix[0] + matrix[4]); |
| 250 | matrix[13] -= 0.5f * (matrix[1] + matrix[5]); |
| 251 | // Post translate with 0.5 to move coordinates to range [0, 1]. |
| 252 | matrix[12] += 0.5f; |
| 253 | matrix[13] += 0.5f; |
| 254 | } |
| 255 | |
| 256 | /** |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 257 | * Each scaling type has a one-to-one correspondence to a numeric minimum fraction of the video |
| 258 | * that must remain visible. |
| 259 | */ |
| 260 | private static float convertScalingTypeToVisibleFraction(ScalingType scalingType) { |
| 261 | switch (scalingType) { |
| 262 | case SCALE_ASPECT_FIT: |
| 263 | return 1.0f; |
| 264 | case SCALE_ASPECT_FILL: |
| 265 | return 0.0f; |
| 266 | case SCALE_ASPECT_BALANCED: |
| 267 | return BALANCED_VISIBLE_FRACTION; |
| 268 | default: |
| 269 | throw new IllegalArgumentException(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Calculate display size based on minimum fraction of the video that must remain visible, |
| 275 | * video aspect ratio, and maximum display size. |
| 276 | */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 277 | private static Point getDisplaySize( |
| 278 | float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) { |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 279 | // If there is no constraint on the amount of cropping, fill the allowed display area. |
| 280 | if (minVisibleFraction == 0 || videoAspectRatio == 0) { |
| 281 | return new Point(maxDisplayWidth, maxDisplayHeight); |
| 282 | } |
| 283 | // Each dimension is constrained on max display size and how much we are allowed to crop. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 284 | final int width = Math.min( |
| 285 | maxDisplayWidth, Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio)); |
| 286 | final int height = Math.min( |
| 287 | maxDisplayHeight, Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio)); |
Magnus Jedvert | ff020c0 | 2015-08-20 14:03:07 +0200 | [diff] [blame] | 288 | return new Point(width, height); |
| 289 | } |
| 290 | } |