OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * 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. | |
9 */ | |
10 | |
11 package org.webrtc; | |
12 | |
13 import android.graphics.Matrix; | |
14 import android.graphics.Point; | |
15 | |
16 /** | |
17 * Helper class to draw VideoFrames. Calls either drawer.drawOes, drawer.drawRgb , or | |
18 * drawer.drawYuv depending on the type of the buffer. The frame will be rendere d with rotation | |
19 * taken into account. You can supply an additional render matrix for custom tra nsformations. | |
20 */ | |
21 public class VideoFrameDrawer { | |
22 private static int distance(float x0, float y0, float x1, float y1) { | |
23 return (int) Math.round(Math.hypot(x1 - x0, y1 - y0)); | |
24 } | |
25 | |
26 // These points are used to calculate the size of the part of the frame we are rendering. | |
27 final static float[] srcPoints = | |
28 new float[] {0f /* x0 */, 0f /* y0 */, 1f /* x1 */, 0f /* y1 */, 0f /* x2 */, 1f /* y2 */}; | |
29 private final float[] dstPoints = new float[6]; | |
30 private final Point renderSize = new Point(); | |
31 | |
32 // Get the frame size after |renderMatrix| is applied. | |
33 private Point getTransformedSize(int frameWidth, int frameHeight, Matrix rende rMatrix) { | |
34 if (renderMatrix == null) { | |
35 renderSize.x = frameWidth; | |
36 renderSize.y = frameHeight; | |
37 return renderSize; | |
38 } | |
39 // Transform the texture coordinates (in the range [0, 1]) according to |ren derMatrix|. | |
40 renderMatrix.mapPoints(dstPoints, srcPoints); | |
41 | |
42 // Multiply with the width and height to get the positions in terms of pixel s. | |
43 for (int i = 0; i < 3; ++i) { | |
44 dstPoints[i * 2 + 0] *= frameWidth; | |
45 dstPoints[i * 2 + 1] *= frameHeight; | |
46 } | |
47 | |
48 // Get the length of the sides of the transformed rectangle in terms of pixe ls. | |
49 renderSize.x = distance(dstPoints[0], dstPoints[1], dstPoints[2], dstPoints[ 3]); | |
50 renderSize.y = distance(dstPoints[0], dstPoints[1], dstPoints[4], dstPoints[ 5]); | |
51 return renderSize; | |
52 } | |
53 | |
54 private final RendererCommon.YuvUploader yuvUploader = new RendererCommon.YuvU ploader(); | |
55 // This variable will only be used for checking reference equality and is used for caching I420 | |
56 // textures. | |
57 private VideoFrame lastI420Frame; | |
58 private final Matrix renderMatrix = new Matrix(); | |
59 | |
60 public void drawFrame(VideoFrame frame, RendererCommon.GlDrawer drawer) { | |
61 drawFrame(frame, drawer, null /* additionalRenderMatrix */); | |
62 } | |
63 | |
64 public void drawFrame( | |
65 VideoFrame frame, RendererCommon.GlDrawer drawer, Matrix additionalRenderM atrix) { | |
66 drawFrame(frame, drawer, additionalRenderMatrix, 0 /* viewportX */, 0 /* vie wportY */, | |
67 frame.getRotatedWidth(), frame.getRotatedHeight()); | |
68 } | |
69 | |
70 public void drawFrame(VideoFrame frame, RendererCommon.GlDrawer drawer, | |
71 Matrix additionalRenderMatrix, int viewportX, int viewportY, int viewportW idth, | |
72 int viewportHeight) { | |
73 final int width = frame.getRotatedWidth(); | |
74 final int height = frame.getRotatedHeight(); | |
75 | |
76 final Point renderSize = getTransformedSize(width, height, additionalRenderM atrix); | |
77 | |
78 final boolean isTextureFrame = frame.getBuffer() instanceof VideoFrame.Textu reBuffer; | |
79 renderMatrix.reset(); | |
80 renderMatrix.preTranslate(0.5f, 0.5f); | |
81 if (!isTextureFrame) { | |
82 renderMatrix.preScale(1f, -1f); // I420-frames are upside down | |
83 } | |
84 renderMatrix.preRotate(frame.getRotation()); | |
85 renderMatrix.preTranslate(-0.5f, -0.5f); | |
86 if (additionalRenderMatrix != null) { | |
87 renderMatrix.preConcat(additionalRenderMatrix); | |
88 } | |
89 | |
90 if (isTextureFrame) { | |
91 lastI420Frame = null; | |
92 RendererCommon.drawTexture(drawer, (VideoFrame.TextureBuffer) frame.getBuf fer(), renderMatrix, | |
sakal
2017/09/11 09:05:58
Maybe we should move RendererCommon.drawTexture to
magjed_webrtc
2017/09/11 12:45:48
Done, I also moved YuvUploader.
| |
93 renderSize.x, renderSize.y, viewportX, viewportY, viewportWidth, viewp ortHeight); | |
94 } else { | |
95 // Only upload the I420 data to textures once per frame, if we are called multiple times | |
96 // with the same frame. | |
97 if (frame != lastI420Frame) { | |
98 lastI420Frame = frame; | |
99 final VideoFrame.I420Buffer i420Buffer = frame.getBuffer().toI420(); | |
100 yuvUploader.uploadFromBuffer(i420Buffer); | |
101 i420Buffer.release(); | |
102 } | |
103 | |
104 drawer.drawYuv(yuvUploader.getYuvTextures(), | |
105 RendererCommon.convertMatrixFromAndroidGraphicsMatrix(renderMatrix), r enderSize.x, | |
106 renderSize.y, viewportX, viewportY, viewportWidth, viewportHeight); | |
107 } | |
108 } | |
109 | |
110 public void release() { | |
111 yuvUploader.release(); | |
112 lastI420Frame = null; | |
113 } | |
114 } | |
OLD | NEW |