Skip to content

Commit

Permalink
to 1.1.2;draw circle-opaque;offScreenCanvas can shared context, can p…
Browse files Browse the repository at this point in the history
…roduce texture
  • Loading branch information
ChillingVan committed Nov 17, 2016
1 parent 0d5afd4 commit d438ba3
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 79 deletions.
4 changes: 3 additions & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Idea from:
* The canvasGL provides similar API as android canvas. And there are GLViews that can be extended to custom your own view to draw things with OpenGL.
* Similar to the filters of GPUImage, you can apply the filter to the bitmap draw into the GLViews.
* Provides GLViews that using GLSurfaceView and TextureView.

* The GLContinuousView can provide high performance continuous rendering animation because it uses openGL to draw in its own thread.
![anim](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/anim-activity-example.png)

Compare to GPUImage:
* Provide the continuous rendering Thread in GLContinuousView and GLContinuousTextureView.
Expand All @@ -36,7 +38,7 @@ allprojects {
// module build.gradle
dependencies {
compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.1'
compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.2'
}
```

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
* 提供一个类似Android Canvas类的使用OpenGL来实现绘制的canvasGL。可以像传统自定义View那样直接继承 GLViews, 再使用这个canvas绘制需要的东西。
* 提供类似 GPUImage 里的Filter的API,可以在使用canvasGL画东西时实现图像处理。
* 提供的View是继承 GLSurfaceView 或 TextureView 的,可以使用这两种View的特性,特别是TextureView的特性。

* 另外,因为使用OpenGL在另一线程渲染,所以里面的 GLContinuousView 还提供能够实现高性能的动画的方法。
![anim](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/anim-activity-example.png)


与GPUImage对比:
* 提供无限循环渲染线程的 GLContinuousView 和 GLContinuousTextureView。
Expand All @@ -38,7 +41,7 @@ allprojects {
// module build.gradle
dependencies {
compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.1'
compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.2'
}
```

Expand Down Expand Up @@ -105,6 +108,11 @@ public class MyGLView extends GLView {
* 每一个View的onGLDraw都运行在自己的线程而非主线程。
* 目前的Filter没有GPUImage里那么多,后续会陆续增加,如果有需要,参照我的代码自己实现即可,难度不高。

## 相关博客文章
[OpenGL绘制一张图片的流程](http://www.jianshu.com/p/40521c92ef85)
[如何封装 opengl 流程](http://www.jianshu.com/p/c45d11627c70)
[代替GLSurfaceView的GLTextureView](http://www.jianshu.com/p/5a127d43b39a)

## License
Copyright 2016 ChillingVan.

Expand Down
4 changes: 2 additions & 2 deletions canvasgl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 101010
versionName "1.1.1"
versionCode 101020
versionName "1.1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
19 changes: 17 additions & 2 deletions canvasgl/src/main/java/com/chillingvan/canvasgl/CanvasGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.chillingvan.canvasgl.glcanvas.GLCanvas;
import com.chillingvan.canvasgl.glcanvas.GLES20Canvas;
import com.chillingvan.canvasgl.glcanvas.GLPaint;
import com.chillingvan.canvasgl.glcanvas.RawTexture;
import com.chillingvan.canvasgl.shapeFilter.BasicDrawShapeFilter;
import com.chillingvan.canvasgl.shapeFilter.DrawCircleFilter;
import com.chillingvan.canvasgl.shapeFilter.DrawShapeFilter;
Expand Down Expand Up @@ -98,6 +99,16 @@ public BitmapTexture bindBitmapToTexture(int whichTexture, Bitmap bitmap) {
return texture;
}

@Override
public void beginRenderTarget(RawTexture texture) {
glCanvas.beginRenderTarget(texture);
}

@Override
public void endRenderTarget() {
glCanvas.endRenderTarget();
}

@Override
public GLCanvas getGlCanvas() {
return glCanvas;
Expand All @@ -110,8 +121,12 @@ public void drawSurfaceTexture(BasicTexture texture, SurfaceTexture surfaceTextu

@Override
public void drawSurfaceTexture(BasicTexture texture, SurfaceTexture surfaceTexture, int left, int top, int right, int bottom, TextureFilter basicTextureFilter) {
surfaceTexture.getTransformMatrix(surfaceTextureMatrix);
glCanvas.drawTexture(texture, surfaceTextureMatrix, left, top, right - left, bottom - top, basicTextureFilter);
if (surfaceTexture == null) {
glCanvas.drawTexture(texture, left, top, right - left, bottom - top, basicTextureFilter);
} else {
surfaceTexture.getTransformMatrix(surfaceTextureMatrix);
glCanvas.drawTexture(texture, surfaceTextureMatrix, left, top, right - left, bottom - top, basicTextureFilter);
}
}


Expand Down
10 changes: 8 additions & 2 deletions canvasgl/src/main/java/com/chillingvan/canvasgl/ICanvasGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import android.graphics.SurfaceTexture;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.chillingvan.canvasgl.glcanvas.BasicTexture;
import com.chillingvan.canvasgl.glcanvas.BitmapTexture;
import com.chillingvan.canvasgl.glcanvas.GLCanvas;
import com.chillingvan.canvasgl.glcanvas.GLPaint;
import com.chillingvan.canvasgl.glcanvas.RawTexture;
import com.chillingvan.canvasgl.textureFilter.TextureFilter;

/**
Expand All @@ -41,11 +43,15 @@ public interface ICanvasGL {

BitmapTexture bindBitmapToTexture(int whichTexture, Bitmap bitmap);

void beginRenderTarget(RawTexture texture);

void endRenderTarget();

GLCanvas getGlCanvas();

void drawSurfaceTexture(BasicTexture texture, @NonNull SurfaceTexture surfaceTexture, int left, int top, int right, int bottom);
void drawSurfaceTexture(BasicTexture texture, @Nullable SurfaceTexture surfaceTexture, int left, int top, int right, int bottom);

void drawSurfaceTexture(BasicTexture texture, @NonNull SurfaceTexture surfaceTexture, int left, int top, int right, int bottom, TextureFilter textureFilter);
void drawSurfaceTexture(BasicTexture texture, @Nullable SurfaceTexture surfaceTexture, int left, int top, int right, int bottom, TextureFilter textureFilter);

void drawBitmap(Bitmap bitmap, @NonNull CanvasGL.BitmapMatrix matrix);

Expand Down
102 changes: 91 additions & 11 deletions canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.support.annotation.Nullable;

import com.chillingvan.canvasgl.glcanvas.BasicTexture;
import com.chillingvan.canvasgl.glcanvas.RawTexture;
import com.chillingvan.canvasgl.glview.GLView;
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
import com.chillingvan.canvasgl.glview.texture.gles.GLThread;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;
Expand All @@ -38,53 +45,106 @@
* Created by Chilling on 2016/11/7.
*/

public abstract class OffScreenCanvas implements GLSurfaceView.Renderer{
public abstract class OffScreenCanvas implements GLSurfaceView.Renderer {

protected final GLThread mGLThread;
private int width;
private int height;
protected ICanvasGL mCanvas;
public GL10 mGL;

private BasicTexture outsideSharedTexture;
private SurfaceTexture outsideSharedSurfaceTexture;


private GLSurfaceTextureProducerView.OnSurfaceTextureSet onSurfaceTextureSet;
private SurfaceTexture producedSurfaceTexture;
private RawTexture producedRawTexture;
private Handler handler;
private boolean isStart;
private int producedTextureTarget = GLES20.GL_TEXTURE_2D;

public OffScreenCanvas() {
this(0, 0);
this(0, 0, EGL10.EGL_NO_CONTEXT);
}

public OffScreenCanvas(int width, int height) {
this(width, height, EGL10.EGL_NO_CONTEXT);
}

public OffScreenCanvas(int width, int height, EGLContext sharedEglContext) {
this.width = width;
this.height = height;
mGLThread = new GLThread.Builder().setRenderMode(getRenderMode())
.setSharedEglContext(sharedEglContext)
.setEglWindowSurfaceFactory(new SurfaceFactory())
.setRenderer(this).createGLThread();
handler = new Handler();
}

/**
* If it is used, it must be called before start() called.
* @param producedTextureTarget GLES20.GL_TEXTURE_2D or GLES11Ext.GL_TEXTURE_EXTERNAL_OES
*/
public void setProducedTextureTarget(int producedTextureTarget) {
this.producedTextureTarget = producedTextureTarget;
}

/**
* If it is used, it must be called before start() called.
*/
public void setOnCreateGLContextListener(GLThread.OnCreateGLContextListener onCreateGLContextListener) {
mGLThread.setOnCreateGLContextListener(onCreateGLContextListener);
}


/**
* If it is used, it must be called before start() called.
*/
public void setOnSurfaceTextureSet(GLSurfaceTextureProducerView.OnSurfaceTextureSet onSurfaceTextureSet) {
this.onSurfaceTextureSet = onSurfaceTextureSet;
}

public void setSharedTexture(BasicTexture outsideTexture, @Nullable SurfaceTexture outsideSurfaceTexture) {
this.outsideSharedTexture = outsideTexture;
this.outsideSharedSurfaceTexture = outsideSurfaceTexture;
}

public void setSize(int width, int height) {
this.width = width;
this.height = height;
if (isStart) {
mGLThread.onWindowResize(width, height);
}
}

public void start() {
mGLThread.start();
mGLThread.surfaceCreated();
mGLThread.onWindowResize(width, height);
mGLThread.requestRenderAndWait();

isStart = true;
}

public void end() {
if (mGLThread != null) {
mGLThread.requestExitAndWait();
}

if (producedRawTexture != null) {
producedRawTexture.recycle();
producedRawTexture = null;
}
if (producedSurfaceTexture != null) {
producedSurfaceTexture.release();
producedSurfaceTexture = null;
}
}

@Override
protected void finalize() throws Throwable {
try {
if (mGLThread != null) {
// GLThread may still be running if this view was never
// attached to a window.
mGLThread.requestExitAndWait();
}
end();
} finally {
super.finalize();
}
Expand All @@ -93,7 +153,7 @@ protected void finalize() throws Throwable {
private class SurfaceFactory implements GLThread.EGLWindowSurfaceFactory {
@Override
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) {
int[] attribList = new int[] {
int[] attribList = new int[]{
EGL10.EGL_WIDTH, width,
EGL10.EGL_HEIGHT, height,
EGL10.EGL_NONE
Expand All @@ -119,20 +179,40 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
public void onSurfaceChanged(GL10 gl, int width, int height) {
Loggers.d("BaseGLTextureView", "onSurfaceChanged: ");
mCanvas.setSize(width, height);
if (producedRawTexture == null) {
// producedRawTexture = new RawTexture(width, height, false, GLES11Ext.GL_TEXTURE_EXTERNAL_OES);
producedRawTexture = new RawTexture(width, height, false, producedTextureTarget);
if (!producedRawTexture.isLoaded()) {
producedRawTexture.prepare(mCanvas.getGlCanvas());
}
producedSurfaceTexture = new SurfaceTexture(producedRawTexture.getId());
handler.post(new Runnable() {
@Override
public void run() {
if (onSurfaceTextureSet != null) {
onSurfaceTextureSet.onSet(producedSurfaceTexture, producedRawTexture);
}
}
});
} else {
producedRawTexture.setSize(width, height);
}

}


@Override
public void onDrawFrame(GL10 gl) {
onGLDraw(mCanvas);
producedSurfaceTexture.updateTexImage();
onGLDraw(mCanvas, producedSurfaceTexture, producedRawTexture, outsideSharedSurfaceTexture, outsideSharedTexture);
}


protected int getRenderMode() {
return GLThread.RENDERMODE_WHEN_DIRTY;
}

protected abstract void onGLDraw(ICanvasGL canvas);
protected abstract void onGLDraw(ICanvasGL canvas, SurfaceTexture producedSurfaceTexture, RawTexture producedRawTexture, @Nullable SurfaceTexture outsideSharedSurfaceTexture, @Nullable BasicTexture outsideSharedTexture);

public void queueEvent(Runnable r) {
if (mGLThread == null) {
Expand Down
Loading

0 comments on commit d438ba3

Please sign in to comment.