blob: f039a06f29d86315fa2865bdb92e3c082654643c [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;
Magnus Jedvert529528c2015-09-09 10:59:46 +0200121
122 /**
123 * Returns layout transformation matrix that applies an optional mirror effect and compensates
124 * for video vs display aspect ratio.
125 */
126 public static float[] getLayoutMatrix(
127 boolean mirror, float videoAspectRatio, float displayAspectRatio) {
128 float scaleX = 1;
129 float scaleY = 1;
130 // Scale X or Y dimension so that video and display size have same aspect ratio.
131 if (displayAspectRatio > videoAspectRatio) {
132 scaleY = videoAspectRatio / displayAspectRatio;
133 } else {
134 scaleX = displayAspectRatio / videoAspectRatio;
135 }
Magnus Jedvertff020c02015-08-20 14:03:07 +0200136 // Apply optional horizontal flip.
137 if (mirror) {
Magnus Jedvert529528c2015-09-09 10:59:46 +0200138 scaleX *= -1;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200139 }
Magnus Jedvert529528c2015-09-09 10:59:46 +0200140 final float matrix[] = new float[16];
141 Matrix.setIdentityM(matrix, 0);
142 Matrix.scaleM(matrix, 0, scaleX, scaleY, 1);
143 adjustOrigin(matrix);
144 return matrix;
Magnus Jedvertff020c02015-08-20 14:03:07 +0200145 }
146
Bjorn Mellem8fb23612017-07-18 11:33:39 -0700147 /** Converts a float[16] matrix array to android.graphics.Matrix. */
148 public static android.graphics.Matrix convertMatrixToAndroidGraphicsMatrix(float[] matrix4x4) {
149 // clang-format off
150 float[] values = {
151 matrix4x4[0 * 4 + 0], matrix4x4[1 * 4 + 0], matrix4x4[3 * 4 + 0],
152 matrix4x4[0 * 4 + 1], matrix4x4[1 * 4 + 1], matrix4x4[3 * 4 + 1],
153 matrix4x4[0 * 4 + 3], matrix4x4[1 * 4 + 3], matrix4x4[3 * 4 + 3],
154 };
155 // clang-format on
156
157 android.graphics.Matrix matrix = new android.graphics.Matrix();
158 matrix.setValues(values);
159 return matrix;
160 }
161
Bjorn Mellem0cf9a4a2017-07-18 13:19:26 -0700162 /** Converts android.graphics.Matrix to a float[16] matrix array. */
163 public static float[] convertMatrixFromAndroidGraphicsMatrix(android.graphics.Matrix matrix) {
164 float[] values = new float[9];
165 matrix.getValues(values);
166
167 // The android.graphics.Matrix looks like this:
168 // [x1 y1 w1]
169 // [x2 y2 w2]
170 // [x3 y3 w3]
171 // We want to contruct a matrix that looks like this:
172 // [x1 y1 0 w1]
173 // [x2 y2 0 w2]
174 // [ 0 0 1 0]
175 // [x3 y3 0 w3]
176 // Since it is stored in column-major order, it looks like this:
177 // [x1 x2 0 x3
178 // y1 y2 0 y3
179 // 0 0 1 0
180 // w1 w2 0 w3]
181 // clang-format off
182 float[] matrix4x4 = {
183 values[0 * 3 + 0], values[1 * 3 + 0], 0, values[2 * 3 + 0],
184 values[0 * 3 + 1], values[1 * 3 + 1], 0, values[2 * 3 + 1],
185 0, 0, 1, 0,
186 values[0 * 3 + 2], values[1 * 3 + 2], 0, values[2 * 3 + 2],
187 };
188 // clang-format on
189 return matrix4x4;
190 }
191
Magnus Jedvertff020c02015-08-20 14:03:07 +0200192 /**
193 * Calculate display size based on scaling type, video aspect ratio, and maximum display size.
194 */
sakalb6760f92016-09-29 04:12:44 -0700195 public static Point getDisplaySize(
196 ScalingType scalingType, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200197 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), videoAspectRatio,
198 maxDisplayWidth, maxDisplayHeight);
199 }
200
201 /**
Magnus Jedvert529528c2015-09-09 10:59:46 +0200202 * Move |matrix| transformation origin to (0.5, 0.5). This is the origin for texture coordinates
203 * that are in the range 0 to 1.
204 */
205 private static void adjustOrigin(float[] matrix) {
206 // Note that OpenGL is using column-major order.
207 // Pre translate with -0.5 to move coordinates to range [-0.5, 0.5].
208 matrix[12] -= 0.5f * (matrix[0] + matrix[4]);
209 matrix[13] -= 0.5f * (matrix[1] + matrix[5]);
210 // Post translate with 0.5 to move coordinates to range [0, 1].
211 matrix[12] += 0.5f;
212 matrix[13] += 0.5f;
213 }
214
215 /**
Magnus Jedvertff020c02015-08-20 14:03:07 +0200216 * Each scaling type has a one-to-one correspondence to a numeric minimum fraction of the video
217 * that must remain visible.
218 */
219 private static float convertScalingTypeToVisibleFraction(ScalingType scalingType) {
220 switch (scalingType) {
221 case SCALE_ASPECT_FIT:
222 return 1.0f;
223 case SCALE_ASPECT_FILL:
224 return 0.0f;
225 case SCALE_ASPECT_BALANCED:
226 return BALANCED_VISIBLE_FRACTION;
227 default:
228 throw new IllegalArgumentException();
229 }
230 }
231
232 /**
233 * Calculate display size based on minimum fraction of the video that must remain visible,
234 * video aspect ratio, and maximum display size.
235 */
sakalb6760f92016-09-29 04:12:44 -0700236 private static Point getDisplaySize(
237 float minVisibleFraction, float videoAspectRatio, int maxDisplayWidth, int maxDisplayHeight) {
Magnus Jedvertff020c02015-08-20 14:03:07 +0200238 // If there is no constraint on the amount of cropping, fill the allowed display area.
239 if (minVisibleFraction == 0 || videoAspectRatio == 0) {
240 return new Point(maxDisplayWidth, maxDisplayHeight);
241 }
242 // Each dimension is constrained on max display size and how much we are allowed to crop.
sakalb6760f92016-09-29 04:12:44 -0700243 final int width = Math.min(
244 maxDisplayWidth, Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio));
245 final int height = Math.min(
246 maxDisplayHeight, Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio));
Magnus Jedvertff020c02015-08-20 14:03:07 +0200247 return new Point(width, height);
248 }
249}