Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ce766d76
authored
Dec 19, 2022
by
tofunmi
Committed by
Tianyi Feng
Dec 21, 2022
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Create dynamically created shaders for multiple overlays.
PiperOrigin-RevId: 496379904
parent
d2a3d8f6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
203 additions
and
131 deletions
library/effect/src/androidTest/java/com/google/android/exoplayer2/effect/OverlayTextureProcessorPixelTest.java
library/effect/src/main/assets/shaders/fragment_shader_overlay_es2.glsl
library/effect/src/main/assets/shaders/vertex_shader_overlay_es2.glsl
library/effect/src/main/java/com/google/android/exoplayer2/effect/OverlayTextureProcessor.java
testdata/src/test/assets/media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_multiple.png
testdata/src/test/assets/media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_overlap.png
library/effect/src/androidTest/java/com/google/android/exoplayer2/effect/OverlayTextureProcessorPixelTest.java
View file @
ce766d76
...
@@ -70,6 +70,10 @@ public class OverlayTextureProcessorPixelTest {
...
@@ -70,6 +70,10 @@ public class OverlayTextureProcessorPixelTest {
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_text_default.png"
;
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_text_default.png"
;
public
static
final
String
OVERLAY_TEXT_TRANSLATE
=
public
static
final
String
OVERLAY_TEXT_TRANSLATE
=
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_text_translate.png"
;
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_text_translate.png"
;
public
static
final
String
OVERLAY_MULTIPLE
=
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_multiple.png"
;
public
static
final
String
OVERLAY_OVERLAP
=
"media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_overlap.png"
;
private
final
Context
context
=
getApplicationContext
();
private
final
Context
context
=
getApplicationContext
();
...
@@ -103,7 +107,7 @@ public class OverlayTextureProcessorPixelTest {
...
@@ -103,7 +107,7 @@ public class OverlayTextureProcessorPixelTest {
@Test
@Test
public
void
drawFrame_noOverlay_leavesFrameUnchanged
()
throws
Exception
{
public
void
drawFrame_noOverlay_leavesFrameUnchanged
()
throws
Exception
{
String
testId
=
"drawFrame_noOverlay
s
"
;
String
testId
=
"drawFrame_noOverlay"
;
overlayTextureProcessor
=
overlayTextureProcessor
=
new
OverlayEffect
(
/* textureOverlays= */
ImmutableList
.
of
())
new
OverlayEffect
(
/* textureOverlays= */
ImmutableList
.
of
())
.
toGlTextureProcessor
(
context
,
/* useHdr= */
false
);
.
toGlTextureProcessor
(
context
,
/* useHdr= */
false
);
...
@@ -281,6 +285,77 @@ public class OverlayTextureProcessorPixelTest {
...
@@ -281,6 +285,77 @@ public class OverlayTextureProcessorPixelTest {
assertThat
(
averagePixelAbsoluteDifference
).
isAtMost
(
MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE
);
assertThat
(
averagePixelAbsoluteDifference
).
isAtMost
(
MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE
);
}
}
@Test
public
void
drawFrame_multipleOverlays_blendsBothIntoFrame
()
throws
Exception
{
String
testId
=
"drawFrame_multipleOverlays"
;
float
[]
translateMatrix1
=
GlUtil
.
create4x4IdentityMatrix
();
Matrix
.
translateM
(
translateMatrix1
,
/* mOffset= */
0
,
/* x= */
0.5f
,
/* y= */
0.5f
,
/* z= */
1
);
SpannableString
overlayText
=
new
SpannableString
(
/* source= */
"Overlay 1"
);
overlayText
.
setSpan
(
new
ForegroundColorSpan
(
Color
.
GRAY
),
/* start= */
0
,
/* end= */
4
,
Spannable
.
SPAN_EXCLUSIVE_EXCLUSIVE
);
OverlaySettings
overlaySettings1
=
new
OverlaySettings
.
Builder
().
setMatrix
(
translateMatrix1
).
build
();
TextOverlay
textOverlay
=
TextOverlay
.
createStaticTextOverlay
(
overlayText
,
overlaySettings1
);
Bitmap
bitmap
=
readBitmap
(
OVERLAY_PNG_ASSET_PATH
);
OverlaySettings
overlaySettings2
=
new
OverlaySettings
.
Builder
().
setAlpha
(
0.5f
).
build
();
BitmapOverlay
bitmapOverlay
=
BitmapOverlay
.
createStaticBitmapOverlay
(
bitmap
,
overlaySettings2
);
overlayTextureProcessor
=
new
OverlayEffect
(
ImmutableList
.
of
(
textOverlay
,
bitmapOverlay
))
.
toGlTextureProcessor
(
context
,
/* useHdr= */
false
);
Size
outputSize
=
overlayTextureProcessor
.
configure
(
inputWidth
,
inputHeight
);
setupOutputTexture
(
outputSize
.
getWidth
(),
outputSize
.
getHeight
());
Bitmap
expectedBitmap
=
readBitmap
(
OVERLAY_MULTIPLE
);
overlayTextureProcessor
.
drawFrame
(
inputTexId
,
/* presentationTimeUs= */
0
);
Bitmap
actualBitmap
=
createArgb8888BitmapFromCurrentGlFramebuffer
(
outputSize
.
getWidth
(),
outputSize
.
getHeight
());
maybeSaveTestBitmapToCacheDirectory
(
testId
,
/* bitmapLabel= */
"actual"
,
actualBitmap
);
float
averagePixelAbsoluteDifference
=
getBitmapAveragePixelAbsoluteDifferenceArgb8888
(
expectedBitmap
,
actualBitmap
,
testId
);
assertThat
(
averagePixelAbsoluteDifference
).
isAtMost
(
MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE
);
}
@Test
public
void
drawFrame_overlappingOverlays_blendsOnFifoOrder
()
throws
Exception
{
String
testId
=
"drawFrame_overlappingOverlays"
;
SpannableString
overlayText
=
new
SpannableString
(
/* source= */
"Overlapping text"
);
overlayText
.
setSpan
(
new
ForegroundColorSpan
(
Color
.
WHITE
),
/* start= */
0
,
/* end= */
overlayText
.
length
(),
Spannable
.
SPAN_EXCLUSIVE_EXCLUSIVE
);
float
[]
scaleTextMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
Matrix
.
scaleM
(
scaleTextMatrix
,
/* mOffset= */
0
,
/* x= */
0.5f
,
/* y= */
0.5f
,
/* z= */
1
);
OverlaySettings
overlaySettings1
=
new
OverlaySettings
.
Builder
().
setMatrix
(
scaleTextMatrix
).
build
();
TextOverlay
textOverlay
=
TextOverlay
.
createStaticTextOverlay
(
overlayText
,
overlaySettings1
);
Bitmap
bitmap
=
readBitmap
(
OVERLAY_PNG_ASSET_PATH
);
float
[]
scaleMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
Matrix
.
scaleM
(
scaleMatrix
,
/* mOffset= */
0
,
/* x= */
3
,
/* y= */
3
,
/* z= */
1
);
OverlaySettings
overlaySettings2
=
new
OverlaySettings
.
Builder
().
setMatrix
(
scaleMatrix
).
build
();
BitmapOverlay
bitmapOverlay
=
BitmapOverlay
.
createStaticBitmapOverlay
(
bitmap
,
overlaySettings2
);
overlayTextureProcessor
=
new
OverlayEffect
(
ImmutableList
.
of
(
bitmapOverlay
,
textOverlay
))
.
toGlTextureProcessor
(
context
,
/* useHdr= */
false
);
Size
outputSize
=
overlayTextureProcessor
.
configure
(
inputWidth
,
inputHeight
);
setupOutputTexture
(
outputSize
.
getWidth
(),
outputSize
.
getHeight
());
Bitmap
expectedBitmap
=
readBitmap
(
OVERLAY_OVERLAP
);
overlayTextureProcessor
.
drawFrame
(
inputTexId
,
/* presentationTimeUs= */
0
);
Bitmap
actualBitmap
=
createArgb8888BitmapFromCurrentGlFramebuffer
(
outputSize
.
getWidth
(),
outputSize
.
getHeight
());
maybeSaveTestBitmapToCacheDirectory
(
testId
,
/* bitmapLabel= */
"actual"
,
actualBitmap
);
float
averagePixelAbsoluteDifference
=
getBitmapAveragePixelAbsoluteDifferenceArgb8888
(
expectedBitmap
,
actualBitmap
,
testId
);
assertThat
(
averagePixelAbsoluteDifference
).
isAtMost
(
MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE
);
}
private
void
setupOutputTexture
(
int
outputWidth
,
int
outputHeight
)
throws
GlUtil
.
GlException
{
private
void
setupOutputTexture
(
int
outputWidth
,
int
outputHeight
)
throws
GlUtil
.
GlException
{
int
outputTexId
=
int
outputTexId
=
GlUtil
.
createTexture
(
GlUtil
.
createTexture
(
...
...
library/effect/src/main/assets/shaders/fragment_shader_overlay_es2.glsl
deleted
100644 → 0
View file @
d2a3d8f6
#version 100
// Copyright 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ES 2 fragment shader that overlays a bitmap over a video frame.
precision
mediump
float
;
// Texture containing an input video frame.
uniform
sampler2D
uVideoTexSampler0
;
// Texture containing the overlay bitmap.
uniform
sampler2D
uOverlayTexSampler1
;
// The alpha values for the texture.
uniform
float
uOverlayAlpha1
;
varying
vec2
vVideoTexSamplingCoord
;
varying
vec2
vOverlayTexSamplingCoord1
;
// Manually implementing the CLAMP_TO_BORDER texture wrapping option
// (https://open.gl/textures) since it's not implemented until OpenGL ES 3.2.
vec4
getClampToBorderOverlayColor
()
{
if
(
vOverlayTexSamplingCoord1
.
x
>
1
.
0
||
vOverlayTexSamplingCoord1
.
x
<
0
.
0
||
vOverlayTexSamplingCoord1
.
y
>
1
.
0
||
vOverlayTexSamplingCoord1
.
y
<
0
.
0
)
{
return
vec4
(
0
.
0
,
0
.
0
,
0
.
0
,
0
.
0
);
}
else
{
vec4
overlayColor
=
vec4
(
texture2D
(
uOverlayTexSampler1
,
vOverlayTexSamplingCoord1
));
overlayColor
.
a
=
uOverlayAlpha1
*
overlayColor
.
a
;
return
overlayColor
;
}
}
float
getMixAlpha
(
float
videoAlpha
,
float
overlayAlpha
)
{
if
(
videoAlpha
==
0
.
0
)
{
return
1
.
0
;
}
else
{
return
clamp
(
overlayAlpha
/
videoAlpha
,
0
.
0
,
1
.
0
);
}
}
void
main
()
{
vec4
videoColor
=
vec4
(
texture2D
(
uVideoTexSampler0
,
vVideoTexSamplingCoord
));
vec4
overlayColor
=
getClampToBorderOverlayColor
();
// Blend the video decoder output and the overlay bitmap.
gl_FragColor
=
mix
(
videoColor
,
overlayColor
,
getMixAlpha
(
videoColor
.
a
,
overlayColor
.
a
));
}
library/effect/src/main/assets/shaders/vertex_shader_overlay_es2.glsl
deleted
100644 → 0
View file @
d2a3d8f6
#version 100
// Copyright 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ES 2 vertex shader that leaves the frame coordinates unchanged
// and applies matrix transformations to the texture coordinates.
uniform
mat4
uAspectRatioMatrix
;
uniform
mat4
uOverlayMatrix
;
attribute
vec4
aFramePosition
;
varying
vec2
vVideoTexSamplingCoord
;
varying
vec2
vOverlayTexSamplingCoord1
;
vec2
getTexSamplingCoord
(
vec2
ndcPosition
)
{
return
vec2
(
ndcPosition
.
x
*
0
.
5
+
0
.
5
,
ndcPosition
.
y
*
0
.
5
+
0
.
5
);
}
void
main
()
{
gl_Position
=
aFramePosition
;
vec4
aOverlayPosition
=
uAspectRatioMatrix
*
uOverlayMatrix
*
aFramePosition
;
vOverlayTexSamplingCoord1
=
getTexSamplingCoord
(
aOverlayPosition
.
xy
);
vVideoTexSamplingCoord
=
getTexSamplingCoord
(
aFramePosition
.
xy
);
}
library/effect/src/main/java/com/google/android/exoplayer2/effect/OverlayTextureProcessor.java
View file @
ce766d76
...
@@ -24,16 +24,13 @@ import com.google.android.exoplayer2.util.FrameProcessingException;
...
@@ -24,16 +24,13 @@ import com.google.android.exoplayer2.util.FrameProcessingException;
import
com.google.android.exoplayer2.util.GlProgram
;
import
com.google.android.exoplayer2.util.GlProgram
;
import
com.google.android.exoplayer2.util.GlUtil
;
import
com.google.android.exoplayer2.util.GlUtil
;
import
com.google.android.exoplayer2.util.Size
;
import
com.google.android.exoplayer2.util.Size
;
import
com.google.android.exoplayer2.util.Util
;
import
com.google.common.collect.ImmutableList
;
import
com.google.common.collect.ImmutableList
;
import
java.io.IOException
;
/** Applies
one
or more {@link TextureOverlay}s onto each frame. */
/** Applies
zero
or more {@link TextureOverlay}s onto each frame. */
/* package */
final
class
OverlayTextureProcessor
extends
SingleFrameGlTextureProcessor
{
/* package */
final
class
OverlayTextureProcessor
extends
SingleFrameGlTextureProcessor
{
private
static
final
String
VERTEX_SHADER_PATH
=
"shaders/vertex_shader_overlay_es2.glsl"
;
private
static
final
String
FRAGMENT_SHADER_PATH
=
"shaders/fragment_shader_overlay_es2.glsl"
;
private
static
final
int
MATRIX_OFFSET
=
0
;
private
static
final
int
MATRIX_OFFSET
=
0
;
private
static
final
int
TRANSPARENT_TEXTURE_WIDTH_HEIGHT
=
1
;
private
final
GlProgram
glProgram
;
private
final
GlProgram
glProgram
;
private
final
ImmutableList
<
TextureOverlay
>
overlays
;
private
final
ImmutableList
<
TextureOverlay
>
overlays
;
...
@@ -56,16 +53,19 @@ import java.io.IOException;
...
@@ -56,16 +53,19 @@ import java.io.IOException;
throws
FrameProcessingException
{
throws
FrameProcessingException
{
super
(
useHdr
);
super
(
useHdr
);
checkArgument
(!
useHdr
,
"OverlayTextureProcessor does not support HDR colors yet."
);
checkArgument
(!
useHdr
,
"OverlayTextureProcessor does not support HDR colors yet."
);
// TODO: If the limit on the number of overlays ever becomes and issue, investigate expanding it
// in relation to common device limits.
checkArgument
(
checkArgument
(
overlays
.
size
()
<=
1
,
overlays
.
size
()
<=
8
,
"OverlayTextureProcessor does not
support multiple
overlays in the same processor yet."
);
"OverlayTextureProcessor does not
more than 8
overlays in the same processor yet."
);
this
.
overlays
=
overlays
;
this
.
overlays
=
overlays
;
aspectRatioMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
aspectRatioMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
overlayMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
overlayMatrix
=
GlUtil
.
create4x4IdentityMatrix
();
try
{
try
{
glProgram
=
new
GlProgram
(
context
,
VERTEX_SHADER_PATH
,
FRAGMENT_SHADER_PATH
);
glProgram
=
}
catch
(
GlUtil
.
GlException
|
IOException
e
)
{
new
GlProgram
(
createVertexShader
(
overlays
.
size
()),
createFragmentShader
(
overlays
.
size
()));
}
catch
(
GlUtil
.
GlException
e
)
{
throw
new
FrameProcessingException
(
e
);
throw
new
FrameProcessingException
(
e
);
}
}
...
@@ -87,30 +87,32 @@ import java.io.IOException;
...
@@ -87,30 +87,32 @@ import java.io.IOException;
try
{
try
{
glProgram
.
use
();
glProgram
.
use
();
if
(!
overlays
.
isEmpty
())
{
if
(!
overlays
.
isEmpty
())
{
TextureOverlay
overlay
=
overlays
.
get
(
0
);
for
(
int
texUnitIndex
=
1
;
texUnitIndex
<=
overlays
.
size
();
texUnitIndex
++)
{
TextureOverlay
overlay
=
overlays
.
get
(
texUnitIndex
-
1
);
glProgram
.
setSamplerTexIdUniform
(
glProgram
.
setSamplerTexIdUniform
(
"uOverlayTexSampler1"
,
overlay
.
getTextureId
(
presentationTimeUs
),
/* texUnitIndex= */
1
);
Util
.
formatInvariant
(
"uOverlayTexSampler%d"
,
texUnitIndex
),
Size
overlayTextureSize
=
overlay
.
getTextureSize
(
presentationTimeUs
);
overlay
.
getTextureId
(
presentationTimeUs
),
texUnitIndex
);
GlUtil
.
setToIdentity
(
aspectRatioMatrix
);
GlUtil
.
setToIdentity
(
aspectRatioMatrix
);
Matrix
.
scaleM
(
Matrix
.
scaleM
(
aspectRatioMatrix
,
aspectRatioMatrix
,
MATRIX_OFFSET
,
MATRIX_OFFSET
,
videoWidth
/
(
float
)
overlayTextureSize
.
getWidth
(),
videoWidth
/
(
float
)
overlay
.
getTextureSize
(
presentationTimeUs
)
.
getWidth
(),
videoHeight
/
(
float
)
overlayTextureSize
.
getHeight
(),
videoHeight
/
(
float
)
overlay
.
getTextureSize
(
presentationTimeUs
)
.
getHeight
(),
/* z= */
1
);
/* z= */
1
);
glProgram
.
setFloatsUniform
(
"uAspectRatioMatrix"
,
aspectRatioMatrix
);
glProgram
.
setFloatsUniform
(
Util
.
formatInvariant
(
"uAspectRatioMatrix%d"
,
texUnitIndex
),
aspectRatioMatrix
);
Matrix
.
invertM
(
Matrix
.
invertM
(
overlayMatrix
,
overlayMatrix
,
MATRIX_OFFSET
,
MATRIX_OFFSET
,
overlay
.
getOverlaySettings
(
presentationTimeUs
).
matrix
,
overlay
.
getOverlaySettings
(
presentationTimeUs
).
matrix
,
MATRIX_OFFSET
);
MATRIX_OFFSET
);
glProgram
.
setFloatsUniform
(
"uOverlayMatrix"
,
overlayMatrix
);
glProgram
.
setFloatsUniform
(
Util
.
formatInvariant
(
"uOverlayMatrix%d"
,
texUnitIndex
),
overlayMatrix
);
glProgram
.
setFloatUniform
(
glProgram
.
setFloatUniform
(
"uOverlayAlpha1"
,
overlay
.
getOverlaySettings
(
presentationTimeUs
).
alpha
);
Util
.
formatInvariant
(
"uOverlayAlpha%d"
,
texUnitIndex
),
overlay
.
getOverlaySettings
(
presentationTimeUs
).
alpha
);
}
else
{
}
glProgram
.
setSamplerTexIdUniform
(
"uOverlayTexSampler1"
,
createTransparentTexture
(),
/* texUnitIndex= */
1
);
}
}
glProgram
.
setSamplerTexIdUniform
(
"uVideoTexSampler0"
,
inputTexId
,
/* texUnitIndex= */
0
);
glProgram
.
setSamplerTexIdUniform
(
"uVideoTexSampler0"
,
inputTexId
,
/* texUnitIndex= */
0
);
glProgram
.
bindAttributesAndUniforms
();
glProgram
.
bindAttributesAndUniforms
();
...
@@ -122,20 +124,6 @@ import java.io.IOException;
...
@@ -122,20 +124,6 @@ import java.io.IOException;
}
}
}
}
private
int
createTransparentTexture
()
throws
FrameProcessingException
{
try
{
int
textureId
=
GlUtil
.
createTexture
(
TRANSPARENT_TEXTURE_WIDTH_HEIGHT
,
TRANSPARENT_TEXTURE_WIDTH_HEIGHT
,
/* useHighPrecisionColorComponents= */
false
);
GlUtil
.
checkGlError
();
return
textureId
;
}
catch
(
GlUtil
.
GlException
e
)
{
throw
new
FrameProcessingException
(
e
);
}
}
@Override
@Override
public
void
release
()
throws
FrameProcessingException
{
public
void
release
()
throws
FrameProcessingException
{
super
.
release
();
super
.
release
();
...
@@ -145,4 +133,108 @@ import java.io.IOException;
...
@@ -145,4 +133,108 @@ import java.io.IOException;
throw
new
FrameProcessingException
(
e
);
throw
new
FrameProcessingException
(
e
);
}
}
}
}
private
static
String
createVertexShader
(
int
numOverlays
)
{
StringBuilder
shader
=
new
StringBuilder
()
.
append
(
"#version 100\n"
)
.
append
(
"attribute vec4 aFramePosition;\n"
)
.
append
(
"varying vec2 vVideoTexSamplingCoord0;\n"
);
for
(
int
texUnitIndex
=
1
;
texUnitIndex
<=
numOverlays
;
texUnitIndex
++)
{
shader
.
append
(
Util
.
formatInvariant
(
"uniform mat4 uAspectRatioMatrix%d;\n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
"uniform mat4 uOverlayMatrix%d;\n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
"varying vec2 vOverlayTexSamplingCoord%d;\n"
,
texUnitIndex
));
}
shader
.
append
(
"vec2 getTexSamplingCoord(vec2 ndcPosition){\n"
)
.
append
(
" return vec2(ndcPosition.x * 0.5 + 0.5, ndcPosition.y * 0.5 + 0.5);\n"
)
.
append
(
"}\n"
)
.
append
(
"void main() {\n"
)
.
append
(
" gl_Position = aFramePosition;\n"
)
.
append
(
" vVideoTexSamplingCoord0 = getTexSamplingCoord(aFramePosition.xy);\n"
);
for
(
int
texUnitIndex
=
1
;
texUnitIndex
<=
numOverlays
;
texUnitIndex
++)
{
shader
.
append
(
Util
.
formatInvariant
(
" vec4 aOverlayPosition%d = \n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
" uAspectRatioMatrix%d * uOverlayMatrix%d * aFramePosition;\n"
,
texUnitIndex
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
" vOverlayTexSamplingCoord%d = getTexSamplingCoord(aOverlayPosition%d.xy);\n"
,
texUnitIndex
,
texUnitIndex
));
}
shader
.
append
(
"}\n"
);
return
shader
.
toString
();
}
private
static
String
createFragmentShader
(
int
numOverlays
)
{
StringBuilder
shader
=
new
StringBuilder
()
.
append
(
"#version 100\n"
)
.
append
(
"precision mediump float;\n"
)
.
append
(
"uniform sampler2D uVideoTexSampler0;\n"
)
.
append
(
"varying vec2 vVideoTexSamplingCoord0;\n"
)
.
append
(
"// Manually implementing the CLAMP_TO_BORDER texture wrapping option\n"
)
.
append
(
"// (https://open.gl/textures) since it's not implemented until OpenGL ES 3.2.\n"
)
.
append
(
"vec4 getClampToBorderOverlayColor(\n"
)
.
append
(
" sampler2D texSampler, vec2 texSamplingCoord, float alpha){\n"
)
.
append
(
" if (texSamplingCoord.x > 1.0 || texSamplingCoord.x < 0.0\n"
)
.
append
(
" || texSamplingCoord.y > 1.0 || texSamplingCoord.y < 0.0) {\n"
)
.
append
(
" return vec4(0.0, 0.0, 0.0, 0.0);\n"
)
.
append
(
" } else {\n"
)
.
append
(
" vec4 overlayColor = vec4(texture2D(texSampler, texSamplingCoord));\n"
)
.
append
(
" overlayColor.a = alpha * overlayColor.a;\n"
)
.
append
(
" return overlayColor;\n"
)
.
append
(
" }\n"
)
.
append
(
"}\n"
)
.
append
(
"\n"
)
.
append
(
"float getMixAlpha(float videoAlpha, float overlayAlpha) {\n"
)
.
append
(
" if (videoAlpha == 0.0) {\n"
)
.
append
(
" return 1.0;\n"
)
.
append
(
" } else {\n"
)
.
append
(
" return clamp(overlayAlpha/videoAlpha, 0.0, 1.0);\n"
)
.
append
(
" }\n"
)
.
append
(
"}\n"
);
for
(
int
texUnitIndex
=
1
;
texUnitIndex
<=
numOverlays
;
texUnitIndex
++)
{
shader
.
append
(
Util
.
formatInvariant
(
"uniform sampler2D uOverlayTexSampler%d;\n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
"uniform float uOverlayAlpha%d;\n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
"varying vec2 vOverlayTexSamplingCoord%d;\n"
,
texUnitIndex
));
}
shader
.
append
(
"void main() {\n"
)
.
append
(
" vec4 videoColor = vec4(texture2D(uVideoTexSampler0, vVideoTexSamplingCoord0));\n"
)
.
append
(
" vec4 fragColor = videoColor;\n"
);
for
(
int
texUnitIndex
=
1
;
texUnitIndex
<=
numOverlays
;
texUnitIndex
++)
{
shader
.
append
(
Util
.
formatInvariant
(
" vec4 overlayColor%d = getClampToBorderOverlayColor(\n"
,
texUnitIndex
))
.
append
(
Util
.
formatInvariant
(
" uOverlayTexSampler%d, vOverlayTexSamplingCoord%d, uOverlayAlpha%d);\n"
,
texUnitIndex
,
texUnitIndex
,
texUnitIndex
))
.
append
(
" fragColor = mix(\n"
)
.
append
(
Util
.
formatInvariant
(
" fragColor, overlayColor%d, getMixAlpha(videoColor.a, overlayColor%d.a));\n"
,
texUnitIndex
,
texUnitIndex
));
}
shader
.
append
(
" gl_FragColor = fragColor;\n"
).
append
(
"}\n"
);
return
shader
.
toString
();
}
}
}
testdata/src/test/assets/media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_multiple.png
0 → 100644
View file @
ce766d76
546 KB
testdata/src/test/assets/media/bitmap/sample_mp4_first_frame/electrical_colors/overlay_overlap.png
0 → 100644
View file @
ce766d76
546 KB
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment