Skip to content

Commit

Permalink
add ifNeedInit
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabeef committed Nov 1, 2018
1 parent 03d6929 commit 15cb6fb
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 78 deletions.
Expand Up @@ -99,7 +99,7 @@ public GPUImageRenderer(final GPUImageFilter filter) {
public void onSurfaceCreated(final GL10 unused, final EGLConfig config) {
GLES20.glClearColor(backgroundRed, backgroundGreen, backgroundBlue, 1);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
filter.init();
filter.ifNeedInit();
}

@Override
Expand Down Expand Up @@ -202,7 +202,7 @@ public void run() {
if (oldFilter != null) {
oldFilter.destroy();
}
GPUImageRenderer.this.filter.init();
GPUImageRenderer.this.filter.ifNeedInit();
GLES20.glUseProgram(GPUImageRenderer.this.filter.getProgram());
GPUImageRenderer.this.filter.onOutputSizeChanged(outputWidth, outputHeight);
}
Expand Down
Expand Up @@ -89,6 +89,11 @@ public GPUImage3x3ConvolutionFilter(final float[] convolutionKernel) {
public void onInit() {
super.onInit();
uniformConvolutionMatrix = GLES20.glGetUniformLocation(getProgram(), "convolutionMatrix");
}

@Override
public void onInitialized() {
super.onInitialized();
setConvolutionKernel(convolutionKernel);
}

Expand Down
Expand Up @@ -81,6 +81,11 @@ public void onInit() {
super.onInit();
uniformTexelWidthLocation = GLES20.glGetUniformLocation(getProgram(), "texelWidth");
uniformTexelHeightLocation = GLES20.glGetUniformLocation(getProgram(), "texelHeight");
}

@Override
public void onInitialized() {
super.onInitialized();
if (texelWidth != 0) {
updateTexelValues();
}
Expand Down
Expand Up @@ -71,7 +71,7 @@ public class GPUImageBoxBlurFilter extends GPUImageTwoPassTextureSamplingFilter
"gl_FragColor = fragmentColor;\n" +
"}\n";

private float blurSize = 1f;
private float blurSize;

/**
* Construct new BoxBlurFilter with default blur size of 1.0.
Expand All @@ -86,6 +86,12 @@ public GPUImageBoxBlurFilter(float blurSize) {
this.blurSize = blurSize;
}

@Override
public void onInitialized() {
super.onInitialized();
setBlurSize(blurSize);
}

/**
* A scaling for the size of the applied blur, default of 1.0
*
Expand Down
Expand Up @@ -81,6 +81,7 @@ public void onInit() {
@Override
public void onInitialized() {
super.onInitialized();
setAspectRatio(aspectRatio);
setRadius(radius);
setScale(scale);
setCenter(center);
Expand Down
Expand Up @@ -169,10 +169,10 @@ public void onInit() {
@Override
public void onInitialized() {
super.onInitialized();
setMidtones(this.midtones);
setShowdows(this.showdows);
setHighlights(this.highlights);
setPreserveLuminosity(this.preserveLuminosity);
setMidtones(midtones);
setShowdows(showdows);
setHighlights(highlights);
setPreserveLuminosity(preserveLuminosity);
}

public void setShowdows(float[] showdows) {
Expand Down
Expand Up @@ -36,6 +36,11 @@ public GPUImageEmbossFilter(final float intensity) {
@Override
public void onInit() {
super.onInit();
}

@Override
public void onInitialized() {
super.onInitialized();
setIntensity(intensity);
}

Expand Down
Expand Up @@ -70,8 +70,7 @@ public GPUImageFilter(final String vertexShader, final String fragmentShader) {
this.fragmentShader = fragmentShader;
}

public final void init() {
if (isInitialized) return;
private final void init() {
onInit();
onInitialized();
}
Expand All @@ -87,6 +86,10 @@ public void onInit() {
public void onInitialized() {
}

public void ifNeedInit() {
if (!isInitialized) init();
}

public final void destroy() {
isInitialized = false;
GLES20.glDeleteProgram(glProgId);
Expand Down Expand Up @@ -166,78 +169,70 @@ public int getUniformTexture() {
}

protected void setInteger(final int location, final int intValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform1i(location, intValue);
}
});
}

protected void setFloat(final int location, final float floatValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform1f(location, floatValue);
}
});
}

protected void setFloatVec2(final int location, final float[] arrayValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform2fv(location, 1, FloatBuffer.wrap(arrayValue));
}
});
}

protected void setFloatVec3(final int location, final float[] arrayValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform3fv(location, 1, FloatBuffer.wrap(arrayValue));
}
});
}

protected void setFloatVec4(final int location, final float[] arrayValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform4fv(location, 1, FloatBuffer.wrap(arrayValue));
}
});
}

protected void setFloatArray(final int location, final float[] arrayValue) {
checkIsInitialized();

runOnDraw(new Runnable() {
@Override
public void run() {
ifNeedInit();
GLES20.glUniform1fv(location, arrayValue.length, FloatBuffer.wrap(arrayValue));
}
});
}

protected void setPoint(final int location, final PointF point) {
checkIsInitialized();

runOnDraw(new Runnable() {

@Override
public void run() {
ifNeedInit();
float[] vec2 = new float[2];
vec2[0] = point.x;
vec2[1] = point.y;
Expand All @@ -247,24 +242,22 @@ public void run() {
}

protected void setUniformMatrix3f(final int location, final float[] matrix) {
checkIsInitialized();

runOnDraw(new Runnable() {

@Override
public void run() {
ifNeedInit();
GLES20.glUniformMatrix3fv(location, 1, false, matrix, 0);
}
});
}

protected void setUniformMatrix4f(final int location, final float[] matrix) {
checkIsInitialized();

runOnDraw(new Runnable() {

@Override
public void run() {
ifNeedInit();
GLES20.glUniformMatrix4fv(location, 1, false, matrix, 0);
}
});
Expand Down Expand Up @@ -295,10 +288,4 @@ public static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}

private void checkIsInitialized() {
if (!isInitialized) {
throw new AssertionError("Filter should be initialized");
}
}
}
Expand Up @@ -99,7 +99,7 @@ public void addFilter(GPUImageFilter aFilter) {
public void onInit() {
super.onInit();
for (GPUImageFilter filter : filters) {
filter.init();
filter.ifNeedInit();
}
}

Expand Down
Expand Up @@ -78,7 +78,7 @@ public class GPUImageGaussianBlurFilter extends GPUImageTwoPassTextureSamplingFi
" gl_FragColor = vec4(sum,fragColor.a);\n" +
"}";

protected float blurSize = 1f;
protected float blurSize;

public GPUImageGaussianBlurFilter() {
this(1f);
Expand All @@ -89,6 +89,12 @@ public GPUImageGaussianBlurFilter(float blurSize) {
this.blurSize = blurSize;
}

@Override
public void onInitialized() {
super.onInitialized();
setBlurSize(blurSize);
}

@Override
public float getVerticalTexelOffsetRatio() {
return blurSize;
Expand Down
Expand Up @@ -93,6 +93,7 @@ public void onInit() {
@Override
public void onInitialized() {
super.onInitialized();
setAspectRatio(aspectRatio);
setRadius(radius);
setCenter(center);
setRefractiveIndex(refractiveIndex);
Expand Down
Expand Up @@ -46,7 +46,13 @@ public void onInit() {
super.onInit();
fractionalWidthOfPixelLocation = GLES20.glGetUniformLocation(getProgram(), "fractionalWidthOfPixel");
aspectRatioLocation = GLES20.glGetUniformLocation(getProgram(), "aspectRatio");
}

@Override
public void onInitialized() {
super.onInitialized();
setFractionalWidthOfAPixel(fractionalWidthOfAPixel);
setAspectRatio(aspectRatio);
}

@Override
Expand Down
Expand Up @@ -80,6 +80,11 @@ private GPUImageLaplacianFilter(final float[] convolutionKernel) {
public void onInit() {
super.onInit();
uniformConvolutionMatrix = GLES20.glGetUniformLocation(getProgram(), "convolutionMatrix");
}

@Override
public void onInitialized() {
super.onInitialized();
setConvolutionKernel(convolutionKernel);
}

Expand Down
Expand Up @@ -50,7 +50,6 @@ private GPUImageLevelsFilter(final float[] min, final float[] mid, final float[]
this.max = max;
minOutput = minOUt;
maxOutput = maxOut;
setMin(0.0f, 1.0f, 1.0f, 0.0f, 1.0f);
}

@Override
Expand All @@ -66,6 +65,7 @@ public void onInit() {
@Override
public void onInitialized() {
super.onInitialized();
setMin(0.0f, 1.0f, 1.0f, 0.0f, 1.0f);
updateUniforms();
}

Expand Down
Expand Up @@ -91,11 +91,11 @@ public void setIntensity(final float intensity) {

public void setColor(final float[] color) {
this.color = color;
setColorRed(this.color[0], this.color[1], this.color[2]);
setColor(this.color[0], this.color[1], this.color[2]);

}

public void setColorRed(final float red, final float green, final float blue) {
public void setColor(final float red, final float green, final float blue) {
setFloatVec3(filterColorLocation, new float[]{red, green, blue});
}
}
Expand Up @@ -58,6 +58,11 @@ public void onInit() {
imageWidthFactorLocation = GLES20.glGetUniformLocation(getProgram(), "imageWidthFactor");
imageHeightFactorLocation = GLES20.glGetUniformLocation(getProgram(), "imageHeightFactor");
pixelLocation = GLES20.glGetUniformLocation(getProgram(), "pixel");
}

@Override
public void onInitialized() {
super.onInitialized();
setPixel(pixel);
}

Expand Down
Expand Up @@ -53,6 +53,11 @@ public GPUImagePosterizeFilter(final int colorLevels) {
public void onInit() {
super.onInit();
glUniformColorLevels = GLES20.glGetUniformLocation(getProgram(), "colorLevels");
}

@Override
public void onInitialized() {
super.onInitialized();
setColorLevels(colorLevels);
}

Expand Down

0 comments on commit 15cb6fb

Please sign in to comment.