|
|
@ -58,7 +58,7 @@ import javax.microedition.khronos.opengles.GL10; |
|
|
|
* Callbacks are guaranteed to be called on the renderer thread, which means that we can fetch |
|
|
|
* Callbacks are guaranteed to be called on the renderer thread, which means that we can fetch |
|
|
|
* the GL context that was created and is managed by the {@link GLSurfaceView}. |
|
|
|
* the GL context that was created and is managed by the {@link GLSurfaceView}. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture> { |
|
|
|
public class GlCameraPreview extends FilterCameraPreview<GLSurfaceView, SurfaceTexture> { |
|
|
|
|
|
|
|
|
|
|
|
private boolean mDispatched; |
|
|
|
private boolean mDispatched; |
|
|
|
private final float[] mTransformMatrix = new float[16]; |
|
|
|
private final float[] mTransformMatrix = new float[16]; |
|
|
@ -69,8 +69,7 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
@VisibleForTesting float mCropScaleX = 1F; |
|
|
|
@VisibleForTesting float mCropScaleX = 1F; |
|
|
|
@VisibleForTesting float mCropScaleY = 1F; |
|
|
|
@VisibleForTesting float mCropScaleY = 1F; |
|
|
|
private View mRootView; |
|
|
|
private View mRootView; |
|
|
|
|
|
|
|
private Filter mCurrentFilter; |
|
|
|
private Filter mCurrentShaderEffect; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public GlCameraPreview(@NonNull Context context, @NonNull ViewGroup parent) { |
|
|
|
public GlCameraPreview(@NonNull Context context, @NonNull ViewGroup parent) { |
|
|
|
super(context, parent); |
|
|
|
super(context, parent); |
|
|
@ -142,7 +141,8 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
@RendererThread |
|
|
|
@RendererThread |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
|
|
|
public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
|
|
|
mOutputViewport = new EglViewport(); |
|
|
|
if (mCurrentFilter == null) mCurrentFilter = new NoFilter(); |
|
|
|
|
|
|
|
mOutputViewport = new EglViewport(mCurrentFilter); |
|
|
|
mOutputTextureId = mOutputViewport.createTexture(); |
|
|
|
mOutputTextureId = mOutputViewport.createTexture(); |
|
|
|
mInputSurfaceTexture = new SurfaceTexture(mOutputTextureId); |
|
|
|
mInputSurfaceTexture = new SurfaceTexture(mOutputTextureId); |
|
|
|
getView().queueEvent(new Runnable() { |
|
|
|
getView().queueEvent(new Runnable() { |
|
|
@ -165,15 +165,13 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
getView().requestRender(); // requestRender is thread-safe.
|
|
|
|
getView().requestRender(); // requestRender is thread-safe.
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//init the default shader effect
|
|
|
|
|
|
|
|
mCurrentShaderEffect = new NoFilter(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@RendererThread |
|
|
|
@RendererThread |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onSurfaceChanged(GL10 gl, final int width, final int height) { |
|
|
|
public void onSurfaceChanged(GL10 gl, final int width, final int height) { |
|
|
|
gl.glViewport(0, 0, width, height); |
|
|
|
gl.glViewport(0, 0, width, height); |
|
|
|
|
|
|
|
mCurrentFilter.setPreviewingViewSize(width, height); |
|
|
|
if (!mDispatched) { |
|
|
|
if (!mDispatched) { |
|
|
|
dispatchOnSurfaceAvailable(width, height); |
|
|
|
dispatchOnSurfaceAvailable(width, height); |
|
|
|
mDispatched = true; |
|
|
|
mDispatched = true; |
|
|
@ -216,7 +214,7 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
synchronized (mRendererFrameCallbacks) { |
|
|
|
synchronized (mRendererFrameCallbacks) { |
|
|
|
// Need to synchronize when iterating the Collections.synchronizedSet
|
|
|
|
// Need to synchronize when iterating the Collections.synchronizedSet
|
|
|
|
for (RendererFrameCallback callback : mRendererFrameCallbacks) { |
|
|
|
for (RendererFrameCallback callback : mRendererFrameCallbacks) { |
|
|
|
callback.onRendererFrame(mInputSurfaceTexture, mCropScaleX, mCropScaleY, mCurrentShaderEffect); |
|
|
|
callback.onRendererFrame(mInputSurfaceTexture, mCropScaleX, mCropScaleY); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -284,7 +282,7 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
mRendererFrameCallbacks.add(callback); |
|
|
|
mRendererFrameCallbacks.add(callback); |
|
|
|
if (mOutputTextureId != 0) callback.onRendererTextureCreated(mOutputTextureId); |
|
|
|
if (mOutputTextureId != 0) callback.onRendererTextureCreated(mOutputTextureId); |
|
|
|
callback.onFilterChanged(mCurrentShaderEffect); |
|
|
|
callback.onFilterChanged(mCurrentFilter); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -317,14 +315,30 @@ public class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture |
|
|
|
return new Renderer(); |
|
|
|
return new Renderer(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setShaderEffect(@NonNull Filter shaderEffect){ |
|
|
|
//region Filters
|
|
|
|
shaderEffect.setPreviewingViewSize(getView().getWidth(), getView().getHeight()); |
|
|
|
|
|
|
|
mCurrentShaderEffect = shaderEffect; |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Filter getCurrentFilter() { |
|
|
|
|
|
|
|
return mCurrentFilter; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mOutputViewport.changeShaderFilter(shaderEffect); |
|
|
|
@Override |
|
|
|
|
|
|
|
public void setFilter(@NonNull Filter filter) { |
|
|
|
|
|
|
|
if (hasSurface()) { |
|
|
|
|
|
|
|
filter.setPreviewingViewSize(mOutputSurfaceWidth, mOutputSurfaceHeight); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mCurrentFilter = filter; |
|
|
|
|
|
|
|
if (mOutputViewport != null) { |
|
|
|
|
|
|
|
mOutputViewport.changeShaderFilter(filter); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (RendererFrameCallback callback : mRendererFrameCallbacks) { |
|
|
|
// Need to synchronize when iterating the Collections.synchronizedSet
|
|
|
|
callback.onFilterChanged(shaderEffect); |
|
|
|
synchronized (mRendererFrameCallbacks) { |
|
|
|
|
|
|
|
for (RendererFrameCallback callback : mRendererFrameCallbacks) { |
|
|
|
|
|
|
|
callback.onFilterChanged(filter); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|