Skip to content

Commit cc49870

Browse files
committed
move new filter to kotlin
1 parent b25a9f6 commit cc49870

5 files changed

Lines changed: 131 additions & 123 deletions

File tree

encoder/src/main/java/com/pedro/encoder/input/gl/render/filters/object/ViewSurfaceFilterRender.java

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (C) 2026 pedroSG94.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.pedro.encoder.input.gl.render.filters.`object`
17+
18+
import android.content.Context
19+
import android.graphics.SurfaceTexture
20+
import android.hardware.display.DisplayManager
21+
import android.hardware.display.VirtualDisplay
22+
import android.opengl.GLES11Ext
23+
import android.opengl.GLES20
24+
import android.os.Build
25+
import android.os.Handler
26+
import android.os.Looper
27+
import android.view.Surface
28+
import android.view.View
29+
import androidx.annotation.LayoutRes
30+
import androidx.annotation.RequiresApi
31+
import com.pedro.encoder.R
32+
import com.pedro.encoder.utils.ViewPresentation
33+
import com.pedro.encoder.utils.gl.GlUtil
34+
35+
/**
36+
* Created by pedro on 18/07/18.
37+
*/
38+
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
39+
class ViewSurfaceFilterRender: BaseObjectFilterRender() {
40+
private var surfaceTexture: SurfaceTexture? = null
41+
private var surface: Surface? = null
42+
private var virtualDisplay: VirtualDisplay? = null
43+
private var viewPresentation: ViewPresentation? = null
44+
private var context: Context? = null
45+
private var view: View? = null
46+
private var layout: Int? = null
47+
48+
override fun initGlFilter(context: Context) {
49+
this.context = context
50+
fragment = R.raw.surface_fragment
51+
super.initGlFilter(context)
52+
GlUtil.createExternalTextures(streamObjectTextureId.size, streamObjectTextureId, 0)
53+
surfaceTexture = SurfaceTexture(streamObjectTextureId[0])
54+
surfaceTexture?.setDefaultBufferSize(width, height)
55+
surface = Surface(surfaceTexture)
56+
val displayManager: DisplayManager =
57+
context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
58+
virtualDisplay = displayManager.createVirtualDisplay(
59+
this.javaClass.name,
60+
width, height, context.resources.displayMetrics.densityDpi,
61+
surface,
62+
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
63+
)
64+
startRender()
65+
}
66+
67+
override fun drawFilter() {
68+
surfaceTexture?.updateTexImage()
69+
super.drawFilter()
70+
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, streamObjectTextureId[0])
71+
//Set alpha. 0f if no image loaded.
72+
GLES20.glUniform1f(uAlphaHandle, if (streamObjectTextureId[0] == -1) 0f else alpha)
73+
}
74+
75+
override fun release() {
76+
super.release()
77+
viewPresentation?.dismiss()
78+
viewPresentation = null
79+
virtualDisplay?.release()
80+
surfaceTexture?.release()
81+
surface?.release()
82+
}
83+
84+
/**
85+
* This texture must be renderer using an api called on main thread to avoid possible errors
86+
*/
87+
fun getSurfaceTexture(): SurfaceTexture? {
88+
return surfaceTexture
89+
}
90+
91+
fun setView(view: View?) {
92+
if (view == null) return
93+
layout = null
94+
this.view = view
95+
startRender()
96+
}
97+
98+
fun setView(@LayoutRes layoutId: Int) {
99+
if (layoutId == 0) return
100+
this.view = null
101+
this.layout = layoutId
102+
startRender()
103+
}
104+
105+
private fun startRender() {
106+
Handler(Looper.getMainLooper()).post(Runnable {
107+
if (view == null && layout == 0) return@Runnable
108+
val context = this.context ?: return@Runnable
109+
val virtualDisplay = this.virtualDisplay ?: return@Runnable
110+
viewPresentation?.dismiss()
111+
viewPresentation = ViewPresentation(context, virtualDisplay.display)
112+
view?.let { viewPresentation?.setContentView(it) }
113+
layout?.let { viewPresentation?.setContentView(it) }
114+
viewPresentation?.show()
115+
})
116+
}
117+
}

encoder/src/main/java/com/pedro/encoder/utils/ViewPresentation.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ import androidx.annotation.RequiresApi
1414

1515
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
1616
class ViewPresentation(
17-
private val view: View, context: Context, display: Display
17+
context: Context, display: Display
1818
): Presentation(context, display) {
1919
override fun onCreate(savedInstanceState: Bundle?) {
2020
super.onCreate(savedInstanceState)
2121
window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
2222
window?.setFormat(PixelFormat.TRANSLUCENT)
23+
}
24+
25+
override fun setContentView(view: View) {
26+
(view.parent as? ViewGroup)?.removeView(view)
27+
super.setContentView(view)
28+
}
29+
30+
override fun setContentView(view: View, params: ViewGroup.LayoutParams?) {
2331
(view.parent as? ViewGroup)?.removeView(view)
24-
setContentView(view)
32+
super.setContentView(view, params)
2533
}
2634
}

library/src/main/java/com/pedro/library/view/GlStreamInterface.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
157157
}
158158

159159
override fun removeMediaCodecSurface() {
160+
threadQueue.clear()
160161
surfaceManagerEncoder.release()
161162
}
162163

@@ -168,6 +169,7 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
168169
}
169170

170171
override fun removeMediaCodecRecordSurface() {
172+
threadQueue.clear()
171173
surfaceManagerEncoderRecord.release()
172174
}
173175

library/src/main/java/com/pedro/library/view/OpenGlView.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ open class OpenGlView : SurfaceView, GlInterface, OnFrameAvailableListener, Surf
283283
}
284284

285285
override fun removeMediaCodecSurface() {
286+
threadQueue.clear()
286287
surfaceManagerEncoder.release()
287288
}
288289

@@ -294,6 +295,7 @@ open class OpenGlView : SurfaceView, GlInterface, OnFrameAvailableListener, Surf
294295
}
295296

296297
override fun removeMediaCodecRecordSurface() {
298+
threadQueue.clear()
297299
surfaceManagerEncoderRecord.release()
298300
}
299301

0 commit comments

Comments
 (0)