Skip to content

Commit

Permalink
Overhaul camera surface and add some shader preset attrs for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dwillmc committed Jul 3, 2018
1 parent fcaa787 commit b4033e0
Show file tree
Hide file tree
Showing 16 changed files with 700 additions and 21 deletions.
1 change: 1 addition & 0 deletions camerakit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {

dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5'
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.camerakit:jpegkit:0.2.0-alpha1'
testImplementation 'junit:junit:4.12'
Expand Down
83 changes: 62 additions & 21 deletions camerakit/src/main/java/com/camerakit/CameraKitView.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
import android.util.AttributeSet;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.camerakit.surface.CameraSurfaceView;
import com.jpegkit.Jpeg;

import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -97,7 +100,8 @@ public class CameraKitView extends GestureLayout {
@Retention(RetentionPolicy.SOURCE)
@IntDef(flag = true,
value = {PERMISSION_CAMERA, PERMISSION_MICROPHONE, PERMISSION_STORAGE, PERMISSION_LOCATION})
@interface Permission {}
@interface Permission {
}

/**
*
Expand Down Expand Up @@ -240,11 +244,11 @@ public interface FrameCallback {
private int mPermissions;
private float mImageMegaPixels;
private int mImageJpegQuality;
private int mShader;
private GestureListener mGestureListener;
private CameraListener mCameraListener;
private PreviewListener mPreviewListener;
private ErrorListener mErrorListener;

private PermissionsListener mPermissionsListener;

private CameraPreview mCameraPreview;
Expand Down Expand Up @@ -276,6 +280,7 @@ private void obtainAttributes(Context context, AttributeSet attrs) {
mPermissions = a.getInteger(R.styleable.CameraKitView_camera_permissions, PERMISSION_CAMERA);
mImageMegaPixels = a.getFloat(R.styleable.CameraKitView_camera_imageMegaPixels, 2f);
mImageJpegQuality = a.getInteger(R.styleable.CameraKitView_camera_imageJpegQuality, 100);
mShader = a.getInteger(R.styleable.CameraKitView_camera_shader, 0);

a.recycle();
}
Expand Down Expand Up @@ -394,7 +399,7 @@ public void onResume() {
if (Build.VERSION.SDK_INT < 21) {
mCameraPreview = new Camera1(getContext(), mFacing);
} else {
mCameraPreview = new Camera2(getContext(), mFacing);
mCameraPreview = new Camera1(getContext(), mFacing);
}

addView(mCameraPreview);
Expand All @@ -414,6 +419,16 @@ public void onPause() {
}
}


public void setShader(int shader) {
mShader = shader;

if (mCameraPreview != null) {
mCameraPreview.setShader(shader);
}
}


/**
* @param callback
*/
Expand Down Expand Up @@ -595,7 +610,6 @@ public float getAspectRatio() {
* @param facing one of {@link CameraKit.Facing}'s constants.
* @see CameraKit#FACING_BACK
* @see CameraKit#FACING_FRONT
*
*/
public void setFacing(@CameraKit.Facing int facing) {
mFacing = facing;
Expand Down Expand Up @@ -830,31 +844,27 @@ public void onPinch(CameraKitView view, float ds, float dsx, float dsy) {
}

/**
*
* @param cameraListener
*/
public void setCameraListener(CameraListener cameraListener) {
mCameraListener = cameraListener;
}

/**
*
* @return
*/
public CameraListener getCameraListener() {
return mCameraListener;
}

/**
*
* @param previewListener
*/
public void setPreviewListener(PreviewListener previewListener) {
mPreviewListener = previewListener;
}

/**
*
* @return
*/
public PreviewListener getPreviewListener() {
Expand Down Expand Up @@ -1007,6 +1017,9 @@ public void stop() {
mApi.closeCamera();
}

public void setShader(int shader) {
}

public void reconfigure() {
}

Expand Down Expand Up @@ -1184,7 +1197,7 @@ protected void background(Runnable runnable) {
*/
private class Camera1 extends CameraPreview implements SurfaceHolder.Callback {

private final SurfaceView mSurfaceView;
private final CameraSurfaceView mSurfaceView;
private final SurfaceHolder mSurfaceHolder;

private Camera mCamera;
Expand All @@ -1197,11 +1210,11 @@ public Camera1(Context context) {
protected Camera1(Context context, @CameraKit.Facing int facing) {
super(context, facing);

mSurfaceView = new SurfaceView(context);
mSurfaceView = new CameraSurfaceView(context);
mSurfaceView.setShader(mShader);

mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

// CameraPreview:
Expand All @@ -1211,6 +1224,19 @@ protected Camera1(Context context, @CameraKit.Facing int facing) {
protected void onAttachedToWindow() {
super.onAttachedToWindow();
addView(mSurfaceView);
mSurfaceView.onResume();
}

@Override
public void stop() {
super.stop();
mSurfaceView.onPause();
}

@Override
public void setShader(int shader) {
super.setShader(shader);
mSurfaceView.setShader(shader);
}

// ViewGroup:
Expand Down Expand Up @@ -1337,7 +1363,8 @@ public void run() {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight());

if (parameters.getSupportedFlashModes().contains(FOCUS_MODE_CONTINUOUS_PICTURE)) {
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
if (supportedFlashModes != null && supportedFlashModes.contains(FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);
}

Expand All @@ -1359,16 +1386,27 @@ public void run() {
mCamera.setParameters(parameters);
}

try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
dispatchEvent(EVENT_PREVIEW_ERROR);
return;
}
mSurfaceView.awaitSurface(new CameraSurfaceView.Callback() {
@Override
public void run(@NotNull SurfaceTexture surfaceTexture) {
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
mSurfaceView.requestRender();
}
});

mCamera.startPreview();
try {
mCamera.setPreviewTexture(surfaceTexture);
mCamera.startPreview();

dispatchEvent(EVENT_PREVIEW_STARTED);
dispatchEvent(EVENT_PREVIEW_STARTED);
} catch (IOException e) {
Log.e("CameraKiln", e.toString());
dispatchEvent(EVENT_PREVIEW_ERROR);
}
}
});
} else {
dispatchEvent(EVENT_SURFACE_ERROR);
}
Expand Down Expand Up @@ -1403,7 +1441,9 @@ public void run() {
} else {
previewRotation = (mCameraInfo.orientation - displayRotation + 360) % 360;
}

mCamera.setDisplayOrientation(previewRotation);
mSurfaceView.setOrientation(displayRotation, mCameraInfo.orientation, mFacing);
}
}
});
Expand Down Expand Up @@ -1589,6 +1629,7 @@ public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}


// CameraApi:

@Override
Expand Down
101 changes: 101 additions & 0 deletions camerakit/src/main/java/com/camerakit/surface/CameraSurfaceRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.camerakit.surface

import android.content.Context
import android.graphics.SurfaceTexture
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import com.camerakit.CameraKit
import kotlinx.coroutines.experimental.delay
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

internal class CameraSurfaceRenderer(private val context: Context) : GLSurfaceView.Renderer {

private var textureId: Int = -1
private var surfaceTexture: SurfaceTexture? = null

private var textureProgram: TextureProgram? = null

var shader: Int = 0
set(value) {
field = value
textureProgram?.let {
it.release()

var type = TextureProgram.Type.TEXTURE_EXT
if (value == 1) {
type = TextureProgram.Type.TEXTURE_EDGES
} else if (value == 2) {
type = TextureProgram.Type.TEXTURE_TWIRL
} else if (value == 3) {
type = TextureProgram.Type.TEXTURE_WARP
}

textureProgram = TextureProgram(context, type)
}
}

private val drawModel = DrawModel(
vertices = DrawModel.FULL_RECT_VERTICES,
texCoords = DrawModel.FULL_RECT_TEX_COORDS,
coordsPerVertex = DrawModel.FULL_RECT_COORDS_PER_VERTEX
)

private val drawModelProjector = DrawModelProjector(drawModel)

suspend fun getSurfaceTexture(): SurfaceTexture {
while (true) {
if (surfaceTexture != null) {
return surfaceTexture!!
}

delay(50)
}
}

override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {
var type = TextureProgram.Type.TEXTURE_EXT
if (shader == 1) {
type = TextureProgram.Type.TEXTURE_EDGES
} else if (shader == 2) {
type = TextureProgram.Type.TEXTURE_TWIRL
} else if (shader == 3) {
type = TextureProgram.Type.TEXTURE_WARP
}

val textureProgram = TextureProgram(context, type)
textureId = textureProgram.genTexture()
surfaceTexture = SurfaceTexture(textureId)
drawModelProjector.textureId = textureId

this.textureProgram = textureProgram
}

override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
GLES20.glViewport(0, 0, width, height)
}

override fun onDrawFrame(gl: GL10) {
surfaceTexture?.run {
updateTexImage()
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
gl.glClear(GLES20.GL_COLOR_BUFFER_BIT)

textureProgram?.let {
drawModelProjector.draw(it)
}
}
}

fun setOrientation(displayRotation: Int, sensorOrientation: Int, facing: Int) {
if (facing == CameraKit.FACING_FRONT) {
drawModelProjector.setScale(-1f, -1f)
drawModelProjector.setRotation((sensorOrientation + displayRotation).toFloat())
} else {
drawModelProjector.setScale(-1f, 1f)
drawModelProjector.setRotation((sensorOrientation + displayRotation).toFloat())
}
}


}
46 changes: 46 additions & 0 deletions camerakit/src/main/java/com/camerakit/surface/CameraSurfaceView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.camerakit.surface

import android.content.Context
import android.graphics.SurfaceTexture
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch

class CameraSurfaceView : GLSurfaceView {

internal val renderer: CameraSurfaceRenderer

constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

init {
setEGLContextClientVersion(2)

renderer = CameraSurfaceRenderer(context)
setRenderer(renderer)

renderMode = RENDERMODE_WHEN_DIRTY
keepScreenOn = true
}

fun setShader(shader: Int) {
renderer.shader = shader
}

fun setOrientation(displayRotation: Int, sensorOrientation: Int, facing: Int) {
renderer.setOrientation(displayRotation, sensorOrientation, facing)
}

fun awaitSurface(callback: Callback) {
launch(UI) {
val surfaceTexture = renderer.getSurfaceTexture()
callback.run(surfaceTexture)
}
}

interface Callback {
fun run(surfaceTexture: SurfaceTexture)
}

}
Loading

0 comments on commit b4033e0

Please sign in to comment.