|
|
@ -11,17 +11,17 @@ import com.seu.magicfilter.utils.OpenGlUtils;
|
|
|
|
import android.opengl.GLES20;
|
|
|
|
import android.opengl.GLES20;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
public class MagicBaseGroupFilter extends GPUImageFilter {
|
|
|
|
protected static int[] frameBuffers = null;
|
|
|
|
private static int[] frameBuffers = null;
|
|
|
|
protected static int[] frameBufferTextures = null;
|
|
|
|
private static int[] frameBufferTextures = null;
|
|
|
|
private int frameWidth = -1;
|
|
|
|
private int frameWidth = -1;
|
|
|
|
private int frameHeight = -1;
|
|
|
|
private int frameHeight = -1;
|
|
|
|
protected List<GPUImageFilter> filters;
|
|
|
|
protected List<GPUImageFilter> filters;
|
|
|
|
|
|
|
|
|
|
|
|
public MagicBaseGroupFilter(List<GPUImageFilter> filters){
|
|
|
|
public MagicBaseGroupFilter(List<GPUImageFilter> filters) {
|
|
|
|
this.filters = filters;
|
|
|
|
this.filters = filters;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
public void onDestroy() {
|
|
|
|
for (GPUImageFilter filter : filters) {
|
|
|
|
for (GPUImageFilter filter : filters) {
|
|
|
@ -29,14 +29,14 @@ public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destroyFramebuffers();
|
|
|
|
destroyFramebuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void init() {
|
|
|
|
public void init() {
|
|
|
|
for (GPUImageFilter filter : filters) {
|
|
|
|
for (GPUImageFilter filter : filters) {
|
|
|
|
filter.init();
|
|
|
|
filter.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onInputSizeChanged(final int width, final int height) {
|
|
|
|
public void onInputSizeChanged(final int width, final int height) {
|
|
|
|
super.onInputSizeChanged(width, height);
|
|
|
|
super.onInputSizeChanged(width, height);
|
|
|
@ -44,41 +44,41 @@ public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
filters.get(i).onInputSizeChanged(width, height);
|
|
|
|
filters.get(i).onInputSizeChanged(width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(frameBuffers != null && (frameWidth != width || frameHeight != height || frameBuffers.length != size-1)){
|
|
|
|
if (frameBuffers != null && (frameWidth != width || frameHeight != height || frameBuffers.length != size - 1)) {
|
|
|
|
destroyFramebuffers();
|
|
|
|
destroyFramebuffers();
|
|
|
|
frameWidth = width;
|
|
|
|
frameWidth = width;
|
|
|
|
frameHeight = height;
|
|
|
|
frameHeight = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frameBuffers == null) {
|
|
|
|
if (frameBuffers == null) {
|
|
|
|
frameBuffers = new int[size-1];
|
|
|
|
frameBuffers = new int[size - 1];
|
|
|
|
frameBufferTextures = new int[size-1];
|
|
|
|
frameBufferTextures = new int[size - 1];
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < size-1; i++) {
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
GLES20.glGenFramebuffers(1, frameBuffers, i);
|
|
|
|
GLES20.glGenFramebuffers(1, frameBuffers, i);
|
|
|
|
|
|
|
|
|
|
|
|
GLES20.glGenTextures(1, frameBufferTextures, i);
|
|
|
|
GLES20.glGenTextures(1, frameBufferTextures, i);
|
|
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameBufferTextures[i]);
|
|
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameBufferTextures[i]);
|
|
|
|
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
|
|
|
|
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
|
|
|
|
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
|
|
|
|
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
|
|
|
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
|
|
|
|
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
|
|
|
|
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
|
|
|
|
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
|
|
|
|
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
|
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
|
|
|
|
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
|
|
|
|
GLES20.GL_TEXTURE_2D, frameBufferTextures[i], 0);
|
|
|
|
GLES20.GL_TEXTURE_2D, frameBufferTextures[i], 0);
|
|
|
|
|
|
|
|
|
|
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
|
|
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int onDrawFrame(final int textureId, final FloatBuffer cubeBuffer,
|
|
|
|
public int onDrawFrame(final int textureId, final FloatBuffer cubeBuffer,
|
|
|
|
final FloatBuffer textureBuffer) {
|
|
|
|
final FloatBuffer textureBuffer) {
|
|
|
@ -91,29 +91,29 @@ public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
GPUImageFilter filter = filters.get(i);
|
|
|
|
GPUImageFilter filter = filters.get(i);
|
|
|
|
boolean isNotLast = i < size - 1;
|
|
|
|
boolean isNotLast = i < size - 1;
|
|
|
|
if (isNotLast) {
|
|
|
|
if (isNotLast) {
|
|
|
|
GLES20.glViewport(0, 0, mInputWidth, mInputHeight);
|
|
|
|
GLES20.glViewport(0, 0, mInputWidth, mInputHeight);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
|
GLES20.glClearColor(0, 0, 0, 0);
|
|
|
|
GLES20.glClearColor(0, 0, 0, 0);
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
previousTexture = frameBufferTextures[i];
|
|
|
|
previousTexture = frameBufferTextures[i];
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
GLES20.glViewport(0, 0, mOutputWidth, mOutputHeight);
|
|
|
|
GLES20.glViewport(0, 0, mOutputWidth, mOutputHeight);
|
|
|
|
filter.onDrawFrame(previousTexture, cubeBuffer, textureBuffer);
|
|
|
|
filter.onDrawFrame(previousTexture, cubeBuffer, textureBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OpenGlUtils.ON_DRAWN;
|
|
|
|
return OpenGlUtils.ON_DRAWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int onDrawFrame(final int textureId) {
|
|
|
|
public int onDrawFrame(int textureId, boolean needFbo) {
|
|
|
|
if (frameBuffers == null || frameBufferTextures == null) {
|
|
|
|
if (frameBuffers == null || frameBufferTextures == null) {
|
|
|
|
return OpenGlUtils.NOT_INIT;
|
|
|
|
return OpenGlUtils.NOT_INIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int size = filters.size();
|
|
|
|
int size = filters.size();
|
|
|
|
int previousTexture = textureId;
|
|
|
|
int previousTexture = textureId;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
GPUImageFilter filter = filters.get(i);
|
|
|
|
GPUImageFilter filter = filters.get(i);
|
|
|
|
boolean isNotLast = i < size - 1;
|
|
|
|
boolean isNotLast = i < size - 1;
|
|
|
|
if (isNotLast) {
|
|
|
|
if (isNotLast) {
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers[i]);
|
|
|
@ -121,13 +121,13 @@ public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
|
|
|
|
previousTexture = frameBufferTextures[i];
|
|
|
|
previousTexture = frameBufferTextures[i];
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
filter.onDrawFrame(previousTexture, mGLCubeBuffer, mGLTextureBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OpenGlUtils.ON_DRAWN;
|
|
|
|
return OpenGlUtils.ON_DRAWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void destroyFramebuffers() {
|
|
|
|
private void destroyFramebuffers() {
|
|
|
|
if (frameBufferTextures != null) {
|
|
|
|
if (frameBufferTextures != null) {
|
|
|
|
GLES20.glDeleteTextures(frameBufferTextures.length, frameBufferTextures, 0);
|
|
|
|
GLES20.glDeleteTextures(frameBufferTextures.length, frameBufferTextures, 0);
|
|
|
@ -138,8 +138,8 @@ public class MagicBaseGroupFilter extends GPUImageFilter{
|
|
|
|
frameBuffers = null;
|
|
|
|
frameBuffers = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getSize(){
|
|
|
|
public int getSize() {
|
|
|
|
return filters.size();
|
|
|
|
return filters.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|