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 android.hardware.camera2.MultiResolutionImageReader
20 import android.media.ImageReader
21 import android.view.Surface
22 import androidx.camera.camera2.pipe.OutputId
23 import androidx.camera.camera2.pipe.StreamId
24 import androidx.camera.camera2.pipe.UnsafeWrapper
25 
26 /** Simplified wrapper for [ImageReader]-like classes. */
27 public interface ImageReaderWrapper : UnsafeWrapper, AutoCloseable {
28     /**
29      * Get a Surface that can be used to produce images for this ImageReader.
30      *
31      * @see [ImageReader.getSurface]
32      */
33     public val surface: Surface
34 
35     /**
36      * Get the maximum number of images that can be produced before stalling or throwing exceptions.
37      *
38      * @see [ImageReader.acquireNextImage]
39      * @see [ImageReader.acquireLatestImage]
40      */
41     public val capacity: Int
42 
43     /**
44      * Set the [OnImageListener]. Setting additional listeners will override the previous listener.]
45      */
setOnImageListenernull46     public fun setOnImageListener(onImageListener: OnImageListener)
47 
48     /**
49      * Discard free buffers from the internal memory pool.
50      *
51      * @see [ImageReader.discardFreeBuffers]
52      * @see [MultiResolutionImageReader.flush]
53      */
54     public fun flush()
55 
56     /**
57      * The OnNextImageListener adapts the standard [ImageReader.OnImageAvailableListener] to push
58      * images into a consumer. This consumer is responsible for processing and/or closing images
59      * when they are no longer needed.
60      */
61     public fun interface OnImageListener {
62         /**
63          * Handle the next [ImageWrapper] from an [ImageReaderWrapper]. Implementations are
64          * responsible for closing images when they are no longer in use.
65          *
66          * [ImageWrapper.timestamp] is not guaranteed to be in order when used with a multi-sensor
67          * camera system, but should *usually* be in order
68          */
69         public fun onImage(streamId: StreamId, outputId: OutputId, image: ImageWrapper)
70     }
71 }
72