xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/device3/Camera3SharedOutputStream.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2016-2018 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 #ifndef ANDROID_SERVERS_CAMERA3_SHARED_OUTPUT_STREAM_H
18 #define ANDROID_SERVERS_CAMERA3_SHARED_OUTPUT_STREAM_H
19 
20 #include <array>
21 
22 #include "Flags.h"
23 
24 #include "Camera3OutputStream.h"
25 
26 #if USE_NEW_STREAM_SPLITTER
27 #include "Camera3StreamSplitter.h"
28 #else
29 #include "deprecated/DeprecatedCamera3StreamSplitter.h"
30 #endif  // USE_NEW_STREAM_SPLITTER
31 
32 namespace android {
33 
34 namespace camera3 {
35 
36 class Camera3SharedOutputStream :
37         public Camera3OutputStream {
38 public:
39     /**
40      * Set up a stream for formats that have 2 dimensions, with multiple
41      * surfaces. A valid stream set id needs to be set to support buffer
42      * sharing between multiple streams.
43      */
44     Camera3SharedOutputStream(int id, const std::vector<SurfaceHolder>& surfaces,
45             uint32_t width, uint32_t height, int format,
46             uint64_t consumerUsage, android_dataspace dataSpace,
47             camera_stream_rotation_t rotation, nsecs_t timestampOffset,
48             const std::string& physicalCameraId,
49             const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport,
50             int setId, bool useHalBufManager, int64_t dynamicProfile, int64_t streamUseCase,
51             bool deviceTimeBaseIsRealtime, int timestampBase,
52             int32_t colorSpace, bool useReadoutTimestamp);
53 
54     virtual ~Camera3SharedOutputStream();
55 
56     void setHalBufferManager(bool enabled) override;
57 
58     virtual status_t notifyBufferReleased(ANativeWindowBuffer *buffer);
59 
60     virtual bool isConsumerConfigurationDeferred(size_t surface_id) const;
61 
62     virtual status_t setConsumers(const std::vector<SurfaceHolder>& consumers);
63 
64     virtual ssize_t getSurfaceId(const sp<Surface> &surface);
65 
66     /**
67      * Query the unique surface IDs of current surfaceIds.
68      * When passing unique surface IDs in returnBuffer(), if the
69      * surfaceId has been removed from the stream, the output corresponding to
70      * the unique surface ID will be ignored and not delivered to client.
71      */
72     virtual status_t getUniqueSurfaceIds(const std::vector<size_t>& surfaceIds,
73             /*out*/std::vector<size_t>* outUniqueIds) override;
74 
75     virtual status_t updateStream(const std::vector<SurfaceHolder> &outputSurfaces,
76             const std::vector<OutputStreamInfo> &outputInfo,
77             const std::vector<size_t> &removedSurfaceIds,
78             KeyedVector<sp<Surface>, size_t> *outputMap/*out*/);
79 
getOfflineProcessingSupport()80     virtual bool getOfflineProcessingSupport() const {
81         // As per Camera spec. shared streams currently do not support
82         // offline mode.
83         return false;
84     }
85 
86     virtual status_t  setTransform(int transform, bool mayChangeMirror, int surfaceId);
87 
88 private:
89 
90     static const size_t kMaxOutputs = 4;
91 
92     // Whether HAL is in control for buffer management. Surface sharing behavior
93     // depends on this flag.
94     bool mUseHalBufManager;
95 
96     // Struct of an output SurfaceHolder, transform, and its unique ID
97     struct SurfaceHolderUniqueId {
98         SurfaceHolder mSurfaceHolder;
99         int mTransform = -1;
100         size_t mId = -1;
101 
102         SurfaceHolderUniqueId() = default;
SurfaceHolderUniqueIdSurfaceHolderUniqueId103         SurfaceHolderUniqueId(size_t id) : mId(id) {}
SurfaceHolderUniqueIdSurfaceHolderUniqueId104         SurfaceHolderUniqueId(const SurfaceHolder& holder, size_t id) :
105                 mSurfaceHolder(holder), mId(id) {}
106     };
107 
108     // Map surfaceId -> SurfaceHolderUniqueId
109     std::array<SurfaceHolderUniqueId, kMaxOutputs> mSurfaceUniqueIds;
110 
111     size_t mNextUniqueSurfaceId = 0;
112 
113     ssize_t getNextSurfaceIdLocked();
114 
115     status_t revertPartialUpdateLocked(const KeyedVector<size_t, SurfaceHolder> &removedSurfaces,
116             const KeyedVector<sp<Surface>, size_t> &attachedSurfaces);
117 
118     /**
119      * The Camera3StreamSplitter object this stream uses for stream
120      * sharing.
121      */
122 #if USE_NEW_STREAM_SPLITTER
123     sp<Camera3StreamSplitter> mStreamSplitter;
124 #else
125     sp<DeprecatedCamera3StreamSplitter> mStreamSplitter;
126 #endif  // USE_NEW_STREAM_SPLITTER
127     /**
128      * Initialize stream splitter.
129      */
130     status_t connectStreamSplitterLocked();
131 
132     /**
133      * Attach the output buffer to stream splitter.
134      * When camera service is doing buffer management, this method will be called
135      * before the buffer is handed out to HAL in request thread.
136      * When HAL is doing buffer management, this method will be called when
137      * the buffer is returned from HAL in hwbinder callback thread.
138      */
139     status_t attachBufferToSplitterLocked(ANativeWindowBuffer* anb,
140             const std::vector<size_t>& surface_ids);
141 
142     /**
143      * Internal Camera3Stream interface
144      */
145     virtual status_t getBufferLocked(camera_stream_buffer *buffer,
146             const std::vector<size_t>& surface_ids);
147 
148     virtual status_t queueBufferToConsumer(sp<ANativeWindow>& consumer,
149             ANativeWindowBuffer* buffer, int anwReleaseFence,
150             const std::vector<size_t>& uniqueSurfaceIds);
151 
152     virtual status_t configureQueueLocked();
153 
154     virtual status_t disconnectLocked();
155 
156     virtual status_t getEndpointUsage(uint64_t *usage);
157 
158 }; // class Camera3SharedOutputStream
159 
160 } // namespace camera3
161 
162 } // namespace android
163 
164 #endif // ANDROID_SERVERS_CAMERA3_SHARED_OUTPUT_STREAM_H
165