blob: 0d7a2c144c591eb981baa4dc2f64749c135fbb7a [file] [log] [blame]
Yin-Chia Yehead91462016-01-06 16:45:08 -08001/*
2 * Copyright (C) 2016 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#ifndef _ACAMERA_CAPTURE_SESSION_H
17#define _ACAMERA_CAPTURE_SESSION_H
18
19#include <set>
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080020#include <string>
Yin-Chia Yehead91462016-01-06 16:45:08 -080021#include <hardware/camera3.h>
Colin Cross7e8d4ba2017-05-04 16:17:42 -070022#include <camera/NdkCameraDevice.h>
Jayant Chowdhary6df26072018-11-06 23:55:12 -080023
24#ifdef __ANDROID_VNDK__
25#include "ndk_vendor/impl/ACameraDevice.h"
Jayant Chowdhary6df26072018-11-06 23:55:12 -080026#else
Yin-Chia Yehead91462016-01-06 16:45:08 -080027#include "ACameraDevice.h"
Avichal Rakesh8effe982023-11-13 18:53:40 -080028#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -080029
30using namespace android;
31
32struct ACaptureSessionOutput {
Avichal Rakesh8effe982023-11-13 18:53:40 -080033 explicit ACaptureSessionOutput(ANativeWindow* window, bool isShared = false,
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080034 const char* physicalCameraId = "") :
35 mWindow(window), mIsShared(isShared), mPhysicalCameraId(physicalCameraId) {};
Yin-Chia Yehead91462016-01-06 16:45:08 -080036
37 bool operator == (const ACaptureSessionOutput& other) const {
38 return mWindow == other.mWindow;
39 }
40 bool operator != (const ACaptureSessionOutput& other) const {
41 return mWindow != other.mWindow;
42 }
43 bool operator < (const ACaptureSessionOutput& other) const {
44 return mWindow < other.mWindow;
45 }
46 bool operator > (const ACaptureSessionOutput& other) const {
47 return mWindow > other.mWindow;
48 }
49
Avichal Rakesh8effe982023-11-13 18:53:40 -080050 inline bool isWindowEqual(ANativeWindow* window) const {
Avichal Rakeshf099b232022-10-27 15:44:50 -070051 return mWindow == window;
52 }
53
54 // returns true if the window was successfully added, false otherwise.
Avichal Rakesh8effe982023-11-13 18:53:40 -080055 inline bool addSharedWindow(ANativeWindow* window) {
Avichal Rakeshf099b232022-10-27 15:44:50 -070056 auto ret = mSharedWindows.insert(window);
57 return ret.second;
58 }
59
60 // returns the number of elements removed.
Avichal Rakesh8effe982023-11-13 18:53:40 -080061 inline size_t removeSharedWindow(ANativeWindow* window) {
Avichal Rakeshf099b232022-10-27 15:44:50 -070062 return mSharedWindows.erase(window);
63 }
64
Avichal Rakesh8effe982023-11-13 18:53:40 -080065 ANativeWindow* mWindow;
66 std::set<ANativeWindow*> mSharedWindows;
Emilian Peev40ead602017-09-26 15:46:36 +010067 bool mIsShared;
Yin-Chia Yehead91462016-01-06 16:45:08 -080068 int mRotation = CAMERA3_STREAM_ROTATION_0;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080069 std::string mPhysicalCameraId;
Yin-Chia Yehead91462016-01-06 16:45:08 -080070};
71
72struct ACaptureSessionOutputContainer {
73 std::set<ACaptureSessionOutput> mOutputs;
74};
75
76/**
Jayant Chowdhary719b4662023-03-14 20:30:57 +000077 * Capture session state callbacks used in {@link ACameraDevice_setPrepareCallbacks}
78 */
79typedef struct ACameraCaptureSession_prepareCallbacks {
80 /// optional application context. This will be passed in the context
81 /// parameter of the {@link onWindowPrepared} callback.
82 void* context;
83
84 ACameraCaptureSession_prepareCallback onWindowPrepared;
85} ACameraCaptureSession_prepareCallbacks;
86
87/**
Yin-Chia Yehead91462016-01-06 16:45:08 -080088 * ACameraCaptureSession opaque struct definition
89 * Leave outside of android namespace because it's NDK struct
90 */
91struct ACameraCaptureSession : public RefBase {
92 public:
Avichal Rakeshf099b232022-10-27 15:44:50 -070093#ifdef __ANDROID_VNDK__
94 ACameraCaptureSession(
95 int id,
96 const ACaptureSessionOutputContainer* outputs,
97 const ACameraCaptureSession_stateCallbacks* cb,
98 std::weak_ptr<android::acam::CameraDevice> device) :
99 mId(id), mOutput(*outputs), mUserSessionCallback(*cb),
100 mDevice(std::move(device)) {}
101#else
Yin-Chia Yehead91462016-01-06 16:45:08 -0800102 ACameraCaptureSession(
103 int id,
104 const ACaptureSessionOutputContainer* outputs,
105 const ACameraCaptureSession_stateCallbacks* cb,
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800106 android::acam::CameraDevice* device) :
Yin-Chia Yehead91462016-01-06 16:45:08 -0800107 mId(id), mOutput(*outputs), mUserSessionCallback(*cb),
108 mDevice(device) {}
Avichal Rakeshf099b232022-10-27 15:44:50 -0700109#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800110
111 // This can be called in app calling close() or after some app callback is finished
112 // Make sure the caller does not hold device or session lock!
113 ~ACameraCaptureSession();
114
115 // No API except Session_Close will work if device is closed
116 // A session will enter closed state when one of the following happens:
117 // 1. Explicitly closed by app
118 // 2. Replaced by a newer session
119 // 3. Device is closed
120 bool isClosed() { Mutex::Autolock _l(mSessionLock); return mIsClosed; }
121
122 // Close the session and mark app no longer need this session.
123 void closeByApp();
124
125 camera_status_t stopRepeating();
126
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700127 camera_status_t abortCaptures();
128
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800129 template<class T>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800130 camera_status_t setRepeatingRequest(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800131 /*optional*/T* cbs,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800132 int numRequests, ACaptureRequest** requests,
133 /*optional*/int* captureSequenceId);
134
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800135 template<class T>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800136 camera_status_t capture(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800137 /*optional*/T* cbs,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800138 int numRequests, ACaptureRequest** requests,
139 /*optional*/int* captureSequenceId);
140
Emilian Peev40ead602017-09-26 15:46:36 +0100141 camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output);
142
Jayant Chowdhary719b4662023-03-14 20:30:57 +0000143 void setWindowPreparedCallback(void *context,
144 ACameraCaptureSession_prepareCallback cb) {
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000145 Mutex::Autolock _l(mSessionLock);
Jayant Chowdhary719b4662023-03-14 20:30:57 +0000146 mPreparedCb.context = context;
147 mPreparedCb.onWindowPrepared = cb;
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000148 }
Avichal Rakesh8effe982023-11-13 18:53:40 -0800149 camera_status_t prepare(ANativeWindow *window);
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000150
Yin-Chia Yehead91462016-01-06 16:45:08 -0800151 ACameraDevice* getDevice();
152
153 private:
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800154 friend class android::acam::CameraDevice;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800155
156 // Close session because app close camera device, camera device got ERROR_DISCONNECTED,
157 // or a new session is replacing this session.
158 void closeByDevice();
159
Avichal Rakeshf099b232022-10-27 15:44:50 -0700160#ifdef __ANDROID_VNDK__
161 std::shared_ptr<android::acam::CameraDevice> getDevicePtr();
162#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800163 sp<android::acam::CameraDevice> getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700164#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800165
166 const int mId;
167 const ACaptureSessionOutputContainer mOutput;
168 const ACameraCaptureSession_stateCallbacks mUserSessionCallback;
Avichal Rakeshf099b232022-10-27 15:44:50 -0700169#ifdef __ANDROID_VNDK__
170 const std::weak_ptr<android::acam::CameraDevice> mDevice;
171#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800172 const wp<android::acam::CameraDevice> mDevice;
Avichal Rakeshf099b232022-10-27 15:44:50 -0700173#endif
174
Yin-Chia Yehead91462016-01-06 16:45:08 -0800175 bool mIsClosed = false;
Yin-Chia Yeh085dd092016-03-02 14:16:31 -0800176 bool mClosedByApp = false;
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000177 ACameraCaptureSession_prepareCallbacks mPreparedCb;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800178 Mutex mSessionLock;
179};
180
181#endif // _ACAMERA_CAPTURE_SESSION_H