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+ }
0 commit comments