-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul camera surface and add some shader preset attrs for testing
- Loading branch information
Showing
16 changed files
with
700 additions
and
21 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
101 changes: 101 additions & 0 deletions
101
camerakit/src/main/java/com/camerakit/surface/CameraSurfaceRenderer.kt
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,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
46
camerakit/src/main/java/com/camerakit/surface/CameraSurfaceView.kt
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.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) | ||
} | ||
|
||
} |
Oops, something went wrong.