Remove scale from EglViewport, was used in oval shader

v2
Mattia Iavarone 6 years ago
parent d5e1bb16f2
commit 147f175ba8
  1. 44
      cameraview/src/main/gles/com/otaliastudios/cameraview/EglViewport.java
  2. 9
      cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java
  3. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  4. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  5. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java
  6. 7
      cameraview/src/main/views/com/otaliastudios/cameraview/GlCameraPreview.java

@ -18,15 +18,12 @@ class EglViewport extends EglElement {
private static final String SIMPLE_VERTEX_SHADER = private static final String SIMPLE_VERTEX_SHADER =
"uniform mat4 uMVPMatrix;\n" + "uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" + "uniform mat4 uTexMatrix;\n" +
"uniform vec2 uTextureScale;\n" +
"attribute vec4 aPosition;\n" + "attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" + "attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" + "varying vec2 vTextureCoord;\n" +
"varying vec2 vTextureScale;\n" +
"void main() {\n" + "void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" + " gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" + " vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
" vTextureScale = uTextureScale.xy;\n" +
"}\n"; "}\n";
// Simple fragment shader for use with external 2D textures // Simple fragment shader for use with external 2D textures
@ -39,33 +36,6 @@ class EglViewport extends EglElement {
" gl_FragColor = texture2D(sTexture, vTextureCoord);\n" + " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
"}\n"; "}\n";
/**
* EXPERIMENT:
* An oval fragment shader. We draw all the pixels outside the circle of center (0.5, 0.5)
* and radius 0.5 as transparent. Works well, but we have:
*
* 1. black in the preview: This can be solved with a custom GlSurfaceView that overrides
* dispatchDraw, and, before calling super, clips the canvas with an oval path (canvas.clipPath).
* 2. black in picture snapshot: This can be solved by using PNG, but it is suepr slow.
* 3. black in videos: No way to solve this AFAIK.
*/
private static final String CIRCLE_FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec2 vTextureScale;\n" +
"uniform samplerExternalOES sTexture;\n" +
"vec2 axis = vec2(0.5, 0.5);\n" +
"vec2 center = vec2(0.5, 0.5);\n" +
"vec4 none = vec4(0.0, 0.0, 0.0, 0.0);\n" +
"void main() {\n" +
" if (pow((vTextureCoord.x - center.x) / (axis.x * vTextureScale.x), 2.0) + pow((vTextureCoord.y - center.y) / (axis.y * vTextureScale.y), 2.0) > 1.0) {\n" +
" gl_FragColor = none;\n" +
" } else {\n" +
" gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
" }\n" +
"}\n";
// Stuff from Drawable2d.FULL_RECTANGLE // Stuff from Drawable2d.FULL_RECTANGLE
// A full square, extending from -1 to +1 in both dimensions. // A full square, extending from -1 to +1 in both dimensions.
// When the model/view/projection matrix is identity, this will exactly cover the viewport. // When the model/view/projection matrix is identity, this will exactly cover the viewport.
@ -98,7 +68,6 @@ class EglViewport extends EglElement {
private int muTexMatrixLocation; private int muTexMatrixLocation;
private int maPositionLocation; private int maPositionLocation;
private int maTextureCoordLocation; private int maTextureCoordLocation;
private int muTextureScaleLocation;
// private int muKernelLoc; // Used for filtering // private int muKernelLoc; // Used for filtering
// private int muTexOffsetLoc; // Used for filtering // private int muTexOffsetLoc; // Used for filtering
@ -111,8 +80,6 @@ class EglViewport extends EglElement {
checkLocation(maPositionLocation, "aPosition"); checkLocation(maPositionLocation, "aPosition");
maTextureCoordLocation = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord"); maTextureCoordLocation = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord");
checkLocation(maTextureCoordLocation, "aTextureCoord"); checkLocation(maTextureCoordLocation, "aTextureCoord");
muTextureScaleLocation = GLES20.glGetUniformLocation(mProgramHandle, "uTextureScale");
checkLocation(muTextureScaleLocation, "uTextureScale");
muMVPMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix"); muMVPMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix");
checkLocation(muMVPMatrixLocation, "uMVPMatrix"); checkLocation(muMVPMatrixLocation, "uMVPMatrix");
muTexMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix"); muTexMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix");
@ -149,10 +116,10 @@ class EglViewport extends EglElement {
return texId; return texId;
} }
void drawFrame(int textureId, float[] textureMatrix, float[] textureScale) { void drawFrame(int textureId, float[] textureMatrix) {
drawFrame(textureId, textureMatrix, drawFrame(textureId, textureMatrix,
mVertexCoordinatesArray, mVertexCoordinatesArray,
mTextureCoordinatesArray, textureScale); mTextureCoordinatesArray);
} }
/** /**
@ -170,8 +137,7 @@ class EglViewport extends EglElement {
*/ */
private void drawFrame(int textureId, float[] textureMatrix, private void drawFrame(int textureId, float[] textureMatrix,
FloatBuffer vertexBuffer, FloatBuffer vertexBuffer,
FloatBuffer texBuffer, FloatBuffer texBuffer) {
float[] textureScale) {
check("draw start"); check("draw start");
// Select the program. // Select the program.
@ -190,10 +156,6 @@ class EglViewport extends EglElement {
GLES20.glUniformMatrix4fv(muTexMatrixLocation, 1, false, textureMatrix, 0); GLES20.glUniformMatrix4fv(muTexMatrixLocation, 1, false, textureMatrix, 0);
check("glUniformMatrix4fv"); check("glUniformMatrix4fv");
// Pass scale values.
GLES20.glUniform2fv(muTextureScaleLocation, 1, textureScale, 0);
check("glUniform2f");
// Enable the "aPosition" vertex attribute. // Enable the "aPosition" vertex attribute.
// Connect vertexBuffer to "aPosition". // Connect vertexBuffer to "aPosition".
GLES20.glEnableVertexAttribArray(maPositionLocation); GLES20.glEnableVertexAttribArray(maPositionLocation);

@ -85,8 +85,6 @@ class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.Config>
// Nothing to do here. Waiting for the first frame. // Nothing to do here. Waiting for the first frame.
} }
private float[] mScaleXY = new float[2];
@EncoderThread @EncoderThread
@Override @Override
void notify(@NonNull String event, @Nullable Object data) { void notify(@NonNull String event, @Nullable Object data) {
@ -126,10 +124,9 @@ class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.Config>
Matrix.translateM(transform, 0, -0.5F, -0.5F, 0); Matrix.translateM(transform, 0, -0.5F, -0.5F, 0);
drain(false); drain(false);
boolean flip = mConfig.scaleFlipped;// mConfig.rotation % 180 != 0; // Future note: passing scale values to the viewport? They are scaleX and scaleY,
mScaleXY[0] = flip ? scaleY : scaleX; // but flipped based on the mConfig.scaleFlipped boolean.
mScaleXY[1] = flip ? scaleX : scaleY; mViewport.drawFrame(mConfig.textureId, transform);
mViewport.drawFrame(mConfig.textureId, transform, mScaleXY);
mWindow.setPresentationTime(timestamp); mWindow.setPresentationTime(timestamp);
mWindow.swapBuffers(); mWindow.swapBuffers();
} }

@ -58,6 +58,7 @@ public class CameraOptions {
} }
// Flash // Flash
supportedFlash.add(Flash.OFF);
strings = params.getSupportedFlashModes(); strings = params.getSupportedFlashModes();
if (strings != null) { if (strings != null) {
for (String string : strings) { for (String string : strings) {
@ -67,6 +68,7 @@ public class CameraOptions {
} }
// Hdr // Hdr
supportedHdr.add(Hdr.OFF);
strings = params.getSupportedSceneModes(); strings = params.getSupportedSceneModes();
if (strings != null) { if (strings != null) {
for (String string : strings) { for (String string : strings) {

@ -239,13 +239,16 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
protected CameraPreview instantiatePreview(Context context, ViewGroup container) { protected CameraPreview instantiatePreview(Context context, ViewGroup container) {
LOG.w("preview:", "isHardwareAccelerated:", isHardwareAccelerated()); LOG.w("preview:", "isHardwareAccelerated:", isHardwareAccelerated());
switch (mPreview) { switch (mPreview) {
case SURFACE: return new SurfaceCameraPreview(context, container, null); case SURFACE:
return new SurfaceCameraPreview(context, container, null);
case TEXTURE: { case TEXTURE: {
if (isHardwareAccelerated()) { // TextureView is not supported without hardware acceleration. if (isHardwareAccelerated()) {
// TextureView is not supported without hardware acceleration.
return new TextureCameraPreview(context, container, null); return new TextureCameraPreview(context, container, null);
} }
} }
case GL_SURFACE: default: { case GL_SURFACE: default: {
mPreview = Preview.GL_SURFACE;
return new GlCameraPreview(context, container, null); return new GlCameraPreview(context, container, null);
} }
} }

@ -122,8 +122,9 @@ class SnapshotPictureRecorder extends PictureRecorder {
Matrix.rotateM(mTransform, 0, rotation, 0, 0, 1); Matrix.rotateM(mTransform, 0, rotation, 0, 0, 1);
Matrix.translateM(mTransform, 0, -0.5F, -0.5F, 0); Matrix.translateM(mTransform, 0, -0.5F, -0.5F, 0);
float[] scaleXY = new float[] { realScaleX, realScaleY }; // Future note: passing scale values to the viewport?
viewport.drawFrame(mTextureId, mTransform, scaleXY); // They are simply realScaleX and realScaleY.
viewport.drawFrame(mTextureId, mTransform);
// don't - surface.swapBuffers(); // don't - surface.swapBuffers();
mResult.data = surface.saveFrameTo(Bitmap.CompressFormat.JPEG); mResult.data = surface.saveFrameTo(Bitmap.CompressFormat.JPEG);
mResult.format = PictureResult.FORMAT_JPEG; mResult.format = PictureResult.FORMAT_JPEG;

@ -61,7 +61,6 @@ class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture> imple
private Set<RendererFrameCallback> mRendererFrameCallbacks = Collections.synchronizedSet(new HashSet<RendererFrameCallback>()); private Set<RendererFrameCallback> mRendererFrameCallbacks = Collections.synchronizedSet(new HashSet<RendererFrameCallback>());
/* for tests */ float mScaleX = 1F; /* for tests */ float mScaleX = 1F;
/* for tests */ float mScaleY = 1F; /* for tests */ float mScaleY = 1F;
private float[] mScaleXY = new float[] { mScaleX, mScaleY };
private View mRootView; private View mRootView;
@ -203,9 +202,9 @@ class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture> imple
Matrix.translateM(mTransformMatrix, 0, translX, translY, 0); Matrix.translateM(mTransformMatrix, 0, translX, translY, 0);
Matrix.scaleM(mTransformMatrix, 0, mScaleX, mScaleY, 1); Matrix.scaleM(mTransformMatrix, 0, mScaleX, mScaleY, 1);
} }
mScaleXY[0] = mInputFlipped ? mScaleY : mScaleX; // Future note: passing scale to the viewport?
mScaleXY[1] = mInputFlipped ? mScaleX : mScaleY; // They are scaleX an scaleY, but flipped based on mInputFlipped.
mOutputViewport.drawFrame(mOutputTextureId, mTransformMatrix, mScaleXY); mOutputViewport.drawFrame(mOutputTextureId, mTransformMatrix);
for (RendererFrameCallback callback : mRendererFrameCallbacks) { for (RendererFrameCallback callback : mRendererFrameCallbacks) {
callback.onRendererFrame(mInputSurfaceTexture, mScaleX, mScaleY); callback.onRendererFrame(mInputSurfaceTexture, mScaleX, mScaleY);
} }

Loading…
Cancel
Save