From a1c1bcaf88d7216abc7e2af3c1a040bcea3f34be Mon Sep 17 00:00:00 2001 From: HokoFly Date: Mon, 31 Aug 2020 12:01:16 +0800 Subject: [PATCH] Kotlin style implementation update --- .../main/java/com/hoko/ktblur/api/Program.kt | 4 +-- .../main/java/com/hoko/ktblur/api/Texture.kt | 11 +++--- .../hoko/ktblur/opengl/cache/TextureCache.kt | 2 +- .../opengl/framebuffer/SimpleFrameBuffer.kt | 4 +-- .../hoko/ktblur/opengl/offscreen/EglBuffer.kt | 23 +++++++------ .../opengl/offscreen/OffScreenBlurRenderer.kt | 26 +++++++------- .../ktblur/opengl/program/SimpleProgram.kt | 7 +--- .../hoko/ktblur/opengl/texture/Textures.kt | 34 ++++++------------- .../ktblur/processor/OpenGLBlurProcessor.kt | 4 +-- 9 files changed, 49 insertions(+), 66 deletions(-) diff --git a/library/src/main/java/com/hoko/ktblur/api/Program.kt b/library/src/main/java/com/hoko/ktblur/api/Program.kt index bf0f7d2..6689495 100644 --- a/library/src/main/java/com/hoko/ktblur/api/Program.kt +++ b/library/src/main/java/com/hoko/ktblur/api/Program.kt @@ -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 } \ No newline at end of file diff --git a/library/src/main/java/com/hoko/ktblur/api/Texture.kt b/library/src/main/java/com/hoko/ktblur/api/Texture.kt index 1a1a9e6..f8caa63 100644 --- a/library/src/main/java/com/hoko/ktblur/api/Texture.kt +++ b/library/src/main/java/com/hoko/ktblur/api/Texture.kt @@ -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() } \ No newline at end of file diff --git a/library/src/main/java/com/hoko/ktblur/opengl/cache/TextureCache.kt b/library/src/main/java/com/hoko/ktblur/opengl/cache/TextureCache.kt index 26b4f2e..b5c819b 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/cache/TextureCache.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/cache/TextureCache.kt @@ -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) { diff --git a/library/src/main/java/com/hoko/ktblur/opengl/framebuffer/SimpleFrameBuffer.kt b/library/src/main/java/com/hoko/ktblur/opengl/framebuffer/SimpleFrameBuffer.kt index 17c9625..9a64ee5 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/framebuffer/SimpleFrameBuffer.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/framebuffer/SimpleFrameBuffer.kt @@ -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) } diff --git a/library/src/main/java/com/hoko/ktblur/opengl/offscreen/EglBuffer.kt b/library/src/main/java/com/hoko/ktblur/opengl/offscreen/EglBuffer.kt index d90b5b4..e425da6 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/offscreen/EglBuffer.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/offscreen/EglBuffer.kt @@ -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 = arrayOfNulls(1) @@ -103,18 +115,7 @@ class EglBuffer { } } - - fun setBlurRadius(radius: Int) { - getRenderer().radius = radius - } - - fun setBlurMode(mode: Mode) { - getRenderer().mode = mode - } - fun free() { getRenderer().free() } - - } \ No newline at end of file diff --git a/library/src/main/java/com/hoko/ktblur/opengl/offscreen/OffScreenBlurRenderer.kt b/library/src/main/java/com/hoko/ktblur/opengl/offscreen/OffScreenBlurRenderer.kt index b48ff94..0fbd8ef 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/offscreen/OffScreenBlurRenderer.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/offscreen/OffScreenBlurRenderer.kt @@ -135,9 +135,9 @@ class OffScreenBlurRenderer : Render { 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, @@ -148,7 +148,7 @@ class OffScreenBlurRenderer : Render { 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) @@ -156,17 +156,17 @@ class OffScreenBlurRenderer : Render { 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) @@ -208,16 +208,16 @@ class OffScreenBlurRenderer : Render { 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) diff --git a/library/src/main/java/com/hoko/ktblur/opengl/program/SimpleProgram.kt b/library/src/main/java/com/hoko/ktblur/opengl/program/SimpleProgram.kt index aaf43f1..87790d0 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/program/SimpleProgram.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/program/SimpleProgram.kt @@ -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) @@ -38,7 +38,6 @@ class SimpleProgram(vertexShaderCode: String, fragmentShaderCode: String) : Prog GLES20.glDeleteProgram(id) id = 0 } - // } } finally { GLES20.glDetachShader(id, vertexShader) @@ -74,8 +73,4 @@ class SimpleProgram(vertexShaderCode: String, fragmentShaderCode: String) : Prog GLES20.glDeleteProgram(id) } } - - override fun id(): Int { - return id - } } \ No newline at end of file diff --git a/library/src/main/java/com/hoko/ktblur/opengl/texture/Textures.kt b/library/src/main/java/com/hoko/ktblur/opengl/texture/Textures.kt index dd9f60e..708e0de 100644 --- a/library/src/main/java/com/hoko/ktblur/opengl/texture/Textures.kt +++ b/library/src/main/java/com/hoko/ktblur/opengl/texture/Textures.kt @@ -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()) @@ -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 - } } @@ -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)) { @@ -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, diff --git a/library/src/main/java/com/hoko/ktblur/processor/OpenGLBlurProcessor.kt b/library/src/main/java/com/hoko/ktblur/processor/OpenGLBlurProcessor.kt index 77cb28a..4988d13 100644 --- a/library/src/main/java/com/hoko/ktblur/processor/OpenGLBlurProcessor.kt +++ b/library/src/main/java/com/hoko/ktblur/processor/OpenGLBlurProcessor.kt @@ -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) }