Permalink
Browse files

Refactor mIsInitialized out into the GPUImageFilter class

  • Loading branch information...
1 parent 42d3f05 commit 9bfe4d0f3c5dbbc320aa8b240fed9d58702e9d00 @pboos pboos committed Dec 27, 2012
View
@@ -1,3 +1,6 @@
#OSX
.DS_Store
target/
+
+.idea/
+*.iml
@@ -16,14 +16,6 @@
package jp.co.cyberagent.android.gpuimage;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.List;
-import java.util.concurrent.Semaphore;
-
-import jp.co.cyberagent.android.gpuimage.GPUImageRenderer.Rotation;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
@@ -45,6 +37,14 @@
import android.provider.MediaStore;
import android.view.Display;
import android.view.WindowManager;
+import jp.co.cyberagent.android.gpuimage.GPUImageRenderer.Rotation;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.Semaphore;
/**
* The main accessor for GPUImage functionality. This class helps to do common
@@ -239,7 +239,7 @@ public Bitmap getBitmapWithFilterApplied(final Bitmap bitmap) {
@Override
public void run() {
- mFilter.onDestroy();
+ mFilter.destroy();
lock.release();
}
});
@@ -259,7 +259,7 @@ public void run() {
buffer.setRenderer(renderer);
renderer.setImageBitmap(bitmap, false);
Bitmap result = buffer.getBitmap();
- mFilter.onDestroy();
+ mFilter.destroy();
renderer.deleteImage();
buffer.destroy();
@@ -296,7 +296,7 @@ public static void getBitmapForMultipleFilters(final Bitmap bitmap,
for (GPUImageFilter filter : filters) {
renderer.setFilter(filter);
listener.response(buffer.getBitmap());
- filter.onDestroy();
+ filter.destroy();
}
renderer.deleteImage();
buffer.destroy();
@@ -37,7 +37,6 @@
private int mBrightnessLocation;
private float mBrightness;
- private boolean mIsInitialized = false;
public GPUImageBrightnessFilter() {
this(0.0f);
@@ -52,14 +51,16 @@ public GPUImageBrightnessFilter(final float brightness) {
public void onInit() {
super.onInit();
mBrightnessLocation = GLES20.glGetUniformLocation(getProgram(), "brightness");
- mIsInitialized = true;
+ }
+
+ @Override
+ public void onInitialized() {
+ super.onInitialized();
setBrightness(mBrightness);
}
public void setBrightness(final float brightness) {
mBrightness = brightness;
- if (mIsInitialized) {
- setFloat(mBrightnessLocation, mBrightness);
- }
+ setFloat(mBrightnessLocation, mBrightness);
}
}
@@ -42,7 +42,6 @@
private float[] mColorMatrix;
private int mColorMatrixLocation;
private int mIntensityLocation;
- private boolean mIsInitialized = false;
public GPUImageColorMatrixFilter() {
this(1.0f, new float[] {
@@ -64,22 +63,22 @@ public void onInit() {
super.onInit();
mColorMatrixLocation = GLES20.glGetUniformLocation(getProgram(), "colorMatrix");
mIntensityLocation = GLES20.glGetUniformLocation(getProgram(), "intensity");
- mIsInitialized = true;
+ }
+
+ @Override
+ public void onInitialized() {
+ super.onInitialized();
setIntensity(mIntensity);
setColorMatrix(mColorMatrix);
}
public void setIntensity(final float intensity) {
mIntensity = intensity;
- if (mIsInitialized) {
- setFloat(mIntensityLocation, intensity);
- }
+ setFloat(mIntensityLocation, intensity);
}
public void setColorMatrix(final float[] colorMatrix) {
mColorMatrix = colorMatrix;
- if (mIsInitialized) {
- setUniformMatrix4f(mColorMatrixLocation, colorMatrix);
- }
+ setUniformMatrix4f(mColorMatrixLocation, colorMatrix);
}
}
@@ -39,7 +39,6 @@
private int mContrastLocation;
private float mContrast;
- private boolean mIsInitialized = false;
public GPUImageContrastFilter() {
this(1.2f);
@@ -54,14 +53,16 @@ public GPUImageContrastFilter(float contrast) {
public void onInit() {
super.onInit();
mContrastLocation = GLES20.glGetUniformLocation(getProgram(), "contrast");
- mIsInitialized = true;
+ }
+
+ @Override
+ public void onInitialized() {
+ super.onInitialized();
setContrast(mContrast);
}
public void setContrast(final float contrast) {
mContrast = contrast;
- if (mIsInitialized) {
- setFloat(mContrastLocation, mContrast);
- }
+ setFloat(mContrastLocation, mContrast);
}
}
@@ -16,11 +16,11 @@
package jp.co.cyberagent.android.gpuimage;
+import android.opengl.GLES20;
+
import java.nio.FloatBuffer;
import java.util.LinkedList;
-import android.opengl.GLES20;
-
public class GPUImageFilter {
public static final String NO_FILTER_VERTEX_SHADER = "" +
"attribute vec4 position;\n" +
@@ -64,13 +64,26 @@ public GPUImageFilter(final String vertexShader, final String fragmentShader) {
mFragmentShader = fragmentShader;
}
+ public final void init() {
+ onInit();
+ mIsInitialized = true;
+ onInitialized();
+ }
+
public void onInit() {
mGLProgId = OpenGlUtils.loadProgram(mVertexShader, mFragmentShader);
mGLAttribPosition = GLES20.glGetAttribLocation(mGLProgId, "position");
mGLUniformTexture = GLES20.glGetUniformLocation(mGLProgId, "inputImageTexture");
mGLAttribTextureCoordinate = GLES20.glGetAttribLocation(mGLProgId,
"inputTextureCoordinate");
- mIsInitialized = true;
+ }
+
+ public void onInitialized() {
+ }
+
+ public final void destroy() {
+ mIsInitialized = false;
+ onDestroy();
}
public void onDestroy() {
@@ -16,15 +16,15 @@
package jp.co.cyberagent.android.gpuimage;
-import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.CUBE;
-import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.TEXTURE_NO_ROTATION;
+import android.opengl.GLES20;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.List;
-import android.opengl.GLES20;
+import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.CUBE;
+import static jp.co.cyberagent.android.gpuimage.GPUImageRenderer.TEXTURE_NO_ROTATION;
/**
* Resembles a filter that consists of multiple filters applied after each
@@ -65,7 +65,7 @@ public GPUImageFilterGroup(final List<GPUImageFilter> filters) {
public void onInit() {
super.onInit();
for (GPUImageFilter filter : mFilters) {
- filter.onInit();
+ filter.init();
}
}
@@ -77,7 +77,7 @@ public void onInit() {
public void onDestroy() {
destroyFramebuffers();
for (GPUImageFilter filter : mFilters) {
- filter.onDestroy();
+ filter.destroy();
}
super.onDestroy();
}
@@ -37,7 +37,6 @@
private int mGammaLocation;
private float mGamma;
- private boolean mIsInitialized = false;
public GPUImageGammaFilter() {
this(1.2f);
@@ -52,14 +51,16 @@ public GPUImageGammaFilter(final float gamma) {
public void onInit() {
super.onInit();
mGammaLocation = GLES20.glGetUniformLocation(getProgram(), "gamma");
- mIsInitialized = true;
+ }
+
+ @Override
+ public void onInitialized() {
+ super.onInitialized();
setGamma(mGamma);
}
public void setGamma(final float gamma) {
mGamma = gamma;
- if (mIsInitialized) {
- setFloat(mGammaLocation, mGamma);
- }
+ setFloat(mGammaLocation, mGamma);
}
}
@@ -16,17 +16,6 @@
package jp.co.cyberagent.android.gpuimage;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.FloatBuffer;
-import java.nio.IntBuffer;
-import java.util.LinkedList;
-import java.util.Queue;
-
-import javax.microedition.khronos.egl.EGLConfig;
-import javax.microedition.khronos.opengles.GL10;
-
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -37,6 +26,16 @@
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.util.LinkedList;
+import java.util.Queue;
+
@TargetApi(11)
public class GPUImageRenderer implements Renderer, PreviewCallback {
public static final int NO_IMAGE = -1;
@@ -111,7 +110,7 @@ public GPUImageRenderer(final GPUImageFilter filter) {
public void onSurfaceCreated(final GL10 unused, final EGLConfig config) {
GLES20.glClearColor(0, 0, 0, 1);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
- mFilter.onInit();
+ mFilter.init();
}
@Override
@@ -188,9 +187,9 @@ public void run() {
final GPUImageFilter oldFilter = mFilter;
mFilter = filter;
if (oldFilter != null) {
- oldFilter.onDestroy();
+ oldFilter.destroy();
}
- mFilter.onInit();
+ mFilter.init();
GLES20.glUseProgram(mFilter.getProgram());
mFilter.onOutputSizeChanged(mOutputWidth, mOutputHeight);
}
@@ -12,5 +12,5 @@
# Project target.
target=android-16
-android.library.reference.1=../library
-proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+android.library.reference.1=../library

0 comments on commit 9bfe4d0

Please sign in to comment.