Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use interface named GLRenderView to reduce if And else #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import android.os.Handler;
import android.provider.MediaStore;
import android.view.Display;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.WindowManager;

import java.io.File;
Expand All @@ -47,6 +49,7 @@

import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.util.Rotation;
import jp.co.cyberagent.android.gpuimage.view.GLRenderView;

/**
* The main accessor for GPUImage functionality. This class helps to do common
Expand All @@ -61,9 +64,7 @@ public enum ScaleType {CENTER_INSIDE, CENTER_CROP}

private final Context context;
private final GPUImageRenderer renderer;
private int surfaceType = SURFACE_TYPE_SURFACE_VIEW;
private GLSurfaceView glSurfaceView;
private GLTextureView glTextureView;
private GLRenderView glRenderView;
private GPUImageFilter filter;
private Bitmap currentBitmap;
private ScaleType scaleType = ScaleType.CENTER_CROP;
Expand Down Expand Up @@ -99,35 +100,22 @@ private boolean supportsOpenGLES2(final Context context) {
}

/**
* Sets the GLSurfaceView which will display the preview.
* Sets the GLRenderView which will display the preview.
*
* @param view the GLSurfaceView
* @param view the GLRenderView instance
*/
public void setGLSurfaceView(final GLSurfaceView view) {
surfaceType = SURFACE_TYPE_SURFACE_VIEW;
glSurfaceView = view;
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
glSurfaceView.setRenderer(renderer);
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
glSurfaceView.requestRender();
}

/**
* Sets the GLTextureView which will display the preview.
*
* @param view the GLTextureView
*/
public void setGLTextureView(final GLTextureView view) {
surfaceType = SURFACE_TYPE_TEXTURE_VIEW;
glTextureView = view;
glTextureView.setEGLContextClientVersion(2);
glTextureView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glTextureView.setOpaque(false);
glTextureView.setRenderer(renderer);
glTextureView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
glTextureView.requestRender();
public void setGLRenderView(final GLRenderView view) {
glRenderView = view;
glRenderView.setEGLContextClientVersion(2);
glRenderView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
if (glRenderView instanceof TextureView) {
((TextureView) glRenderView).setOpaque(false);
} else if (glRenderView instanceof SurfaceView) {
((SurfaceView) glRenderView).getHolder().setFormat(PixelFormat.RGBA_8888);
}
glRenderView.setRender(renderer);
glRenderView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
glRenderView.requestRender();
}

/**
Expand All @@ -145,14 +133,8 @@ public void setBackgroundColor(float red, float green, float blue) {
* Request the preview to be rendered again.
*/
public void requestRender() {
if (surfaceType == SURFACE_TYPE_SURFACE_VIEW) {
if (glSurfaceView != null) {
glSurfaceView.requestRender();
}
} else if (surfaceType == SURFACE_TYPE_TEXTURE_VIEW) {
if (glTextureView != null) {
glTextureView.requestRender();
}
if (glRenderView != null) {
glRenderView.requestRender();
}
}

Expand Down Expand Up @@ -183,11 +165,7 @@ public void setUpCamera(final Camera camera) {
@Deprecated
public void setUpCamera(final Camera camera, final int degrees, final boolean flipHorizontal,
final boolean flipVertical) {
if (surfaceType == SURFACE_TYPE_SURFACE_VIEW) {
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
} else if (surfaceType == SURFACE_TYPE_TEXTURE_VIEW) {
glTextureView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
glRenderView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
renderer.setUpSurfaceTexture(camera);
Rotation rotation = Rotation.NORMAL;
switch (degrees) {
Expand Down Expand Up @@ -259,7 +237,7 @@ public void setScaleType(ScaleType scaleType) {
* @return array with width and height of bitmap image
*/
public int[] getScaleSize() {
return new int[] {scaleWidth, scaleHeight};
return new int[]{scaleWidth, scaleHeight};
}

/**
Expand Down Expand Up @@ -352,7 +330,7 @@ public Bitmap getBitmapWithFilterApplied(final Bitmap bitmap) {
* @return the bitmap with filter applied
*/
public Bitmap getBitmapWithFilterApplied(final Bitmap bitmap, boolean recycle) {
if (glSurfaceView != null || glTextureView != null) {
if (glRenderView != null) {
renderer.deleteImage();
renderer.runOnDraw(new Runnable() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
import jp.co.cyberagent.android.gpuimage.util.OpenGlUtils;
import jp.co.cyberagent.android.gpuimage.util.Rotation;
import jp.co.cyberagent.android.gpuimage.util.TextureRotationUtil;
import jp.co.cyberagent.android.gpuimage.view.GLRenderView;

import static jp.co.cyberagent.android.gpuimage.util.TextureRotationUtil.TEXTURE_NO_ROTATION;

public class GPUImageRenderer implements GLSurfaceView.Renderer, GLTextureView.Renderer, PreviewCallback {
public class GPUImageRenderer implements GLSurfaceView.Renderer, GLTextureView.Renderer, PreviewCallback, GLRenderView.Renderer {
private static final int NO_IMAGE = -1;
public static final float CUBE[] = {
-1.0f, -1.0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@

import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.util.Rotation;
import jp.co.cyberagent.android.gpuimage.view.GLRenderView;

import static jp.co.cyberagent.android.gpuimage.GPUImage.SURFACE_TYPE_SURFACE_VIEW;
import static jp.co.cyberagent.android.gpuimage.GPUImage.SURFACE_TYPE_TEXTURE_VIEW;

public class GPUImageView extends FrameLayout {

private int surfaceType = SURFACE_TYPE_SURFACE_VIEW;
private View surfaceView;
private GLRenderView surfaceView;
private GPUImage gpuImage;
private boolean isShowLoading = true;
private GPUImageFilter filter;
Expand Down Expand Up @@ -83,12 +84,14 @@ private void init(Context context, AttributeSet attrs) {
gpuImage = new GPUImage(context);
if (surfaceType == SURFACE_TYPE_TEXTURE_VIEW) {
surfaceView = new GPUImageGLTextureView(context, attrs);
gpuImage.setGLTextureView((GLTextureView) surfaceView);
} else {
surfaceView = new GPUImageGLSurfaceView(context, attrs);
gpuImage.setGLSurfaceView((GLSurfaceView) surfaceView);
}
addView(surfaceView);
gpuImage.setGLRenderView(surfaceView);

if (surfaceView instanceof View) {
addView((View) surfaceView);
}
}

@Override
Expand Down Expand Up @@ -451,7 +454,7 @@ public Size(int width, int height) {
}
}

private class GPUImageGLSurfaceView extends GLSurfaceView {
private class GPUImageGLSurfaceView extends GLSurfaceView implements GLRenderView {
public GPUImageGLSurfaceView(Context context) {
super(context);
}
Expand All @@ -469,9 +472,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

@Override
public void setRender(GLRenderView.Renderer render) {
if (render instanceof GLSurfaceView.Renderer) {
setRenderer((GLSurfaceView.Renderer) render);
}
}
}

private class GPUImageGLTextureView extends GLTextureView {
private class GPUImageGLTextureView extends GLTextureView implements GLRenderView {
public GPUImageGLTextureView(Context context) {
super(context);
}
Expand All @@ -489,6 +499,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}


@Override
public void setRender(GLRenderView.Renderer render) {
if (render instanceof GLTextureView.Renderer) {
setRenderer((GLTextureView.Renderer) render);
}
}
}

private class LoadingView extends FrameLayout {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package jp.co.cyberagent.android.gpuimage.view;

import android.opengl.GLSurfaceView;

import jp.co.cyberagent.android.gpuimage.GLTextureView;

/*
* Copyright (C) 2022 MichaelX
*
* 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.
*/
public interface GLRenderView extends ViewCompat {
/**
* @see android.opengl.GLSurfaceView#setEGLContextClientVersion(int)
* @see jp.co.cyberagent.android.gpuimage.GLTextureView#setEGLContextClientVersion(int)
*/
void setEGLContextClientVersion(int glVersion);

/**
* @see android.opengl.GLSurfaceView#setEGLConfigChooser(int, int, int, int, int, int)
* @see jp.co.cyberagent.android.gpuimage.GLTextureView#setEGLConfigChooser(int, int, int, int, int, int)
*/
void setEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize,
int depthSize, int stencilSize);

/**
* adapter for {@link android.opengl.GLSurfaceView#setRenderer(GLSurfaceView.Renderer)}
* and {@link jp.co.cyberagent.android.gpuimage.GLTextureView#setRenderer(GLTextureView.Renderer)}
*/
void setRender(Renderer render);

/**
* @see GLSurfaceView#setRenderMode(int)
* @see GLTextureView#setRenderMode(int)
*/
void setRenderMode(int mode);

/**
* @see GLSurfaceView#requestRender()
* @see GLTextureView#requestRender()
*/
void requestRender();

/**
* {@link GLSurfaceView.Renderer}
* {@link GLTextureView.Renderer}
*/
interface Renderer {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jp.co.cyberagent.android.gpuimage.view;

import android.view.View;

/*
* Copyright (C) 2022 MichaelX
*
* 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.
*/
public interface ViewCompat {

/**
* @return The raw measured width of this view.
* @see View#getMeasuredWidth()
*/
int getMeasuredWidth();

/**
* @return The raw measured height of this view.
* @see View#getMeasuredHeight() ()
*/
int getMeasuredHeight();

/**
* @see View#requestLayout()
*/
void requestLayout();
}