1 /*
2  * Copyright (C) 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 #ifndef ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASESSIONCONTEXT_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASESSIONCONTEXT_H
19 
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <set>
24 
25 #include "VirtualCameraStream.h"
26 #include "aidl/android/hardware/camera/device/BufferCache.h"
27 #include "aidl/android/hardware/camera/device/CaptureRequest.h"
28 #include "aidl/android/hardware/camera/device/Stream.h"
29 #include "aidl/android/hardware/camera/device/StreamConfiguration.h"
30 
31 namespace android {
32 namespace companion {
33 namespace virtualcamera {
34 
35 // Encapsulates set of streams belonging to the same camera session.
36 class VirtualCameraSessionContext {
37  public:
38   // (Re)initialize the stream.
39   //
40   // Returns true if the stream is initialized for the first time.
41   bool initializeStream(
42       const ::aidl::android::hardware::camera::device::Stream& stream)
43       EXCLUDES(mLock);
44 
45   // Close all streams and free all asociated buffers.
46   void closeAllStreams() EXCLUDES(mLock);
47 
48   // Remove no longer needed buffers.
49   void removeBufferCaches(
50       const std::vector<::aidl::android::hardware::camera::device::BufferCache>&
51           cachesToRemove) EXCLUDES(mLock);
52 
53   // Remove all streams not referenced by provided configuration.
54   void removeStreamsNotInStreamConfiguration(
55       const ::aidl::android::hardware::camera::device::StreamConfiguration&
56           streamConfiguration) EXCLUDES(mLock);
57 
58   // Importored all not-yet imported buffers referenced by the capture request.
59   bool importBuffersFromCaptureRequest(
60       const ::aidl::android::hardware::camera::device::CaptureRequest&
61           captureRequest) EXCLUDES(mLock);
62 
63   // Get stream configuration for provided stream id.
64   // Returns nullopt in case there's no stream with provided stream id.
65   std::optional<::aidl::android::hardware::camera::device::Stream>
66   getStreamConfig(int streamId) const EXCLUDES(mLock);
67 
68   // Get hardware buffer for provided streamId & bufferId.
69   // Returns nullptr in case there's no such buffer.
70   std::shared_ptr<AHardwareBuffer> fetchHardwareBuffer(int streamId,
71                                                        int bufferId) const
72       EXCLUDES(mLock);
73 
74   // Get EGL framebuffer for provided EGL display, streamId & buffer id.
75   //
76   // This will also lazily create EglFrameBuffer for the provided EGLDisplay
77   // connection and will cache it (subsequent calls for same EGLDisplay and
78   // buffer will return same instance of EglFrameBuffer).
79   //
80   // Returns nullptr in case there's no such buffer or it was not possible
81   // to create EglFrameBuffer.
82   std::shared_ptr<EglFrameBuffer> fetchOrCreateEglFramebuffer(
83       const EGLDisplay eglDisplay, int streamId, int bufferId) EXCLUDES(mLock);
84 
85   // Returns set of all stream ids managed by this instance.
86   std::set<int> getStreamIds() const EXCLUDES(mLock);
87 
88  private:
89   mutable std::mutex mLock;
90   // streamId -> VirtualCameraStream mapping.
91   std::map<int, std::unique_ptr<VirtualCameraStream>> mStreams GUARDED_BY(mLock);
92 };
93 
94 }  // namespace virtualcamera
95 }  // namespace companion
96 }  // namespace android
97 
98 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASESSIONCONTEXT_H
99