blob: 17a09834987ec3d8675a2934406a014592bd4fdb [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"
Igor Murashkine7ee7632013-06-11 18:10:18 -070029
Emilian Peev40ead602017-09-26 15:46:36 +010030using android::camera3::OutputStreamInfo;
31
Igor Murashkine7ee7632013-06-11 18:10:18 -070032namespace android {
33
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080034struct CameraDeviceClientBase :
35 public CameraService::BasicClient,
36 public hardware::camera2::BnCameraDeviceUser
Igor Murashkine7ee7632013-06-11 18:10:18 -070037{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080038 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
Igor Murashkine7ee7632013-06-11 18:10:18 -070039
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
Igor Murashkine7ee7632013-06-11 18:10:18 -070041 return mRemoteCallback;
42 }
43
44protected:
45 CameraDeviceClientBase(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070047 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080048 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080049 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070050 int cameraFacing,
51 int clientPid,
52 uid_t clientUid,
53 int servicePid);
54
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080055 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056};
57
58/**
59 * Implements the binder ICameraDeviceUser API,
60 * meant for HAL3-public implementation of
61 * android.hardware.photography.CameraDevice
62 */
63class CameraDeviceClient :
64 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070065 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070066{
67public:
68 /**
69 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
70 */
71
72 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080073 virtual binder::Status submitRequest(
74 const hardware::camera2::CaptureRequest& request,
75 bool streaming = false,
76 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080077 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Jianing Wei90e59c92014-03-12 18:29:36 -070078 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080079 virtual binder::Status submitRequestList(
80 const std::vector<hardware::camera2::CaptureRequest>& requests,
81 bool streaming = false,
82 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080083 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084 virtual binder::Status cancelRequest(int requestId,
85 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080086 int64_t* lastFrameNumber = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070087
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080088 virtual binder::Status beginConfigure() override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070089
Emilian Peev5fbe0ba2017-10-20 15:45:45 +010090 virtual binder::Status endConfigure(int operatingMode,
91 const hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070092
Emilian Peev35ae8262018-11-08 13:11:32 +000093 // Verify specific session configuration.
94 virtual binder::Status isSessionConfigurationSupported(
95 const SessionConfiguration& sessionConfiguration,
96 /*out*/
97 bool* streamStatus) override;
98
Yin-Chia Yeh5090c732017-07-20 16:05:29 -070099 // Returns -EBUSY if device is not idle or in error state
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800100 virtual binder::Status deleteStream(int streamId) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700101
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800102 virtual binder::Status createStream(
103 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
104 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800105 int32_t* newStreamId = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700106
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700107 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800108 virtual binder::Status createInputStream(int width, int height, int format,
109 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800110 int32_t* newStreamId = NULL) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700111
112 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800113 virtual binder::Status getInputSurface(
114 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800115 view::Surface *inputSurface) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700116
Igor Murashkine7ee7632013-06-11 18:10:18 -0700117 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800118 virtual binder::Status createDefaultRequest(int templateId,
119 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800120 hardware::camera2::impl::CameraMetadataNative* request) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700121
122 // Get the static metadata for the camera
123 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800124 virtual binder::Status getCameraInfo(
125 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800126 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127
Zhijun He2ab500c2013-07-23 08:02:53 -0700128 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800129 virtual binder::Status waitUntilIdle() override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700130
131 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800132 virtual binder::Status flush(
133 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800134 int64_t* lastFrameNumber = NULL) override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700135
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700136 // Prepare stream by preallocating its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800137 virtual binder::Status prepare(int32_t streamId) override;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700138
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700139 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800140 virtual binder::Status tearDown(int32_t streamId) override;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700141
Ruben Brunkc78ac262015-08-13 17:58:46 -0700142 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800143 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700144
Emilian Peev40ead602017-09-26 15:46:36 +0100145 // Update an output configuration
146 virtual binder::Status updateOutputConfiguration(int streamId,
147 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
148
Shuzhen Wang758c2152017-01-10 18:26:18 -0800149 // Finalize the output configurations with surfaces not added before.
150 virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800151 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
Zhijun He5d677d12016-05-29 16:52:39 -0700152
Igor Murashkine7ee7632013-06-11 18:10:18 -0700153 /**
154 * Interface used by CameraService
155 */
156
157 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800158 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700159 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800160 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700161 int cameraFacing,
162 int clientPid,
163 uid_t clientUid,
164 int servicePid);
165 virtual ~CameraDeviceClient();
166
Emilian Peevbd8c5032018-02-14 23:05:40 +0000167 virtual status_t initialize(sp<CameraProviderManager> manager,
168 const String8& monitorTags) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700169
170 virtual status_t dump(int fd, const Vector<String16>& args);
171
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800172 virtual status_t dumpClient(int fd, const Vector<String16>& args);
173
Igor Murashkine7ee7632013-06-11 18:10:18 -0700174 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700175 * Device listener interface
176 */
177
178 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800179 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700180 const CaptureResultExtras& resultExtras);
181 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700182 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -0700183 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700184 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185
186 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700187 * Interface used by independent components of CameraDeviceClient.
188 */
189protected:
190 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700191 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700192 virtual void detachDevice();
193
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700194 // Calculate the ANativeWindow transform from android.sensor.orientation
195 status_t getRotationTransformLocked(/*out*/int32_t* transform);
196
Igor Murashkine7ee7632013-06-11 18:10:18 -0700197private:
Shuzhen Wang0129d522016-10-30 22:43:41 -0700198 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
199 // streamId specifies the index of the stream the surface belongs to, and the
200 // surfaceId specifies the index of the surface within the stream. (one stream
201 // could contain multiple surfaces.)
202 class StreamSurfaceId final {
203 public:
204 StreamSurfaceId() {
205 mStreamId = -1;
206 mSurfaceId = -1;
207 }
208 StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
209 mStreamId = streamId;
210 mSurfaceId = surfaceId;
211 }
212 int32_t streamId() const {
213 return mStreamId;
214 }
215 int32_t surfaceId() const {
216 return mSurfaceId;
217 }
218
219 private:
220 int32_t mStreamId;
221 int32_t mSurfaceId;
222
223 }; // class StreamSurfaceId
224
225private:
Igor Murashkine7ee7632013-06-11 18:10:18 -0700226 /** ICameraDeviceUser interface-related private members */
227
228 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700229 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700230 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
231 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
232
Emilian Peev00420d22018-02-05 21:33:13 +0000233 std::vector<int32_t> mSupportedPhysicalRequestKeys;
234
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800235 template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000236 status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800237
Igor Murashkine7ee7632013-06-11 18:10:18 -0700238 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800239 binder::Status checkPidStatus(const char* checkLocation);
Emilian Peev35ae8262018-11-08 13:11:32 +0000240 binder::Status checkOperatingModeLocked(int operatingMode) const;
241 binder::Status checkPhysicalCameraIdLocked(String8 physicalCameraId);
242 binder::Status checkSurfaceTypeLocked(size_t numBufferProducers, bool deferredConsumer,
243 int surfaceType) const;
244 static void mapStreamInfo(const OutputStreamInfo &streamInfo,
245 camera3_stream_rotation_t rotation, String8 physicalId,
246 hardware::camera::device::V3_4::Stream *stream /*out*/);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700247 bool enforceRequestPermissions(CameraMetadata& metadata);
248
Ruben Brunkbba75572014-11-20 17:29:50 -0800249 // Find the square of the euclidean distance between two points
250 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
251
Zhijun He5d677d12016-05-29 16:52:39 -0700252 // Create an output stream with surface deferred for future.
253 binder::Status createDeferredSurfaceStreamLocked(
254 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800255 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700256 int* newStreamId = NULL);
257
258 // Set the stream transform flags to automatically rotate the camera stream for preview use
259 // cases.
260 binder::Status setStreamTransformLocked(int streamId);
261
Ruben Brunkbba75572014-11-20 17:29:50 -0800262 // Find the closest dimensions for a given format in available stream configurations with
263 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700264 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800265 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800266 android_dataspace dataSpace, const CameraMetadata& info,
267 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800268
Eman Copty6d7af0e2016-06-17 20:46:40 -0700269 //check if format is not custom format
270 static bool isPublicFormat(int32_t format);
271
Shuzhen Wang758c2152017-01-10 18:26:18 -0800272 // Create a Surface from an IGraphicBufferProducer. Returns error if
273 // IGraphicBufferProducer's property doesn't match with streamInfo
274 binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700275 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
276 const String8& physicalCameraId);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800277
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800278
279 // Utility method to insert the surface into SurfaceMap
280 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000281 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
282 /*out*/int32_t* currentStreamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800283
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800284 // Check that the physicalCameraId passed in is spported by the camera
285 // device.
286 bool checkPhysicalCameraId(const String8& physicalCameraId);
287
Shuzhen Wang0129d522016-10-30 22:43:41 -0700288 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
289 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700290
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800291 // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
292 KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
293
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700294 struct InputStreamConfiguration {
295 bool configured;
296 int32_t width;
297 int32_t height;
298 int32_t format;
299 int32_t id;
300 } mInputStream;
301
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700302 // Streaming request ID
303 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700304 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700305 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700306
307 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700308
Zhijun He5d677d12016-05-29 16:52:39 -0700309 // The list of output streams whose surfaces are deferred. We have to track them separately
310 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
311 // Surface is configured, the stream id will be moved to mStreamMap.
312 Vector<int32_t> mDeferredStreams;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700313
Shuzhen Wang758c2152017-01-10 18:26:18 -0800314 // stream ID -> outputStreamInfo mapping
315 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
316
Emilian Peev35ae8262018-11-08 13:11:32 +0000317 static const int32_t MAX_SURFACES_PER_STREAM = 4;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700318 sp<CameraProviderManager> mProviderManager;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700319};
320
321}; // namespace android
322
323#endif