Skip to content

Commit

Permalink
Kotlin style implementation update
Browse files Browse the repository at this point in the history
  • Loading branch information
HokoFly committed Aug 31, 2020
1 parent 1bcddd5 commit a1c1bca
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 66 deletions.
4 changes: 2 additions & 2 deletions library/src/main/java/com/hoko/ktblur/api/Program.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hoko.ktblur.api

interface Program {
var id: Int

fun create(vertexShaderCode: String, fragmentShaderCode: String)

fun delete()

fun id(): Int
}
11 changes: 5 additions & 6 deletions library/src/main/java/com/hoko/ktblur/api/Texture.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.hoko.ktblur.api

interface Texture {
fun create()

fun delete()
val width: Int

fun id(): Int
val height: Int

fun width(): Int
val id: Int

fun height(): Int
fun create()

fun delete()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object TextureCache {
}

override fun checkHit(key: Size, value: Texture): Boolean {
return key.width == value.width() && key.height == value.height()
return key.width == value.width && key.height == value.height
}

override fun entryDeleted(removed: Texture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class SimpleFrameBuffer(private var frameBufferId: Int = 0) : FrameBuffer{

override fun bindTexture(texture: Texture) {
this.texture = texture.also {
check(it.id() != 0)
check(it.id != 0)
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId)
GLES20.glFramebufferTexture2D(
GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
GLES20.GL_TEXTURE_2D, it.id(), 0
GLES20.GL_TEXTURE_2D, it.id, 0
)
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)
}
Expand Down
23 changes: 12 additions & 11 deletions library/src/main/java/com/hoko/ktblur/opengl/offscreen/EglBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class EglBuffer {
private const val EGL_OPENGL_ES2_BIT: Int = 4
}

var blurMode: Mode
get() = getRenderer().mode
set(value) {
getRenderer().mode = value
}

var blurRadius: Int
get() = getRenderer().radius
set(value) {
getRenderer().radius = value
}

private val egl: EGL10 by lazy { EGLContext.getEGL() as EGL10 }
private val eglDisplay: EGLDisplay by lazy { egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY) }
private val eglConfigs: Array<EGLConfig?> = arrayOfNulls(1)
Expand Down Expand Up @@ -103,18 +115,7 @@ class EglBuffer {
}
}


fun setBlurRadius(radius: Int) {
getRenderer().radius = radius
}

fun setBlurMode(mode: Mode) {
getRenderer().mode = mode
}

fun free() {
getRenderer().free()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ class OffScreenBlurRenderer : Render<Bitmap> {
try {
val p = mProgram

GLES20.glUseProgram(p.id())
GLES20.glUseProgram(p.id)

val positionId = GLES20.glGetAttribLocation(p.id(), "aPosition")
val positionId = GLES20.glGetAttribLocation(p.id, "aPosition")
GLES20.glEnableVertexAttribArray(positionId)
GLES20.glVertexAttribPointer(
positionId,
Expand All @@ -148,25 +148,25 @@ class OffScreenBlurRenderer : Render<Bitmap> {
vertexBuffer
)

val texCoordId = GLES20.glGetAttribLocation(p.id(), "aTexCoord")
val texCoordId = GLES20.glGetAttribLocation(p.id, "aTexCoord")
GLES20.glEnableVertexAttribArray(texCoordId)
GLES20.glVertexAttribPointer(texCoordId, 2, GLES20.GL_FLOAT, false, 0, texCoordBuffer)

if (isHorizontal) {
blurContext.blurFrameBuffer.bindSelf()
}

val textureUniformId = GLES20.glGetUniformLocation(p.id(), "uTexture")
val textureUniformId = GLES20.glGetUniformLocation(p.id, "uTexture")
GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
GLES20.glBindTexture(
GLES20.GL_TEXTURE_2D,
if (isHorizontal) blurContext.inputTexture.id() else blurContext.horizontalTexture.id()
if (isHorizontal) blurContext.inputTexture.id else blurContext.horizontalTexture.id
)
GLES20.glUniform1i(textureUniformId, 0)

val radiusId = GLES20.glGetUniformLocation(p.id(), "uRadius")
val widthOffsetId = GLES20.glGetUniformLocation(p.id(), "uWidthOffset")
val heightOffsetId = GLES20.glGetUniformLocation(p.id(), "uHeightOffset")
val radiusId = GLES20.glGetUniformLocation(p.id, "uRadius")
val widthOffsetId = GLES20.glGetUniformLocation(p.id, "uWidthOffset")
val heightOffsetId = GLES20.glGetUniformLocation(p.id, "uHeightOffset")
GLES20.glUniform1i(radiusId, radius)
GLES20.glUniform1f(widthOffsetId, if (isHorizontal) 0f else 1f / blurContext.bitmap.width)
GLES20.glUniform1f(heightOffsetId, if (isHorizontal) 1f / blurContext.bitmap.height else 0f)
Expand Down Expand Up @@ -208,16 +208,16 @@ class OffScreenBlurRenderer : Render<Bitmap> {
deletePrograms()
}

private class BlurContext(internal val bitmap: Bitmap) {
internal val inputTexture: Texture = TextureFactory.create(bitmap)
internal val horizontalTexture: Texture = TextureFactory.create(bitmap.width, bitmap.height)
internal val blurFrameBuffer: FrameBuffer = FrameBufferCache.getFrameBuffer()
private class BlurContext(val bitmap: Bitmap) {
val inputTexture: Texture = TextureFactory.create(bitmap)
val horizontalTexture: Texture = TextureFactory.create(bitmap.width, bitmap.height)
val blurFrameBuffer: FrameBuffer = FrameBufferCache.getFrameBuffer()

init {
blurFrameBuffer.bindTexture(horizontalTexture)
}

internal fun finish() {
fun finish() {
this.inputTexture.delete()
this.horizontalTexture.delete()
FrameBufferCache.recycleFrameBuffer(blurFrameBuffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SimpleProgram(vertexShaderCode: String, fragmentShaderCode: String) : Prog
private val TAG = SimpleProgram::class.java.simpleName
}

private var id: Int = 0
override var id: Int = 0

init {
create(vertexShaderCode, fragmentShaderCode)
Expand Down Expand Up @@ -38,7 +38,6 @@ class SimpleProgram(vertexShaderCode: String, fragmentShaderCode: String) : Prog
GLES20.glDeleteProgram(id)
id = 0
}
//
}
} finally {
GLES20.glDetachShader(id, vertexShader)
Expand Down Expand Up @@ -74,8 +73,4 @@ class SimpleProgram(vertexShaderCode: String, fragmentShaderCode: String) : Prog
GLES20.glDeleteProgram(id)
}
}

override fun id(): Int {
return id
}
}
34 changes: 11 additions & 23 deletions library/src/main/java/com/hoko/ktblur/opengl/texture/Textures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import com.hoko.ktblur.api.Texture
import java.lang.ref.WeakReference
import java.nio.Buffer

sealed class AbstractTexture(val width: Int, val height: Int) : Texture {
private var textureId: Int = 0
sealed class AbstractTexture(override val width: Int, override val height: Int) : Texture {
override var id: Int = 0

override fun create() {
val textureIds = IntArray(1)

GLES20.glGenTextures(1, textureIds, 0)

textureId = textureIds[0]
id = textureIds[0]

if (textureId != 0) {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)
if (id != 0) {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id)
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST.toFloat())
Expand All @@ -32,22 +32,10 @@ sealed class AbstractTexture(val width: Int, val height: Int) : Texture {
abstract fun onTextureCreated()

override fun delete() {
if (textureId != 0) {
GLES20.glDeleteTextures(1, intArrayOf(textureId), 0)
if (id != 0) {
GLES20.glDeleteTextures(1, intArrayOf(id), 0)
}
}

override fun id(): Int {
return textureId
}

override fun width(): Int {
return width
}

override fun height(): Int {
return height
}
}


Expand All @@ -60,7 +48,7 @@ class BitmapTexture(bitmap: Bitmap) : AbstractTexture(bitmap.width, bitmap.heigh
}

override fun onTextureCreated() {
check(width() > 0 && height() > 0)
check(width > 0 && height > 0)

val bitmap = bitmapWeakRef.get()
if (bitmap != null && !(bitmap.isRecycled)) {
Expand All @@ -77,14 +65,14 @@ class SimpleTexture(width: Int, height: Int) : AbstractTexture(width, height) {
}

override fun onTextureCreated() {
check(width() > 0 && height() > 0)
check(width > 0 && height > 0)

GLES20.glTexImage2D(
GLES20.GL_TEXTURE_2D,
0,
GLES20.GL_RGBA,
width(),
height(),
width,
height,
0,
GLES20.GL_RGBA,
GLES20.GL_UNSIGNED_BYTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class OpenGLBlurProcessor(builder: HokoBlurBuild) : AbstractBlurProcessor(builde

override fun realBlur(bitmap: Bitmap, parallel: Boolean): Bitmap {
check(!bitmap.isRecycled)
eglBuffer.setBlurMode(mode)
eglBuffer.setBlurRadius(radius)
eglBuffer.blurMode = mode
eglBuffer.blurRadius = radius
return eglBuffer.getBlurBitmap(bitmap)
}

Expand Down

0 comments on commit a1c1bca

Please sign in to comment.