blob: b97901c634fce8cf768d5e9b7ebce60cd7ebc1fb [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.
Magnus Jedvertfcf3a872019-06-28 13:34:21 +020067 private float visibleFractionMatchOrientation =
68 convertScalingTypeToVisibleFraction(ScalingType.SCALE_ASPECT_BALANCED);
69 private float visibleFractionMismatchOrientation =
70 convertScalingTypeToVisibleFraction(ScalingType.SCALE_ASPECT_BALANCED);
Magnus Jedvert62b1c352016-10-05 15:56:06 +020071
72 public void setScalingType(ScalingType scalingType) {
Magnus Jedvertfcf3a872019-06-28 13:34:21 +020073 setScalingType(/* scalingTypeMatchOrientation= */ scalingType,
74 /* scalingTypeMismatchOrientation= */ scalingType);
Magnus Jedvert62b1c352016-10-05 15:56:06 +020075 }
76
77 public void setScalingType(
78 ScalingType scalingTypeMatchOrientation, ScalingType scalingTypeMismatchOrientation) {
Magnus Jedvertfcf3a872019-06-28 13:34:21 +020079 this.visibleFractionMatchOrientation =
80 convertScalingTypeToVisibleFraction(scalingTypeMatchOrientation);
81 this.visibleFractionMismatchOrientation =
82 convertScalingTypeToVisibleFraction(scalingTypeMismatchOrientation);
83 }
84
85 public void setVisibleFraction(
86 float visibleFractionMatchOrientation, float visibleFractionMismatchOrientation) {
87 this.visibleFractionMatchOrientation = visibleFractionMatchOrientation;
88 this.visibleFractionMismatchOrientation = visibleFractionMismatchOrientation;
Magnus Jedvert62b1c352016-10-05 15:56:06 +020089 }
90
91 public Point measure(int widthSpec, int heightSpec, int frameWidth, int frameHeight) {
92 // Calculate max allowed layout size.
93 final int maxWidth = View.getDefaultSize(Integer.MAX_VALUE, widthSpec);
94 final int maxHeight = View.getDefaultSize(Integer.MAX_VALUE, heightSpec);
95 if (frameWidth == 0 || frameHeight == 0 || maxWidth == 0 || maxHeight == 0) {
96 return new Point(maxWidth, maxHeight);
97 }
98 // Calculate desired display size based on scaling type, video aspect ratio,
99 // and maximum layout size.
100 final float frameAspect = frameWidth / (float) frameHeight;
101 final float displayAspect = maxWidth / (float) maxHeight;
Magnus Jedvertfcf3a872019-06-28 13:34:21 +0200102 final float visibleFraction = (frameAspect > 1.0f) == (displayAspect > 1.0f)
103 ? visibleFractionMatchOrientation
104 : visibleFractionMismatchOrientation;
105 final Point layoutSize = getDisplaySize(visibleFraction, frameAspect, maxWidth, maxHeight);
Magnus Jedvert62b1c352016-10-05 15:56:06 +0200106
107 // If the measure specification is forcing a specific size - yield.
108 if (View.MeasureSpec.getMode(widthSpec) == View.MeasureSpec.EXACTLY) {
109 layoutSize.x = maxWidth;
110 }
111 if (View.MeasureSpec.getMode(heightSpec) == View.MeasureSpec.EXACTLY) {
112 layoutSize.y = maxHeight;
113 }
114 return layoutSize;
115 }
116 }
117
Magnus Jedvertff020c02015-08-20 14:03:07 +0200118 // Types of video scaling:
119 // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by
120 // maintaining the aspect ratio (black borders may be displayed).
121 // SCALE_ASPECT_FILL - video frame is scaled to fill the size of the view by
122 // maintaining the aspect ratio. Some portion of the video frame may be
123 // clipped.
124 // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will fill as much as
125 // possible of the view while maintaining aspect ratio, under the constraint that at least
Artem Titovd7ac5812021-07-27 12:23:39 +0200126 // `BALANCED_VISIBLE_FRACTION` of the frame content will be shown.
Magnus Jedvertff020c02015-08-20 14:03:07 +0200127 public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_ASPECT_BALANCED }
Artem Titovd7ac5812021-07-27 12:23:39 +0200128 // The minimum fraction of the frame content that will be shown for `SCALE_ASPECT_BALANCED`.
Magnus Jedvertff020c02015-08-20 14:03:07 +0200129 // This limits excessive cropping when adjusting display size.
Magnus Jedvert7230a212015-08-25 12:31:21 +0200130 private static float BALANCED_VISIBLE_FRACTION = 0.5625f;
Magnus Jedvert529528c2015-09-09 10:59:46 +0200131
132 /**
133 * Returns layout transformation matrix that applies an optional mirror effect and compensates
134 * for video vs display aspect ratio.
135 */
136 public static float[] getLayoutMatrix(
137 boolean mirror, float videoAspectRatio, float displayAspectRatio) {
138 float scaleX = 1;
139 float scaleY = 1;
140 // Scale X or Y dimension so that video and display size have same aspect ratio.
141 if (displayAspectRatio > videoAspectRatio) {
142 scaleY = videoAspectRatio / displayAspectRatio;
143 } else {
144 scaleX = displayAspectRatio / videoAspectRatio;
145 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200146 // Apply optional horizontal flip.
147 if (mirror) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200148 scaleX *= -1;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200149 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200150 final float matrix[] = new float[16];
151 Matrix.setIdentityM(matrix, 0);
152 Matrix.scaleM(matrix, 0, scaleX, scaleY, 1);
153 adjustOrigin(matrix);
154 return matrix;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200155 }
156
Bjorn Mellem8fb23612017-07-18 11:33:39 -0700157 /** Converts a float[16] matrix array to android.graphics.Matrix. */
158 public static android.graphics.Matrix convertMatrixToAndroidGraphicsMatrix(float[] matrix4x4) {
159 // clang-format off
160 float[] values = {
161 matrix4x4[0 * 4 + 0], matrix4x4[1 * 4 + 0], matrix4x4[3 * 4 + 0],
162 matrix4x4[0 * 4 + 1], matrix4x4[1 * 4 + 1], matrix4x4[3 * 4 + 1],
163 matrix4x4[0 * 4 + 3], matrix4x4[1 * 4 + 3], matrix4x4[3 * 4 + 3],
164 };
165 // clang-format on
166
167 android.graphics.Matrix matrix = new android.graphics.Matrix();
168 matrix.setValues(values);
169 return matrix;
170 }
171
Bjorn Mellem0cf9a4a2017-07-18 13:19:26 -0700172 /** Converts android.graphics.Matrix to a float[16] matrix array. */
173 public static float[] convertMatrixFromAndroidGraphicsMatrix(android.graphics.Matrix matrix) {
174 float[] values = new float[9];
175 matrix.getValues(values);
176
177 // The android.graphics.Matrix looks like this:
178 // [x1 y1 w1]
179 // [x2 y2 w2]
180 // [x3 y3 w3]
181 // We want to contruct a matrix that looks like this:
182 // [x1 y1 0 w1]
183 // [x2 y2 0 w2]
184 // [ 0 0 1 0]
185 // [x3 y3 0 w3]
186 // Since it is stored in column-major order, it looks like this:
187 // [x1 x2 0 x3
188 // y1 y2 0 y3
189 // 0 0 1 0
190 // w1 w2 0 w3]
191 // clang-format off
192 float[] matrix4x4 = {
193 values[0 * 3 + 0], values[1 * 3 + 0], 0, values[2 * 3 + 0],
194 values[0 * 3 + 1], values[1 * 3 + 1], 0, values[2 * 3 + 1],
195 0, 0, 1, 0,
196 values[0 * 3 + 2], values[1 * 3 + 2], 0, values[2 * 3 + 2],
197 };
198 // clang-format on
199 return matrix4x4;
200 }
201
Magnus Jedvertff020c02015-08-20 14:03:07 +0200202 /**
203 * Calculate display size based on scaling type, video aspect ratio, and maximum display size.
204 */
sakalb6760f92016-09-29 04:12:44 -0700205 public static Point getDisplaySize(
206 ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200207 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio,
208 maxDisplayWidth, maxDisplayHeight);
209 }
210
211 /**
Artem Titovd7ac5812021-07-27 12:23:39 +0200212 * Move `matrix` transformation origin to (0.5, 0.5). This is the origin for texture coordinates
Magnus Jedvert529528c2015-09-09 10:59:46 +0200213 * that are in the range 0 to 1.
214 */
215 private static void adjustOrigin(float[] matrix) {
216 // Note that OpenGL is using column-major order.
217 // Pre translate with -0.5 to move coordinates to range [-0.5, 0.5].
218 matrix[12] -= 0.5f * (matrix[0] + matrix[4]);
219 matrix[13] -= 0.5f * (matrix[1] + matrix[5]);
220 // Post translate with 0.5 to move coordinates to range [0, 1].
221 matrix[12] += 0.5f;
222 matrix[13] += 0.5f;
223 }
224
225 /**
Magnus Jedvertff020c02015-08-20 14:03:07 +0200226 * Each scaling type has a one-to-one correspondence to a numeric minimum fraction of the video
227 * that must remain visible.
228 */
229 private static float convertScalingTypeToVisibleFraction(ScalingType scalingType) {
230 switch (scalingType) {
231 case SCALE_ASPECT_FIT:
232 return 1.0f;
233 case SCALE_ASPECT_FILL:
234 return 0.0f;
235 case SCALE_ASPECT_BALANCED:
236 return BALANCED_VISIBLE_FRACTION;
237 default:
238 throw new IllegalArgumentException();
239 }
240 }
241
242 /**
243 * Calculate display size based on minimum fraction of the video that must remain visible,
244 * video aspect ratio, and maximum display size.
245 */
Magnus Jedvert7dd99692019-06-26 09:12:48 +0200246 public static Point getDisplaySize(
sakalb6760f92016-09-29 04:12:44 -0700247 float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200248 // If there is no constraint on the amount of cropping, fill the allowed display area.
249 if (minVisibleFraction == 0 || videoAspectRatio == 0) {
250 return new Point(maxDisplayWidth, maxDisplayHeight);
251 }
252 // Each dimension is constrained on max display size and how much we are allowed to crop.
sakalb6760f92016-09-29 04:12:44 -0700253 final int width = Math.min(
254 maxDisplayWidth, Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio));
255 final int height = Math.min(
256 maxDisplayHeight, Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio));
Magnus Jedvertff020c02015-08-20 14:03:07 +0200257 return new Point(width, height);
258 }
259}