blob: d6472393f746763439c962371e252a36ca9a629a [file] [log] [blame]
Magnus Jedvertff020c02015-08-20 14:03:07 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Magnus Jedvertff020c02015-08-20 14:03:07 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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 Jedvertff020c02015-08-20 14:03:07 +02009 */
10
11package org.webrtc;
12
13import android.graphics.Point;
14import android.opengl.Matrix;
Magnus Jedvert62b1c352016-10-05 15:56:06 +020015import android.view.View;
Magnus Jedvert51254332015-12-15 16:22:29 +010016
Magnus Jedvertff020c02015-08-20 14:03:07 +020017/**
18 * Static helper functions for renderer implementations.
19 */
20public 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 Jedvert65c61dc2018-06-15 09:33:20 +020034 /**
35 * Interface for rendering frames on an EGLSurface with specified viewport location. Rotation,
36 * mirror, and cropping is specified using a 4x4 texture coordinate transform matrix. The frame
37 * input can either be an OES texture, RGB texture, or YUV textures in I420 format. The function
38 * release() must be called manually to free the resources held by this object.
39 */
Magnus Jedvert51254332015-12-15 16:22:29 +010040 public static interface GlDrawer {
41 /**
42 * Functions for drawing frames with different sources. The rendering surface target is
43 * implied by the current EGL context of the calling thread and requires no explicit argument.
44 * The coordinates specify the viewport location on the surface target.
45 */
ivoc1aa435c2016-05-04 07:14:14 -070046 void drawOes(int oesTextureId, float[] texMatrix, int frameWidth, int frameHeight,
47 int viewportX, int viewportY, int viewportWidth, int viewportHeight);
sakalb6760f92016-09-29 04:12:44 -070048 void drawRgb(int textureId, float[] texMatrix, int frameWidth, int frameHeight, int viewportX,
49 int viewportY, int viewportWidth, int viewportHeight);
ivoc1aa435c2016-05-04 07:14:14 -070050 void drawYuv(int[] yuvTextures, float[] texMatrix, int frameWidth, int frameHeight,
51 int viewportX, int viewportY, int viewportWidth, int viewportHeight);
Magnus Jedvert51254332015-12-15 16:22:29 +010052
sakal160e32c2017-08-28 02:41:38 -070053 /**
sakal9bc599f2017-09-08 04:46:33 -070054 * Release all GL resources. This needs to be done manually, otherwise resources may leak.
sakal160e32c2017-08-28 02:41:38 -070055 */
sakal9bc599f2017-09-08 04:46:33 -070056 void release();
57 }
sakal6bdcefc2017-08-15 01:56:02 -070058
sakal9bc599f2017-09-08 04:46:33 -070059 /**
Magnus Jedvert62b1c352016-10-05 15:56:06 +020060 * Helper class for determining layout size based on layout requirements, scaling type, and video
61 * aspect ratio.
62 */
63 public static class VideoLayoutMeasure {
64 // The scaling type determines how the video will fill the allowed layout area in measure(). It
65 // can be specified separately for the case when video has matched orientation with layout size
66 // and when there is an orientation mismatch.
67 private ScalingType scalingTypeMatchOrientation = ScalingType.SCALE_ASPECT_BALANCED;
68 private ScalingType scalingTypeMismatchOrientation = ScalingType.SCALE_ASPECT_BALANCED;
69
70 public void setScalingType(ScalingType scalingType) {
71 this.scalingTypeMatchOrientation = scalingType;
72 this.scalingTypeMismatchOrientation = scalingType;
73 }
74
75 public void setScalingType(
76 ScalingType scalingTypeMatchOrientation, ScalingType scalingTypeMismatchOrientation) {
77 this.scalingTypeMatchOrientation = scalingTypeMatchOrientation;
78 this.scalingTypeMismatchOrientation = scalingTypeMismatchOrientation;
79 }
80
81 public Point measure(int widthSpec, int heightSpec, int frameWidth, int frameHeight) {
82 // Calculate max allowed layout size.
83 final int maxWidth = View.getDefaultSize(Integer.MAX_VALUE, widthSpec);
84 final int maxHeight = View.getDefaultSize(Integer.MAX_VALUE, heightSpec);
85 if (frameWidth == 0 || frameHeight == 0 || maxWidth == 0 || maxHeight == 0) {
86 return new Point(maxWidth, maxHeight);
87 }
88 // Calculate desired display size based on scaling type, video aspect ratio,
89 // and maximum layout size.
90 final float frameAspect = frameWidth / (float) frameHeight;
91 final float displayAspect = maxWidth / (float) maxHeight;
magjeddf494b02016-10-07 05:32:35 -070092 final ScalingType scalingType = (frameAspect > 1.0f) == (displayAspect > 1.0f)
Magnus Jedvert62b1c352016-10-05 15:56:06 +020093 ? scalingTypeMatchOrientation
94 : scalingTypeMismatchOrientation;
magjeddf494b02016-10-07 05:32:35 -070095 final Point layoutSize = getDisplaySize(scalingType, frameAspect, maxWidth, maxHeight);
Magnus Jedvert62b1c352016-10-05 15:56:06 +020096
97 // If the measure specification is forcing a specific size - yield.
98 if (View.MeasureSpec.getMode(widthSpec) == View.MeasureSpec.EXACTLY) {
99 layoutSize.x = maxWidth;
100 }
101 if (View.MeasureSpec.getMode(heightSpec) == View.MeasureSpec.EXACTLY) {
102 layoutSize.y = maxHeight;
103 }
104 return layoutSize;
105 }
106 }
107
Magnus Jedvertff020c02015-08-20 14:03:07 +0200108 // Types of video scaling:
109 // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by
110 // maintaining the aspect ratio (black borders may be displayed).
111 // SCALE_ASPECT_FILL - video frame is scaled to fill the size of the view by
112 // maintaining the aspect ratio. Some portion of the video frame may be
113 // clipped.
114 // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will fill as much as
115 // possible of the view while maintaining aspect ratio, under the constraint that at least
116 // |BALANCED_VISIBLE_FRACTION| of the frame content will be shown.
117 public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_ASPECT_BALANCED }
118 // The minimum fraction of the frame content that will be shown for |SCALE_ASPECT_BALANCED|.
119 // This limits excessive cropping when adjusting display size.
Magnus Jedvert7230a212015-08-25 12:31:21 +0200120 private static float BALANCED_VISIBLE_FRACTION = 0.5625f;
sakalb6760f92016-09-29 04:12:44 -0700121 // clang-format off
Magnus Jedvertb4918da2018-07-05 13:36:10 +0200122 @Deprecated
Magnus Jedvert27551c92015-09-30 18:24:54 +0200123 public static final float[] identityMatrix() {
124 return new float[] {
125 1, 0, 0, 0,
126 0, 1, 0, 0,
127 0, 0, 1, 0,
128 0, 0, 0, 1};
129 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200130 // Matrix with transform y' = 1 - y.
Magnus Jedvertb4918da2018-07-05 13:36:10 +0200131 @Deprecated
Magnus Jedvert27551c92015-09-30 18:24:54 +0200132 public static final float[] verticalFlipMatrix() {
133 return new float[] {
134 1, 0, 0, 0,
135 0, -1, 0, 0,
136 0, 0, 1, 0,
137 0, 1, 0, 1};
138 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200139
perkj3d06eca2015-10-08 12:53:33 +0200140 // Matrix with transform x' = 1 - x.
Magnus Jedvertb4918da2018-07-05 13:36:10 +0200141 @Deprecated
perkj3d06eca2015-10-08 12:53:33 +0200142 public static final float[] horizontalFlipMatrix() {
143 return new float[] {
144 -1, 0, 0, 0,
145 0, 1, 0, 0,
146 0, 0, 1, 0,
147 1, 0, 0, 1};
148 }
sakalb6760f92016-09-29 04:12:44 -0700149 // clang-format on
perkj3d06eca2015-10-08 12:53:33 +0200150
Magnus Jedvertff020c02015-08-20 14:03:07 +0200151 /**
Magnus Jedvert27551c92015-09-30 18:24:54 +0200152 * Returns texture matrix that will have the effect of rotating the frame |rotationDegree|
153 * clockwise when rendered.
Magnus Jedvertff020c02015-08-20 14:03:07 +0200154 */
Magnus Jedvertb4918da2018-07-05 13:36:10 +0200155 @Deprecated
Magnus Jedvert27551c92015-09-30 18:24:54 +0200156 public static float[] rotateTextureMatrix(float[] textureMatrix, float rotationDegree) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200157 final float[] rotationMatrix = new float[16];
Magnus Jedvert8ce0bd52015-09-09 18:51:06 +0200158 Matrix.setRotateM(rotationMatrix, 0, rotationDegree, 0, 0, 1);
Magnus Jedvert529528c2015-09-09 10:59:46 +0200159 adjustOrigin(rotationMatrix);
Magnus Jedvert27551c92015-09-30 18:24:54 +0200160 return multiplyMatrices(textureMatrix, rotationMatrix);
161 }
162
163 /**
164 * Returns new matrix with the result of a * b.
165 */
Magnus Jedvertb4918da2018-07-05 13:36:10 +0200166 @Deprecated
Magnus Jedvert27551c92015-09-30 18:24:54 +0200167 public static float[] multiplyMatrices(float[] a, float[] b) {
168 final float[] resultMatrix = new float[16];
169 Matrix.multiplyMM(resultMatrix, 0, a, 0, b, 0);
170 return resultMatrix;
Magnus Jedvert529528c2015-09-09 10:59:46 +0200171 }
172
173 /**
174 * Returns layout transformation matrix that applies an optional mirror effect and compensates
175 * for video vs display aspect ratio.
176 */
177 public static float[] getLayoutMatrix(
178 boolean mirror, float videoAspectRatio, float displayAspectRatio) {
179 float scaleX = 1;
180 float scaleY = 1;
181 // Scale X or Y dimension so that video and display size have same aspect ratio.
182 if (displayAspectRatio > videoAspectRatio) {
183 scaleY = videoAspectRatio / displayAspectRatio;
184 } else {
185 scaleX = displayAspectRatio / videoAspectRatio;
186 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200187 // Apply optional horizontal flip.
188 if (mirror) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200189 scaleX *= -1;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200190 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200191 final float matrix[] = new float[16];
192 Matrix.setIdentityM(matrix, 0);
193 Matrix.scaleM(matrix, 0, scaleX, scaleY, 1);
194 adjustOrigin(matrix);
195 return matrix;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200196 }
197
Bjorn Mellem8fb23612017-07-18 11:33:39 -0700198 /** Converts a float[16] matrix array to android.graphics.Matrix. */
199 public static android.graphics.Matrix convertMatrixToAndroidGraphicsMatrix(float[] matrix4x4) {
200 // clang-format off
201 float[] values = {
202 matrix4x4[0 * 4 + 0], matrix4x4[1 * 4 + 0], matrix4x4[3 * 4 + 0],
203 matrix4x4[0 * 4 + 1], matrix4x4[1 * 4 + 1], matrix4x4[3 * 4 + 1],
204 matrix4x4[0 * 4 + 3], matrix4x4[1 * 4 + 3], matrix4x4[3 * 4 + 3],
205 };
206 // clang-format on
207
208 android.graphics.Matrix matrix = new android.graphics.Matrix();
209 matrix.setValues(values);
210 return matrix;
211 }
212
Bjorn Mellem0cf9a4a2017-07-18 13:19:26 -0700213 /** Converts android.graphics.Matrix to a float[16] matrix array. */
214 public static float[] convertMatrixFromAndroidGraphicsMatrix(android.graphics.Matrix matrix) {
215 float[] values = new float[9];
216 matrix.getValues(values);
217
218 // The android.graphics.Matrix looks like this:
219 // [x1 y1 w1]
220 // [x2 y2 w2]
221 // [x3 y3 w3]
222 // We want to contruct a matrix that looks like this:
223 // [x1 y1 0 w1]
224 // [x2 y2 0 w2]
225 // [ 0 0 1 0]
226 // [x3 y3 0 w3]
227 // Since it is stored in column-major order, it looks like this:
228 // [x1 x2 0 x3
229 // y1 y2 0 y3
230 // 0 0 1 0
231 // w1 w2 0 w3]
232 // clang-format off
233 float[] matrix4x4 = {
234 values[0 * 3 + 0], values[1 * 3 + 0], 0, values[2 * 3 + 0],
235 values[0 * 3 + 1], values[1 * 3 + 1], 0, values[2 * 3 + 1],
236 0, 0, 1, 0,
237 values[0 * 3 + 2], values[1 * 3 + 2], 0, values[2 * 3 + 2],
238 };
239 // clang-format on
240 return matrix4x4;
241 }
242
Magnus Jedvertff020c02015-08-20 14:03:07 +0200243 /**
244 * Calculate display size based on scaling type, video aspect ratio, and maximum display size.
245 */
sakalb6760f92016-09-29 04:12:44 -0700246 public static Point getDisplaySize(
247 ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200248 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio,
249 maxDisplayWidth, maxDisplayHeight);
250 }
251
252 /**
Magnus Jedvert529528c2015-09-09 10:59:46 +0200253 * Move |matrix| transformation origin to (0.5, 0.5). This is the origin for texture coordinates
254 * that are in the range 0 to 1.
255 */
256 private static void adjustOrigin(float[] matrix) {
257 // Note that OpenGL is using column-major order.
258 // Pre translate with -0.5 to move coordinates to range [-0.5, 0.5].
259 matrix[12] -= 0.5f * (matrix[0] + matrix[4]);
260 matrix[13] -= 0.5f * (matrix[1] + matrix[5]);
261 // Post translate with 0.5 to move coordinates to range [0, 1].
262 matrix[12] += 0.5f;
263 matrix[13] += 0.5f;
264 }
265
266 /**
Magnus Jedvertff020c02015-08-20 14:03:07 +0200267 * Each scaling type has a one-to-one correspondence to a numeric minimum fraction of the video
268 * that must remain visible.
269 */
270 private static float convertScalingTypeToVisibleFraction(ScalingType scalingType) {
271 switch (scalingType) {
272 case SCALE_ASPECT_FIT:
273 return 1.0f;
274 case SCALE_ASPECT_FILL:
275 return 0.0f;
276 case SCALE_ASPECT_BALANCED:
277 return BALANCED_VISIBLE_FRACTION;
278 default:
279 throw new IllegalArgumentException();
280 }
281 }
282
283 /**
284 * Calculate display size based on minimum fraction of the video that must remain visible,
285 * video aspect ratio, and maximum display size.
286 */
sakalb6760f92016-09-29 04:12:44 -0700287 private static Point getDisplaySize(
288 float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200289 // If there is no constraint on the amount of cropping, fill the allowed display area.
290 if (minVisibleFraction == 0 || videoAspectRatio == 0) {
291 return new Point(maxDisplayWidth, maxDisplayHeight);
292 }
293 // Each dimension is constrained on max display size and how much we are allowed to crop.
sakalb6760f92016-09-29 04:12:44 -0700294 final int width = Math.min(
295 maxDisplayWidth, Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio));
296 final int height = Math.min(
297 maxDisplayHeight, Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio));
Magnus Jedvertff020c02015-08-20 14:03:07 +0200298 return new Point(width, height);
299 }
300}