1 /*
2  * Copyright 2023 The Android Open Source Project
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 
17 package androidx.camera.camera2.pipe.media
18 
19 import androidx.camera.camera2.pipe.OutputId
20 import androidx.camera.camera2.pipe.StreamId
21 import kotlin.reflect.KClass
22 
23 /**
24  * An OutputImage is a reference to an [ImageWrapper] that was produced from CameraPipe for a
25  * specific [StreamId]/[OutputId] combination.
26  */
27 public interface OutputImage : ImageWrapper {
28     public val streamId: StreamId
29     public val outputId: OutputId
30 
31     public companion object {
fromnull32         public fun from(streamId: StreamId, outputId: OutputId, image: ImageWrapper): OutputImage {
33             if (image is OutputImage) return image
34             return OutputImageImpl(streamId, outputId, image)
35         }
36 
37         private class OutputImageImpl(
38             override val streamId: StreamId,
39             override val outputId: OutputId,
40             private val image: ImageWrapper,
41         ) : ImageWrapper by image, OutputImage {
42             @Suppress("UNCHECKED_CAST")
unwrapAsnull43             override fun <T : Any> unwrapAs(type: KClass<T>): T? =
44                 when (type) {
45                     OutputImage::class -> this as T?
46                     ImageWrapper::class -> this as T?
47                     else -> image.unwrapAs(type)
48                 }
49         }
50     }
51 }
52