-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
matthewfan
committed
Nov 7, 2016
1 parent
22625ba
commit 562d79d
Showing
10 changed files
with
245 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
canvasgl/src/main/java/com/chillingvan/canvasgl/OffScreenCanvas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
canvasglsample/src/main/java/com/chillingvan/canvasglsample/offscreen/OffScreenActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
canvasglsample/src/main/res/layout/activity_off_screen.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |