diff --git a/README-zh.md b/README-zh.md index 749538c..47dc8e4 100644 --- a/README-zh.md +++ b/README-zh.md @@ -25,13 +25,13 @@ repositories { } dependencies { - compile 'com.github.ChillingVan:android-openGL-canvas:v1.0.1' + compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.0' } ``` ### 样例代码 -自定义一个View. +* 自定义一个View. ```java public class MyGLView extends GLView { @@ -54,10 +54,10 @@ public class MyGLView extends GLView { ![canvas](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/canvas-example.png) -其中, GLContinuouslyView, GLTextureView, GLContinuousTextureView, GLSurfaceTextureProducerView and GLSharedContextView 用法相似. +* 其中, GLContinuouslyView, GLTextureView, GLContinuousTextureView, GLSurfaceTextureProducerView and GLSharedContextView 用法相似. -使用CanvasGL实现绘制 +* 使用CanvasGL实现绘制 ```java canvas.drawBitmap(textBitmap, left, top); @@ -80,10 +80,10 @@ public class MyGLView extends GLView { ![filters](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/filter_example.png) -可以与Camera结合,注意运行样例代码的时候尽量使用真机而不是模拟器。 +* 可以与Camera结合,注意运行样例代码的时候尽量使用真机而不是模拟器。 ![camera](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/camera-example.jpg) - +* 如果不想使用View,可以使用 OffScreenCanvas 实现脱离屏幕的绘制,然后使用getDrawingBitmap方法获取绘制的内容。 ## 注意事项 * 每一个View的onGLDraw都运行在自己的线程而非主线程。 diff --git a/README.md b/README.md index 6447b46..d1a5a91 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ repositories { } dependencies { - compile 'com.github.ChillingVan:android-openGL-canvas:v1.0.1' + compile 'com.github.ChillingVan:android-openGL-canvas:v1.1.0' } ``` ### Sample Code -Custom your view. +* Custom your view. ```java public class MyGLView extends GLView { @@ -56,10 +56,10 @@ public class MyGLView extends GLView { ![canvas](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/canvas-example.png) -The Usage of GLContinuouslyView, GLTextureView, GLContinuousTextureView, GLSurfaceTextureProducerView and GLSharedContextView is similar. +* The Usage of GLContinuouslyView, GLTextureView, GLContinuousTextureView, GLSurfaceTextureProducerView and GLSharedContextView is similar. -Using canvas to draw +* Using canvas to draw ```java canvas.drawBitmap(textBitmap, left, top); @@ -82,14 +82,17 @@ Using canvas to draw ![filters](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/filter_example.png) -And can be used with camera, just run the camera sample code on a real device(not in emulator) to see what will happen. +* And can be used with camera, just run the camera sample code on a real device(not in emulator) to see what will happen. ![camera](https://github.com/ChillingVan/android-openGL-canvas/raw/master/screenshots/camera-example.jpg) +* If you do not want to use GLView, you can use OffScreenCanvas to draw things and fetch it by getDrawingBitmap. + ## Notice * The onGLDraw method in GLView runs in its own thread but not the main thread. * I haven't implemented all the filters in GPUImage. I will add more later. If you need, you can take my code as example to implement your filter. It is simple. +* Remember to call onResume and onPause in the Activity lifecycle when using GLContinuousView and GLContinuousTextureView. ## License Copyright 2012 CyberAgent, Inc. diff --git a/canvasgl/build.gradle b/canvasgl/build.gradle index 669bafa..1ef3650 100644 --- a/canvasgl/build.gradle +++ b/canvasgl/build.gradle @@ -26,8 +26,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 24 - versionCode 1 - versionName "1.0" + versionCode 101000 + versionName "1.1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java b/canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java new file mode 100644 index 0000000..bf061a6 --- /dev/null +++ b/canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java @@ -0,0 +1,140 @@ +package com.chillingvan.canvasgl; + +import android.graphics.Bitmap; +import android.graphics.Rect; +import android.opengl.GLSurfaceView; +import android.os.Handler; + +import com.chillingvan.canvasgl.glview.GLView; +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.EGLDisplay; +import javax.microedition.khronos.egl.EGLSurface; +import javax.microedition.khronos.opengles.GL10; + +/** + * Created by Chilling on 2016/11/7. + */ + +public abstract class OffScreenCanvas implements GLSurfaceView.Renderer{ + + protected final GLThread mGLThread; + private int width; + private int height; + protected ICanvasGL mCanvas; + public GL10 mGL; + + public OffScreenCanvas() { + this(0, 0); + } + + public OffScreenCanvas(int width, int height) { + this.width = width; + this.height = height; + mGLThread = new GLThread.Builder().setRenderMode(getRenderMode()) + .setEglWindowSurfaceFactory(new SurfaceFactory()) + .setRenderer(this).createGLThread(); + } + + public void setSize(int width, int height) { + this.width = width; + this.height = height; + } + + public void start() { + mGLThread.start(); + mGLThread.surfaceCreated(); + mGLThread.onWindowResize(width, height); + mGLThread.requestRender(); + + } + + public void end() { + if (mGLThread != null) { + mGLThread.requestExitAndWait(); + } + } + + @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(); + } + } finally { + super.finalize(); + } + } + + private class SurfaceFactory implements GLThread.EGLWindowSurfaceFactory { + @Override + public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) { + int[] attribList = new int[] { + EGL10.EGL_WIDTH, width, + EGL10.EGL_HEIGHT, height, + EGL10.EGL_NONE + }; + return egl.eglCreatePbufferSurface(display, config, attribList); + } + + @Override + public void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface) { + egl.eglDestroySurface(display, surface); + } + } + + + @Override + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + Loggers.d("BaseGLTextureView", "onSurfaceCreated: "); + mCanvas = new CanvasGL(); + } + + @Override + public void onSurfaceChanged(GL10 gl, int width, int height) { + Loggers.d("BaseGLTextureView", "onSurfaceChanged: "); + mCanvas.setSize(width, height); + + } + + @Override + public void onDrawFrame(GL10 gl) { + mGL = gl; + onGLDraw(mCanvas); + } + + + protected int getRenderMode() { + return GLThread.RENDERMODE_WHEN_DIRTY; + } + + protected abstract void onGLDraw(ICanvasGL canvas); + + + public void getDrawingBitmap(final Rect rect, final GLView.GetDrawingCacheCallback getDrawingCacheCallback) { + final Handler handler = new Handler(); + + mGLThread.queueEvent(new Runnable() { + @Override + public void run() { + if (mGL == null) { + return; + } + onDrawFrame(mGL); + onDrawFrame(mGL); + final Bitmap bitmapFromGLSurface = OpenGLUtil.createBitmapFromGLSurface(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, mGL, height); + handler.post(new Runnable() { + @Override + public void run() { + getDrawingCacheCallback.onFetch(bitmapFromGLSurface); + } + }); + } + }); + mGLThread.requestRender(); + } +} diff --git a/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuouslyView.java b/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuousView.java similarity index 86% rename from canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuouslyView.java rename to canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuousView.java index 0623a7b..716e621 100644 --- a/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuouslyView.java +++ b/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/GLContinuousView.java @@ -28,12 +28,12 @@ * Created by Chilling on 2016/10/24. */ -public abstract class GLContinuouslyView extends GLView { - public GLContinuouslyView(Context context) { +public abstract class GLContinuousView extends GLView { + public GLContinuousView(Context context) { super(context); } - public GLContinuouslyView(Context context, AttributeSet attrs) { + public GLContinuousView(Context context, AttributeSet attrs) { super(context, attrs); } diff --git a/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/texture/gles/GLThread.java b/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/texture/gles/GLThread.java index b258709..e67bdd8 100644 --- a/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/texture/gles/GLThread.java +++ b/canvasgl/src/main/java/com/chillingvan/canvasgl/glview/texture/gles/GLThread.java @@ -1049,7 +1049,7 @@ public GLThread createGLThread() { if (renderer == null) { throw new NullPointerException("renderer has not been set"); } - if (surface == null) { + if (surface == null && eglWindowSurfaceFactory == null) { throw new NullPointerException("surface has not been set"); } if (configChooser == null) { diff --git a/canvasglsample/src/main/AndroidManifest.xml b/canvasglsample/src/main/AndroidManifest.xml index 092192e..aaebc6c 100644 --- a/canvasglsample/src/main/AndroidManifest.xml +++ b/canvasglsample/src/main/AndroidManifest.xml @@ -1,11 +1,9 @@ - - - - + + - - + + + + + + + + diff --git a/canvasglsample/src/main/java/com/chillingvan/canvasglsample/animation/AnimGLView.java b/canvasglsample/src/main/java/com/chillingvan/canvasglsample/animation/AnimGLView.java index 94ebf5d..a1ffb49 100644 --- a/canvasglsample/src/main/java/com/chillingvan/canvasglsample/animation/AnimGLView.java +++ b/canvasglsample/src/main/java/com/chillingvan/canvasglsample/animation/AnimGLView.java @@ -22,7 +22,7 @@ import android.util.AttributeSet; import com.chillingvan.canvasgl.ICanvasGL; -import com.chillingvan.canvasgl.glview.GLContinuouslyView; +import com.chillingvan.canvasgl.glview.GLContinuousView; import com.chillingvan.canvasglsample.animation.bubble.Bubble; import com.chillingvan.canvasglsample.animation.bubble.MovableObj; import com.chillingvan.canvasglsample.animation.bubble.Wall; @@ -34,7 +34,7 @@ * Created by Chilling on 2016/10/24. */ -public class AnimGLView extends GLContinuouslyView { +public class AnimGLView extends GLContinuousView { public static final int INTERNVAL_TIME_MS = 16; private List bubbles = new ArrayList<>(); private Wall wallTop = new Wall.WallY(0); diff --git a/canvasglsample/src/main/java/com/chillingvan/canvasglsample/offscreen/OffScreenActivity.java b/canvasglsample/src/main/java/com/chillingvan/canvasglsample/offscreen/OffScreenActivity.java new file mode 100644 index 0000000..6616762 --- /dev/null +++ b/canvasglsample/src/main/java/com/chillingvan/canvasglsample/offscreen/OffScreenActivity.java @@ -0,0 +1,46 @@ +package com.chillingvan.canvasglsample.offscreen; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Rect; +import android.os.Bundle; +import android.widget.ImageView; + +import com.chillingvan.canvasgl.ICanvasGL; +import com.chillingvan.canvasgl.OffScreenCanvas; +import com.chillingvan.canvasgl.glview.GLView; +import com.chillingvan.canvasglsample.R; + +public class OffScreenActivity extends Activity { + + private ImageView imageView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_off_screen); + + imageView = (ImageView) findViewById(R.id.off_screen_img_v); + + OffScreenCanvas offScreenCanvas = new OffScreenCanvas(400, 400) { + @Override + protected void onGLDraw(ICanvasGL canvas) { + Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna); + canvas.drawBitmap(bitmap, 0, 0); + runOnUiThread(new Runnable() { + @Override + public void run() { + getDrawingBitmap(new Rect(0, 0, 300, 300), new GLView.GetDrawingCacheCallback() { + @Override + public void onFetch(final Bitmap bitmap) { + imageView.setImageBitmap(bitmap); + } + }); + } + }); + } + }; + offScreenCanvas.start(); + } +} diff --git a/canvasglsample/src/main/res/layout/activity_off_screen.xml b/canvasglsample/src/main/res/layout/activity_off_screen.xml new file mode 100644 index 0000000..7d35d38 --- /dev/null +++ b/canvasglsample/src/main/res/layout/activity_off_screen.xml @@ -0,0 +1,23 @@ + + + + + + + +