blob: 1c5abb034d9aef062fa83d58fa8c4c688a95b17f [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
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_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
18#define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
19
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080020#include <android/hardware/camera2/BnCameraDeviceUser.h>
21#include <android/hardware/camera2/ICameraDeviceCallbacks.h>
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070022#include <camera/camera2/OutputConfiguration.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000023#include <camera/camera2/SessionConfiguration.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080024#include <camera/camera2/SubmitInfo.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include "common/FrameProcessorBase.h"
28#include "common/Camera2ClientBase.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000029#include "CompositeStream.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070030
Emilian Peev40ead602017-09-26 15:46:36 +010031using android::camera3::OutputStreamInfo;
Emilian Peev538c90e2018-12-17 18:03:19 +000032using android::camera3::CompositeStream;
Emilian Peev40ead602017-09-26 15:46:36 +010033
Igor Murashkine7ee7632013-06-11 18:10:18 -070034namespace android {
35
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036struct CameraDeviceClientBase :
37 public CameraService::BasicClient,
38 public hardware::camera2::BnCameraDeviceUser
Igor Murashkine7ee7632013-06-11 18:10:18 -070039{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
Igor Murashkine7ee7632013-06-11 18:10:18 -070041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
Igor Murashkine7ee7632013-06-11 18:10:18 -070043 return mRemoteCallback;
44 }
45
46protected:
47 CameraDeviceClientBase(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080048 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070049 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080050 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080051 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070052 int cameraFacing,
53 int clientPid,
54 uid_t clientUid,
55 int servicePid);
56
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080057 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070058};
59
60/**
61 * Implements the binder ICameraDeviceUser API,
62 * meant for HAL3-public implementation of
63 * android.hardware.photography.CameraDevice
64 */
65class CameraDeviceClient :
66 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070067 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070068{
69public:
70 /**
71 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
72 */
73
74 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080075 virtual binder::Status submitRequest(
76 const hardware::camera2::CaptureRequest& request,
77 bool streaming = false,
78 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080079 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Jianing Wei90e59c92014-03-12 18:29:36 -070080 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080081 virtual binder::Status submitRequestList(
82 const std::vector<hardware::camera2::CaptureRequest>& requests,
83 bool streaming = false,
84 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080085 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080086 virtual binder::Status cancelRequest(int requestId,
87 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080088 int64_t* lastFrameNumber = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070089
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080090 virtual binder::Status beginConfigure() override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070091
Emilian Peev5fbe0ba2017-10-20 15:45:45 +010092 virtual binder::Status endConfigure(int operatingMode,
93 const hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070094
Emilian Peev35ae8262018-11-08 13:11:32 +000095 // Verify specific session configuration.
96 virtual binder::Status isSessionConfigurationSupported(
97 const SessionConfiguration& sessionConfiguration,
98 /*out*/
99 bool* streamStatus) override;
100
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700101 // Returns -EBUSY if device is not idle or in error state
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800102 virtual binder::Status deleteStream(int streamId) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700103
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800104 virtual binder::Status createStream(
105 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
106 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800107 int32_t* newStreamId = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700108
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700109 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800110 virtual binder::Status createInputStream(int width, int height, int format,
111 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800112 int32_t* newStreamId = NULL) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700113
114 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115 virtual binder::Status getInputSurface(
116 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800117 view::Surface *inputSurface) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700118
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800120 virtual binder::Status createDefaultRequest(int templateId,
121 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800122 hardware::camera2::impl::CameraMetadataNative* request) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123
124 // Get the static metadata for the camera
125 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800126 virtual binder::Status getCameraInfo(
127 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800128 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700129
Zhijun He2ab500c2013-07-23 08:02:53 -0700130 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800131 virtual binder::Status waitUntilIdle() override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700132
133 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800134 virtual binder::Status flush(
135 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800136 int64_t* lastFrameNumber = NULL) override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700137
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700138 // Prepare stream by preallocating its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800139 virtual binder::Status prepare(int32_t streamId) override;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700140
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700141 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800142 virtual binder::Status tearDown(int32_t streamId) override;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700143
Ruben Brunkc78ac262015-08-13 17:58:46 -0700144 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800145 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700146
Emilian Peev40ead602017-09-26 15:46:36 +0100147 // Update an output configuration
148 virtual binder::Status updateOutputConfiguration(int streamId,
149 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
150
Shuzhen Wang758c2152017-01-10 18:26:18 -0800151 // Finalize the output configurations with surfaces not added before.
152 virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800153 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
Zhijun He5d677d12016-05-29 16:52:39 -0700154
Igor Murashkine7ee7632013-06-11 18:10:18 -0700155 /**
156 * Interface used by CameraService
157 */
158
159 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700161 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800162 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700163 int cameraFacing,
164 int clientPid,
165 uid_t clientUid,
166 int servicePid);
167 virtual ~CameraDeviceClient();
168
Emilian Peevbd8c5032018-02-14 23:05:40 +0000169 virtual status_t initialize(sp<CameraProviderManager> manager,
170 const String8& monitorTags) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700171
172 virtual status_t dump(int fd, const Vector<String16>& args);
173
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800174 virtual status_t dumpClient(int fd, const Vector<String16>& args);
175
Igor Murashkine7ee7632013-06-11 18:10:18 -0700176 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700177 * Device listener interface
178 */
179
180 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800181 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700182 const CaptureResultExtras& resultExtras);
183 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700184 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -0700185 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700186 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187
188 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700189 * Interface used by independent components of CameraDeviceClient.
190 */
191protected:
192 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700193 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700194 virtual void detachDevice();
195
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700196 // Calculate the ANativeWindow transform from android.sensor.orientation
197 status_t getRotationTransformLocked(/*out*/int32_t* transform);
198
Igor Murashkine7ee7632013-06-11 18:10:18 -0700199private:
Shuzhen Wang0129d522016-10-30 22:43:41 -0700200 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
201 // streamId specifies the index of the stream the surface belongs to, and the
202 // surfaceId specifies the index of the surface within the stream. (one stream
203 // could contain multiple surfaces.)
204 class StreamSurfaceId final {
205 public:
206 StreamSurfaceId() {
207 mStreamId = -1;
208 mSurfaceId = -1;
209 }
210 StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
211 mStreamId = streamId;
212 mSurfaceId = surfaceId;
213 }
214 int32_t streamId() const {
215 return mStreamId;
216 }
217 int32_t surfaceId() const {
218 return mSurfaceId;
219 }
220
221 private:
222 int32_t mStreamId;
223 int32_t mSurfaceId;
224
225 }; // class StreamSurfaceId
226
227private:
Igor Murashkine7ee7632013-06-11 18:10:18 -0700228 /** ICameraDeviceUser interface-related private members */
229
230 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700231 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700232 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
233 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
234
Emilian Peev00420d22018-02-05 21:33:13 +0000235 std::vector<int32_t> mSupportedPhysicalRequestKeys;
236
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800237 template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000238 status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800239
Igor Murashkine7ee7632013-06-11 18:10:18 -0700240 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800241 binder::Status checkPidStatus(const char* checkLocation);
Emilian Peev35ae8262018-11-08 13:11:32 +0000242 binder::Status checkOperatingModeLocked(int operatingMode) const;
243 binder::Status checkPhysicalCameraIdLocked(String8 physicalCameraId);
244 binder::Status checkSurfaceTypeLocked(size_t numBufferProducers, bool deferredConsumer,
245 int surfaceType) const;
246 static void mapStreamInfo(const OutputStreamInfo &streamInfo,
247 camera3_stream_rotation_t rotation, String8 physicalId,
248 hardware::camera::device::V3_4::Stream *stream /*out*/);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700249 bool enforceRequestPermissions(CameraMetadata& metadata);
250
Ruben Brunkbba75572014-11-20 17:29:50 -0800251 // Find the square of the euclidean distance between two points
252 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
253
Zhijun He5d677d12016-05-29 16:52:39 -0700254 // Create an output stream with surface deferred for future.
255 binder::Status createDeferredSurfaceStreamLocked(
256 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800257 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700258 int* newStreamId = NULL);
259
260 // Set the stream transform flags to automatically rotate the camera stream for preview use
261 // cases.
262 binder::Status setStreamTransformLocked(int streamId);
263
Ruben Brunkbba75572014-11-20 17:29:50 -0800264 // Find the closest dimensions for a given format in available stream configurations with
265 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700266 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800267 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800268 android_dataspace dataSpace, const CameraMetadata& info,
269 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800270
Eman Copty6d7af0e2016-06-17 20:46:40 -0700271 //check if format is not custom format
272 static bool isPublicFormat(int32_t format);
273
Shuzhen Wang758c2152017-01-10 18:26:18 -0800274 // Create a Surface from an IGraphicBufferProducer. Returns error if
275 // IGraphicBufferProducer's property doesn't match with streamInfo
276 binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700277 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
278 const String8& physicalCameraId);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800279
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800280
281 // Utility method to insert the surface into SurfaceMap
282 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000283 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
284 /*out*/int32_t* currentStreamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800285
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800286 // Check that the physicalCameraId passed in is spported by the camera
287 // device.
288 bool checkPhysicalCameraId(const String8& physicalCameraId);
289
Shuzhen Wang0129d522016-10-30 22:43:41 -0700290 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
291 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700292
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800293 // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
294 KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
295
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700296 struct InputStreamConfiguration {
297 bool configured;
298 int32_t width;
299 int32_t height;
300 int32_t format;
301 int32_t id;
302 } mInputStream;
303
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700304 // Streaming request ID
305 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700306 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700307 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700308
309 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700310
Zhijun He5d677d12016-05-29 16:52:39 -0700311 // The list of output streams whose surfaces are deferred. We have to track them separately
312 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
313 // Surface is configured, the stream id will be moved to mStreamMap.
314 Vector<int32_t> mDeferredStreams;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700315
Shuzhen Wang758c2152017-01-10 18:26:18 -0800316 // stream ID -> outputStreamInfo mapping
317 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
318
Emilian Peev538c90e2018-12-17 18:03:19 +0000319 KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap;
320
Emilian Peev35ae8262018-11-08 13:11:32 +0000321 static const int32_t MAX_SURFACES_PER_STREAM = 4;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700322 sp<CameraProviderManager> mProviderManager;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700323};
324
325}; // namespace android
326
327#endif