summaryrefslogtreecommitdiff
path: root/domain/camera/src/main/java/com/google/jetpackcamera/domain/camera/effects/CopyingSurfaceProcessor.kt
blob: 6f626c17caed8bca60f69c69bbf5705ab9c1731c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.jetpackcamera.domain.camera.effects

import android.graphics.SurfaceTexture
import android.opengl.EGL14
import android.opengl.EGLConfig
import android.opengl.EGLExt
import android.opengl.EGLSurface
import android.util.Size
import android.view.Surface
import androidx.camera.core.SurfaceOutput
import androidx.camera.core.SurfaceProcessor
import androidx.camera.core.SurfaceRequest
import androidx.graphics.opengl.GLRenderer
import androidx.graphics.opengl.egl.EGLManager
import androidx.graphics.opengl.egl.EGLSpec
import com.google.jetpackcamera.core.common.RefCounted
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

private const val TIMESTAMP_UNINITIALIZED = -1L

/**
 * This is a [SurfaceProcessor] that passes on the same content from the input
 * surface to the output surface. Used to make a copies of surfaces.
 */
class CopyingSurfaceProcessor(coroutineScope: CoroutineScope) : SurfaceProcessor {

    private val inputSurfaceFlow = MutableStateFlow<SurfaceRequestScope?>(null)
    private val outputSurfaceFlow = MutableStateFlow<SurfaceOutputScope?>(null)

    init {
        coroutineScope.launch(start = CoroutineStart.UNDISPATCHED) {
            inputSurfaceFlow
                .filterNotNull()
                .collectLatest { surfaceRequestScope ->
                    surfaceRequestScope.withSurfaceRequest { surfaceRequest ->

                        val renderCallbacks = ShaderCopy(surfaceRequest.dynamicRange)
                        renderCallbacks.renderWithSurfaceRequest(surfaceRequest)
                    }
                }
        }
    }

    private suspend fun RenderCallbacks.renderWithSurfaceRequest(surfaceRequest: SurfaceRequest) =
        coroutineScope inputScope@{
            var currentTimestamp = TIMESTAMP_UNINITIALIZED
            val surfaceTextureRef = RefCounted<SurfaceTexture> {
                it.release()
            }
            val textureTransform = FloatArray(16)

            val frameUpdateFlow = MutableStateFlow(0)

            val initializeCallback = object : GLRenderer.EGLContextCallback {

                override fun onEGLContextCreated(eglManager: EGLManager) {
                    initRenderer()

                    val surfaceTex = createSurfaceTexture(
                        surfaceRequest.resolution.width,
                        surfaceRequest.resolution.height
                    )

                    // Initialize the reference counted surface texture
                    surfaceTextureRef.initialize(surfaceTex)

                    surfaceTex.setOnFrameAvailableListener {
                        // Increment frame counter
                        frameUpdateFlow.update { it + 1 }
                    }

                    val inputSurface = Surface(surfaceTex)
                    surfaceRequest.provideSurface(inputSurface, Runnable::run) { result ->
                        inputSurface.release()
                        surfaceTextureRef.release()
                        this@inputScope.cancel(
                            "Input surface no longer receiving frames: $result"
                        )
                    }
                }

                override fun onEGLContextDestroyed(eglManager: EGLManager) {
                    // no-op
                }
            }

            val glRenderer = GLRenderer(
                eglSpecFactory = provideEGLSpec,
                eglConfigFactory = initConfig
            )
            glRenderer.registerEGLContextCallback(initializeCallback)
            glRenderer.start(glThreadName)

            val inputRenderTarget = glRenderer.createRenderTarget(
                surfaceRequest.resolution.width,
                surfaceRequest.resolution.height,
                object : GLRenderer.RenderCallback {

                    override fun onDrawFrame(eglManager: EGLManager) {
                        surfaceTextureRef.acquire()?.also {
                            try {
                                currentTimestamp =
                                    if (currentTimestamp == TIMESTAMP_UNINITIALIZED) {
                                        // Don't perform any updates on first draw,
                                        // we're only setting up the context.
                                        0
                                    } else {
                                        it.updateTexImage()
                                        it.getTransformMatrix(textureTransform)
                                        it.timestamp
                                    }
                            } finally {
                                surfaceTextureRef.release()
                            }
                        }
                    }
                }
            )

            // Create the context and initialize the input. This will call RenderTarget.onDrawFrame,
            // but we won't actually update the frame since this triggers adding the frame callback.
            // All subsequent updates will then happen through frameUpdateFlow.
            // This should be updated when https://issuetracker.google.com/331968279 is resolved.
            inputRenderTarget.requestRender()

            // Connect the onConnectToInput callback with the onDisconnectFromInput
            // Should only be called on worker thread
            var connectedToInput = false

            // Should only be called on worker thread
            val onConnectToInput: () -> Boolean = {
                connectedToInput = surfaceTextureRef.acquire() != null
                connectedToInput
            }

            // Should only be called on worker thread
            val onDisconnectFromInput: () -> Unit = {
                if (connectedToInput) {
                    surfaceTextureRef.release()
                    connectedToInput = false
                }
            }

            // Wait for output surfaces
            outputSurfaceFlow
                .onCompletion {
                    glRenderer.stop(cancelPending = false)
                    glRenderer.unregisterEGLContextCallback(initializeCallback)
                }.filterNotNull()
                .collectLatest { surfaceOutputScope ->
                    surfaceOutputScope.withSurfaceOutput { refCountedSurface,
                                                           size,
                                                           updateTransformMatrix ->
                        // If we can't acquire the surface, then the surface output is already
                        // closed, so we'll return and wait for the next output surface.
                        val outputSurface =
                            refCountedSurface.acquire() ?: return@withSurfaceOutput

                        val surfaceTransform = FloatArray(16)
                        val outputRenderTarget = glRenderer.attach(
                            outputSurface,
                            size.width,
                            size.height,
                            object : GLRenderer.RenderCallback {

                                override fun onSurfaceCreated(
                                    spec: EGLSpec,
                                    config: EGLConfig,
                                    surface: Surface,
                                    width: Int,
                                    height: Int
                                ): EGLSurface? {
                                    return if (onConnectToInput()) {
                                        createOutputSurface(spec, config, surface, width, height)
                                    } else {
                                        null
                                    }
                                }

                                override fun onDrawFrame(eglManager: EGLManager) {
                                    val currentDrawSurface = eglManager.currentDrawSurface
                                    if (currentDrawSurface != eglManager.defaultSurface) {
                                        updateTransformMatrix(
                                            surfaceTransform,
                                            textureTransform
                                        )

                                        drawFrame(
                                            size.width,
                                            size.height,
                                            surfaceTransform
                                        )

                                        // Set timestamp
                                        val display =
                                            EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)
                                        EGLExt.eglPresentationTimeANDROID(
                                            display,
                                            eglManager.currentDrawSurface,
                                            currentTimestamp
                                        )
                                    }
                                }
                            }
                        )

                        frameUpdateFlow
                            .onCompletion {
                                outputRenderTarget.detach(cancelPending = false) {
                                    onDisconnectFromInput()
                                    refCountedSurface.release()
                                }
                            }.filterNot { it == 0 } // Don't attempt render on frame count 0
                            .collectLatest {
                                inputRenderTarget.requestRender()
                                outputRenderTarget.requestRender()
                            }
                    }
                }
        }

    override fun onInputSurface(surfaceRequest: SurfaceRequest) {
        val newScope = SurfaceRequestScope(surfaceRequest)
        inputSurfaceFlow.update { old ->
            old?.cancel("New SurfaceRequest received.")
            newScope
        }
    }

    override fun onOutputSurface(surfaceOutput: SurfaceOutput) {
        val newScope = SurfaceOutputScope(surfaceOutput)
        outputSurfaceFlow.update { old ->
            old?.cancel("New SurfaceOutput received.")
            newScope
        }
    }
}

interface RenderCallbacks {
    val glThreadName: String
    val provideEGLSpec: () -> EGLSpec
    val initConfig: EGLManager.() -> EGLConfig
    val initRenderer: () -> Unit
    val createSurfaceTexture: (width: Int, height: Int) -> SurfaceTexture
    val createOutputSurface: (
        eglSpec: EGLSpec,
        config: EGLConfig,
        surface: Surface,
        width: Int,
        height: Int
    ) -> EGLSurface
    val drawFrame: (outputWidth: Int, outputHeight: Int, surfaceTransform: FloatArray) -> Unit
}

private class SurfaceOutputScope(val surfaceOutput: SurfaceOutput) {
    private val surfaceLifecycleJob = SupervisorJob()
    private val refCountedSurface = RefCounted<Surface>(onRelease = {
        surfaceOutput.close()
    }).apply {
        // Ensure we don't release until after `initialize` has completed by deferring
        // the release.
        val deferredRelease = CompletableDeferred<Unit>()
        initialize(
            surfaceOutput.getSurface(Runnable::run) {
                deferredRelease.complete(Unit)
            }
        )
        CoroutineScope(Dispatchers.Unconfined).launch {
            deferredRelease.await()
            surfaceLifecycleJob.cancel("SurfaceOutput close requested.")
            this@apply.release()
        }
    }

    suspend fun <R> withSurfaceOutput(
        block: suspend CoroutineScope.(
            surface: RefCounted<Surface>,
            surfaceSize: Size,
            updateTransformMatrix: (updated: FloatArray, original: FloatArray) -> Unit
        ) -> R
    ): R {
        return CoroutineScope(coroutineContext + Job(surfaceLifecycleJob)).async(
            start = CoroutineStart.UNDISPATCHED
        ) {
            ensureActive()
            block(
                refCountedSurface,
                surfaceOutput.size,
                surfaceOutput::updateTransformMatrix
            )
        }.await()
    }

    fun cancel(message: String? = null) {
        message?.apply { surfaceLifecycleJob.cancel(message) } ?: surfaceLifecycleJob.cancel()
    }
}

private class SurfaceRequestScope(private val surfaceRequest: SurfaceRequest) {
    private val requestLifecycleJob = SupervisorJob()

    init {
        surfaceRequest.addRequestCancellationListener(Runnable::run) {
            requestLifecycleJob.cancel("SurfaceRequest cancelled.")
        }
    }

    suspend fun <R> withSurfaceRequest(
        block: suspend CoroutineScope.(
            surfaceRequest: SurfaceRequest
        ) -> R
    ): R {
        return CoroutineScope(coroutineContext + Job(requestLifecycleJob)).async(
            start = CoroutineStart.UNDISPATCHED
        ) {
            ensureActive()
            block(surfaceRequest)
        }.await()
    }

    fun cancel(message: String? = null) {
        message?.apply { requestLifecycleJob.cancel(message) } ?: requestLifecycleJob.cancel()
        // Attempt to tell frame producer we will not provide a surface. This may fail (silently)
        // if surface was already provided or the producer has cancelled the request, in which
        // case we don't have to do anything.
        surfaceRequest.willNotProvideSurface()
    }
}