Skip to content

Commit

Permalink
off screen ; up to 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfan committed Nov 7, 2016
1 parent 22625ba commit 562d79d
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 27 deletions.
12 changes: 6 additions & 6 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);

Expand All @@ -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都运行在自己的线程而非主线程。
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions canvasgl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
140 changes: 140 additions & 0 deletions canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 14 additions & 8 deletions canvasglsample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest package="com.chillingvan.canvasglsample"
xmlns:android="http://schemas.android.com/apk/res/android">


<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
Expand Down Expand Up @@ -47,13 +45,21 @@
<category android:name="com.chillingvan.canvasglsample"/>
</intent-filter>
</activity>
<activity android:name=".textureView.TextureCameraActivity"
android:label="TextureCamera"
>

<activity
android:name=".textureView.TextureCameraActivity"
android:label="TextureCamera">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="com.chillingvan.canvasglsample"/>
</intent-filter>
</activity>
<activity android:name=".offscreen.OffScreenActivity"
android:label="OffScreenActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="com.chillingvan.canvasglsample"/>
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Bubble> bubbles = new ArrayList<>();
private Wall wallTop = new Wall.WallY(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
23 changes: 23 additions & 0 deletions canvasglsample/src/main/res/layout/activity_off_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_off_screen"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chillingvan.canvasglsample.offscreen.OffScreenActivity">

<ImageView
android:id="@+id/off_screen_img_v"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
/>



</RelativeLayout>

0 comments on commit 562d79d

Please sign in to comment.