blob: ee73430373ef179d16dff1efef839baa926462a1 [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 Jedvert27551c92015-09-30 18:24:54 +0200122 public static final float[] identityMatrix() {
123 return new float[] {
124 1, 0, 0, 0,
125 0, 1, 0, 0,
126 0, 0, 1, 0,
127 0, 0, 0, 1};
128 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200129 // Matrix with transform y' = 1 - y.
Magnus Jedvert27551c92015-09-30 18:24:54 +0200130 public static final float[] verticalFlipMatrix() {
131 return new float[] {
132 1, 0, 0, 0,
133 0, -1, 0, 0,
134 0, 0, 1, 0,
135 0, 1, 0, 1};
136 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200137
perkj3d06eca2015-10-08 12:53:33 +0200138 // Matrix with transform x' = 1 - x.
139 public static final float[] horizontalFlipMatrix() {
140 return new float[] {
141 -1, 0, 0, 0,
142 0, 1, 0, 0,
143 0, 0, 1, 0,
144 1, 0, 0, 1};
145 }
sakalb6760f92016-09-29 04:12:44 -0700146 // clang-format on
perkj3d06eca2015-10-08 12:53:33 +0200147
Magnus Jedvertff020c02015-08-20 14:03:07 +0200148 /**
Magnus Jedvert27551c92015-09-30 18:24:54 +0200149 * Returns texture matrix that will have the effect of rotating the frame |rotationDegree|
150 * clockwise when rendered.
Magnus Jedvertff020c02015-08-20 14:03:07 +0200151 */
Magnus Jedvert27551c92015-09-30 18:24:54 +0200152 public static float[] rotateTextureMatrix(float[] textureMatrix, float rotationDegree) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200153 final float[] rotationMatrix = new float[16];
Magnus Jedvert8ce0bd52015-09-09 18:51:06 +0200154 Matrix.setRotateM(rotationMatrix, 0, rotationDegree, 0, 0, 1);
Magnus Jedvert529528c2015-09-09 10:59:46 +0200155 adjustOrigin(rotationMatrix);
Magnus Jedvert27551c92015-09-30 18:24:54 +0200156 return multiplyMatrices(textureMatrix, rotationMatrix);
157 }
158
159 /**
160 * Returns new matrix with the result of a * b.
161 */
162 public static float[] multiplyMatrices(float[] a, float[] b) {
163 final float[] resultMatrix = new float[16];
164 Matrix.multiplyMM(resultMatrix, 0, a, 0, b, 0);
165 return resultMatrix;
Magnus Jedvert529528c2015-09-09 10:59:46 +0200166 }
167
168 /**
169 * Returns layout transformation matrix that applies an optional mirror effect and compensates
170 * for video vs display aspect ratio.
171 */
172 public static float[] getLayoutMatrix(
173 boolean mirror, float videoAspectRatio, float displayAspectRatio) {
174 float scaleX = 1;
175 float scaleY = 1;
176 // Scale X or Y dimension so that video and display size have same aspect ratio.
177 if (displayAspectRatio > videoAspectRatio) {
178 scaleY = videoAspectRatio / displayAspectRatio;
179 } else {
180 scaleX = displayAspectRatio / videoAspectRatio;
181 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200182 // Apply optional horizontal flip.
183 if (mirror) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200184 scaleX *= -1;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200185 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200186 final float matrix[] = new float[16];
187 Matrix.setIdentityM(matrix, 0);
188 Matrix.scaleM(matrix, 0, scaleX, scaleY, 1);
189 adjustOrigin(matrix);
190 return matrix;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200191 }
192
Bjorn Mellem8fb23612017-07-18 11:33:39 -0700193 /** Converts a float[16] matrix array to android.graphics.Matrix. */
194 public static android.graphics.Matrix convertMatrixToAndroidGraphicsMatrix(float[] matrix4x4) {
195 // clang-format off
196 float[] values = {
197 matrix4x4[0 * 4 + 0], matrix4x4[1 * 4 + 0], matrix4x4[3 * 4 + 0],
198 matrix4x4[0 * 4 + 1], matrix4x4[1 * 4 + 1], matrix4x4[3 * 4 + 1],
199 matrix4x4[0 * 4 + 3], matrix4x4[1 * 4 + 3], matrix4x4[3 * 4 + 3],
200 };
201 // clang-format on
202
203 android.graphics.Matrix matrix = new android.graphics.Matrix();
204 matrix.setValues(values);
205 return matrix;
206 }
207
Bjorn Mellem0cf9a4a2017-07-18 13:19:26 -0700208 /** Converts android.graphics.Matrix to a float[16] matrix array. */
209 public static float[] convertMatrixFromAndroidGraphicsMatrix(android.graphics.Matrix matrix) {
210 float[] values = new float[9];
211 matrix.getValues(values);
212
213 // The android.graphics.Matrix looks like this:
214 // [x1 y1 w1]
215 // [x2 y2 w2]
216 // [x3 y3 w3]
217 // We want to contruct a matrix that looks like this:
218 // [x1 y1 0 w1]
219 // [x2 y2 0 w2]
220 // [ 0 0 1 0]
221 // [x3 y3 0 w3]
222 // Since it is stored in column-major order, it looks like this:
223 // [x1 x2 0 x3
224 // y1 y2 0 y3
225 // 0 0 1 0
226 // w1 w2 0 w3]
227 // clang-format off
228 float[] matrix4x4 = {
229 values[0 * 3 + 0], values[1 * 3 + 0], 0, values[2 * 3 + 0],
230 values[0 * 3 + 1], values[1 * 3 + 1], 0, values[2 * 3 + 1],
231 0, 0, 1, 0,
232 values[0 * 3 + 2], values[1 * 3 + 2], 0, values[2 * 3 + 2],
233 };
234 // clang-format on
235 return matrix4x4;
236 }
237
Magnus Jedvertff020c02015-08-20 14:03:07 +0200238 /**
239 * Calculate display size based on scaling type, video aspect ratio, and maximum display size.
240 */
sakalb6760f92016-09-29 04:12:44 -0700241 public static Point getDisplaySize(
242 ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200243 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio,
244 maxDisplayWidth, maxDisplayHeight);
245 }
246
247 /**
Magnus Jedvert529528c2015-09-09 10:59:46 +0200248 * Move |matrix| transformation origin to (0.5, 0.5). This is the origin for texture coordinates
249 * that are in the range 0 to 1.
250 */
251 private static void adjustOrigin(float[] matrix) {
252 // Note that OpenGL is using column-major order.
253 // Pre translate with -0.5 to move coordinates to range [-0.5, 0.5].
254 matrix[12] -= 0.5f * (matrix[0] + matrix[4]);
255 matrix[13] -= 0.5f * (matrix[1] + matrix[5]);
256 // Post translate with 0.5 to move coordinates to range [0, 1].
257 matrix[12] += 0.5f;
258 matrix[13] += 0.5f;
259 }
260
261 /**
Magnus Jedvertff020c02015-08-20 14:03:07 +0200262 * Each scaling type has a one-to-one correspondence to a numeric minimum fraction of the video
263 * that must remain visible.
264 */
265 private static float convertScalingTypeToVisibleFraction(ScalingType scalingType) {
266 switch (scalingType) {
267 case SCALE_ASPECT_FIT:
268 return 1.0f;
269 case SCALE_ASPECT_FILL:
270 return 0.0f;
271 case SCALE_ASPECT_BALANCED:
272 return BALANCED_VISIBLE_FRACTION;
273 default:
274 throw new IllegalArgumentException();
275 }
276 }
277
278 /**
279 * Calculate display size based on minimum fraction of the video that must remain visible,
280 * video aspect ratio, and maximum display size.
281 */
sakalb6760f92016-09-29 04:12:44 -0700282 private static Point getDisplaySize(
283 float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200284 // If there is no constraint on the amount of cropping, fill the allowed display area.
285 if (minVisibleFraction == 0 || videoAspectRatio == 0) {
286 return new Point(maxDisplayWidth, maxDisplayHeight);
287 }
288 // Each dimension is constrained on max display size and how much we are allowed to crop.
sakalb6760f92016-09-29 04:12:44 -0700289 final int width = Math.min(
290 maxDisplayWidth, Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio));
291 final int height = Math.min(
292 maxDisplayHeight, Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio));
Magnus Jedvertff020c02015-08-20 14:03:07 +0200293 return new Point(width, height);
294 }
295}