blob: 0554f119c3af843e3cec96b8791c87134e18ebf6 [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 Jedvert51254332015-12-15 16:22:29 +010034 /** 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 */
ivoc1aa435c2016-05-04 07:14:14 -070041 void drawOes(int oesTextureId, float[] texMatrix, int frameWidth, int frameHeight,
42 int viewportX, int viewportY, int viewportWidth, int viewportHeight);
sakalb6760f92016-09-29 04:12:44 -070043 void drawRgb(int textureId, float[] texMatrix, int frameWidth, int frameHeight, int viewportX,
44 int viewportY, int viewportWidth, int viewportHeight);
ivoc1aa435c2016-05-04 07:14:14 -070045 void drawYuv(int[] yuvTextures, float[] texMatrix, int frameWidth, int frameHeight,
46 int viewportX, int viewportY, int viewportWidth, int viewportHeight);
Magnus Jedvert51254332015-12-15 16:22:29 +010047
sakal160e32c2017-08-28 02:41:38 -070048 /**
sakal9bc599f2017-09-08 04:46:33 -070049 * Release all GL resources. This needs to be done manually, otherwise resources may leak.
sakal160e32c2017-08-28 02:41:38 -070050 */
sakal9bc599f2017-09-08 04:46:33 -070051 void release();
52 }
sakal6bdcefc2017-08-15 01:56:02 -070053
sakal9bc599f2017-09-08 04:46:33 -070054 /**
Magnus Jedvert62b1c352016-10-05 15:56:06 +020055 * 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;
magjeddf494b02016-10-07 05:32:35 -070087 final ScalingType scalingType = (frameAspect > 1.0f) == (displayAspect > 1.0f)
Magnus Jedvert62b1c352016-10-05 15:56:06 +020088 ? scalingTypeMatchOrientation
89 : scalingTypeMismatchOrientation;
magjeddf494b02016-10-07 05:32:35 -070090 final Point layoutSize = getDisplaySize(scalingType, frameAspect, maxWidth, maxHeight);
Magnus Jedvert62b1c352016-10-05 15:56:06 +020091
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 Jedvertff020c02015-08-20 14:03:07 +0200103 // 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 Jedvert7230a212015-08-25 12:31:21 +0200115 private static float BALANCED_VISIBLE_FRACTION = 0.5625f;
sakalb6760f92016-09-29 04:12:44 -0700116 // clang-format off
Magnus Jedvert27551c92015-09-30 18:24:54 +0200117 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 Jedvert529528c2015-09-09 10:59:46 +0200124 // Matrix with transform y' = 1 - y.
Magnus Jedvert27551c92015-09-30 18:24:54 +0200125 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 Jedvertff020c02015-08-20 14:03:07 +0200132
perkj3d06eca2015-10-08 12:53:33 +0200133 // 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 }
sakalb6760f92016-09-29 04:12:44 -0700141 // clang-format on
perkj3d06eca2015-10-08 12:53:33 +0200142
Magnus Jedvertff020c02015-08-20 14:03:07 +0200143 /**
Magnus Jedvert27551c92015-09-30 18:24:54 +0200144 * Returns texture matrix that will have the effect of rotating the frame |rotationDegree|
145 * clockwise when rendered.
Magnus Jedvertff020c02015-08-20 14:03:07 +0200146 */
Magnus Jedvert27551c92015-09-30 18:24:54 +0200147 public static float[] rotateTextureMatrix(float[] textureMatrix, float rotationDegree) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200148 final float[] rotationMatrix = new float[16];
Magnus Jedvert8ce0bd52015-09-09 18:51:06 +0200149 Matrix.setRotateM(rotationMatrix, 0, rotationDegree, 0, 0, 1);
Magnus Jedvert529528c2015-09-09 10:59:46 +0200150 adjustOrigin(rotationMatrix);
Magnus Jedvert27551c92015-09-30 18:24:54 +0200151 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 Jedvert529528c2015-09-09 10:59:46 +0200161 }
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 Jedvertff020c02015-08-20 14:03:07 +0200177 // Apply optional horizontal flip.
178 if (mirror) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200179 scaleX *= -1;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200180 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200181 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 Jedvertff020c02015-08-20 14:03:07 +0200186 }
187
Bjorn Mellem8fb23612017-07-18 11:33:39 -0700188 /** 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 Mellem0cf9a4a2017-07-18 13:19:26 -0700203 /** 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 Jedvertff020c02015-08-20 14:03:07 +0200233 /**
234 * Calculate display size based on scaling type, video aspect ratio, and maximum display size.
235 */
sakalb6760f92016-09-29 04:12:44 -0700236 public static Point getDisplaySize(
237 ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200238 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio,
239 maxDisplayWidth, maxDisplayHeight);
240 }
241
242 /**
Magnus Jedvert529528c2015-09-09 10:59:46 +0200243 * 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 Jedvertff020c02015-08-20 14:03:07 +0200257 * 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 */
sakalb6760f92016-09-29 04:12:44 -0700277 private static Point getDisplaySize(
278 float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200279 // 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.
sakalb6760f92016-09-29 04:12:44 -0700284 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 Jedvertff020c02015-08-20 14:03:07 +0200288 return new Point(width, height);
289 }
290}