Remove scale from EglViewport, was used in oval shader

pull/360/head
Mattia Iavarone 6 years ago
parent e9df75b7ae
commit 2dba0fa308
  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 =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uTexMatrix;\n" +
"uniform vec2 uTextureScale;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec2 vTextureScale;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
" vTextureScale = uTextureScale.xy;\n" +
"}\n";
// Simple fragment shader for use with external 2D textures
@ -39,33 +36,6 @@ class EglViewport extends EglElement {
" gl_FragColor = texture2D(sTexture, vTextureCoord);\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
// 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.
@ -98,7 +68,6 @@ class EglViewport extends EglElement {
private int muTexMatrixLocation;
private int maPositionLocation;
private int maTextureCoordLocation;
private int muTextureScaleLocation;
// private int muKernelLoc; // Used for filtering
// private int muTexOffsetLoc; // Used for filtering
@ -111,8 +80,6 @@ class EglViewport extends EglElement {
checkLocation(maPositionLocation, "aPosition");
maTextureCoordLocation = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord");
checkLocation(maTextureCoordLocation, "aTextureCoord");
muTextureScaleLocation = GLES20.glGetUniformLocation(mProgramHandle, "uTextureScale");
checkLocation(muTextureScaleLocation, "uTextureScale");
muMVPMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix");
checkLocation(muMVPMatrixLocation, "uMVPMatrix");
muTexMatrixLocation = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix");
@ -149,10 +116,10 @@ class EglViewport extends EglElement {
return texId;
}
void drawFrame(int textureId, float[] textureMatrix, float[] textureScale) {
void drawFrame(int textureId, float[] textureMatrix) {
drawFrame(textureId, textureMatrix,
mVertexCoordinatesArray,
mTextureCoordinatesArray, textureScale);
mTextureCoordinatesArray);
}
/**
@ -170,8 +137,7 @@ class EglViewport extends EglElement {
*/
private void drawFrame(int textureId, float[] textureMatrix,
FloatBuffer vertexBuffer,
FloatBuffer texBuffer,
float[] textureScale) {
FloatBuffer texBuffer) {
check("draw start");
// Select the program.
@ -190,10 +156,6 @@ class EglViewport extends EglElement {
GLES20.glUniformMatrix4fv(muTexMatrixLocation, 1, false, textureMatrix, 0);
check("glUniformMatrix4fv");
// Pass scale values.
GLES20.glUniform2fv(muTextureScaleLocation, 1, textureScale, 0);
check("glUniform2f");
// Enable the "aPosition" vertex attribute.
// Connect vertexBuffer to "aPosition".
GLES20.glEnableVertexAttribArray(maPositionLocation);

@ -85,8 +85,6 @@ class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.Config>
// Nothing to do here. Waiting for the first frame.
}
private float[] mScaleXY = new float[2];
@EncoderThread
@Override
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);
drain(false);
boolean flip = mConfig.scaleFlipped;// mConfig.rotation % 180 != 0;
mScaleXY[0] = flip ? scaleY : scaleX;
mScaleXY[1] = flip ? scaleX : scaleY;
mViewport.drawFrame(mConfig.textureId, transform, mScaleXY);
// Future note: passing scale values to the viewport? They are scaleX and scaleY,
// but flipped based on the mConfig.scaleFlipped boolean.
mViewport.drawFrame(mConfig.textureId, transform);
mWindow.setPresentationTime(timestamp);
mWindow.swapBuffers();
}

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

@ -239,13 +239,16 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
protected CameraPreview instantiatePreview(Context context, ViewGroup container) {
LOG.w("preview:", "isHardwareAccelerated:", isHardwareAccelerated());
switch (mPreview) {
case SURFACE: return new SurfaceCameraPreview(context, container, null);
case SURFACE:
return new SurfaceCameraPreview(context, container, null);
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);
}
}
case GL_SURFACE: default: {
mPreview = Preview.GL_SURFACE;
return new GlCameraPreview(context, container, null);
}
}

@ -122,8 +122,9 @@ class SnapshotPictureRecorder extends PictureRecorder {
Matrix.rotateM(mTransform, 0, rotation, 0, 0, 1);
Matrix.translateM(mTransform, 0, -0.5F, -0.5F, 0);
float[] scaleXY = new float[] { realScaleX, realScaleY };
viewport.drawFrame(mTextureId, mTransform, scaleXY);
// Future note: passing scale values to the viewport?
// They are simply realScaleX and realScaleY.
viewport.drawFrame(mTextureId, mTransform);
// don't - surface.swapBuffers();
mResult.data = surface.saveFrameTo(Bitmap.CompressFormat.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>());
/* for tests */ float mScaleX = 1F;
/* for tests */ float mScaleY = 1F;
private float[] mScaleXY = new float[] { mScaleX, mScaleY };
private View mRootView;
@ -203,9 +202,9 @@ class GlCameraPreview extends CameraPreview<GLSurfaceView, SurfaceTexture> imple
Matrix.translateM(mTransformMatrix, 0, translX, translY, 0);
Matrix.scaleM(mTransformMatrix, 0, mScaleX, mScaleY, 1);
}
mScaleXY[0] = mInputFlipped ? mScaleY : mScaleX;
mScaleXY[1] = mInputFlipped ? mScaleX : mScaleY;
mOutputViewport.drawFrame(mOutputTextureId, mTransformMatrix, mScaleXY);
// Future note: passing scale to the viewport?
// They are scaleX an scaleY, but flipped based on mInputFlipped.
mOutputViewport.drawFrame(mOutputTextureId, mTransformMatrix);
for (RendererFrameCallback callback : mRendererFrameCallbacks) {
callback.onRendererFrame(mInputSurfaceTexture, mScaleX, mScaleY);
}

Loading…
Cancel
Save