blob: 4e049d81a2012f8b7678ea165f0f7b501285f9e9 [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>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080023#include <camera/camera2/SubmitInfo.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070024
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070026#include "common/FrameProcessorBase.h"
27#include "common/Camera2ClientBase.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070028
Emilian Peev40ead602017-09-26 15:46:36 +010029using android::camera3::OutputStreamInfo;
30
Igor Murashkine7ee7632013-06-11 18:10:18 -070031namespace android {
32
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080033struct CameraDeviceClientBase :
34 public CameraService::BasicClient,
35 public hardware::camera2::BnCameraDeviceUser
Igor Murashkine7ee7632013-06-11 18:10:18 -070036{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080037 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
Igor Murashkine7ee7632013-06-11 18:10:18 -070038
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080039 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
Igor Murashkine7ee7632013-06-11 18:10:18 -070040 return mRemoteCallback;
41 }
42
43protected:
44 CameraDeviceClientBase(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080045 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070046 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080047 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080048 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070049 int cameraFacing,
50 int clientPid,
51 uid_t clientUid,
52 int servicePid);
53
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080054 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070055};
56
57/**
58 * Implements the binder ICameraDeviceUser API,
59 * meant for HAL3-public implementation of
60 * android.hardware.photography.CameraDevice
61 */
62class CameraDeviceClient :
63 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070064 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070065{
66public:
67 /**
68 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
69 */
70
71 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080072 virtual binder::Status submitRequest(
73 const hardware::camera2::CaptureRequest& request,
74 bool streaming = false,
75 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080076 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Jianing Wei90e59c92014-03-12 18:29:36 -070077 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080078 virtual binder::Status submitRequestList(
79 const std::vector<hardware::camera2::CaptureRequest>& requests,
80 bool streaming = false,
81 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080082 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080083 virtual binder::Status cancelRequest(int requestId,
84 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080085 int64_t* lastFrameNumber = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070086
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080087 virtual binder::Status beginConfigure() override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070088
Emilian Peev5fbe0ba2017-10-20 15:45:45 +010089 virtual binder::Status endConfigure(int operatingMode,
90 const hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070091
Yin-Chia Yeh5090c732017-07-20 16:05:29 -070092 // Returns -EBUSY if device is not idle or in error state
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080093 virtual binder::Status deleteStream(int streamId) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070094
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080095 virtual binder::Status createStream(
96 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
97 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080098 int32_t* newStreamId = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070099
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700100 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800101 virtual binder::Status createInputStream(int width, int height, int format,
102 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800103 int32_t* newStreamId = NULL) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700104
105 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800106 virtual binder::Status getInputSurface(
107 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800108 view::Surface *inputSurface) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700109
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800111 virtual binder::Status createDefaultRequest(int templateId,
112 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800113 hardware::camera2::impl::CameraMetadataNative* request) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700114
115 // Get the static metadata for the camera
116 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800117 virtual binder::Status getCameraInfo(
118 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800119 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700120
Zhijun He2ab500c2013-07-23 08:02:53 -0700121 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800122 virtual binder::Status waitUntilIdle() override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700123
124 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800125 virtual binder::Status flush(
126 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800127 int64_t* lastFrameNumber = NULL) override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700128
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700129 // Prepare stream by preallocating its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800130 virtual binder::Status prepare(int32_t streamId) override;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700131
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700132 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800133 virtual binder::Status tearDown(int32_t streamId) override;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700134
Ruben Brunkc78ac262015-08-13 17:58:46 -0700135 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800136 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700137
Emilian Peev40ead602017-09-26 15:46:36 +0100138 // Update an output configuration
139 virtual binder::Status updateOutputConfiguration(int streamId,
140 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
141
Shuzhen Wang758c2152017-01-10 18:26:18 -0800142 // Finalize the output configurations with surfaces not added before.
143 virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800144 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
Zhijun He5d677d12016-05-29 16:52:39 -0700145
Igor Murashkine7ee7632013-06-11 18:10:18 -0700146 /**
147 * Interface used by CameraService
148 */
149
150 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800151 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700152 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800153 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700154 int cameraFacing,
155 int clientPid,
156 uid_t clientUid,
157 int servicePid);
158 virtual ~CameraDeviceClient();
159
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800160 virtual status_t initialize(sp<CameraProviderManager> manager) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700161
162 virtual status_t dump(int fd, const Vector<String16>& args);
163
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800164 virtual status_t dumpClient(int fd, const Vector<String16>& args);
165
Igor Murashkine7ee7632013-06-11 18:10:18 -0700166 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700167 * Device listener interface
168 */
169
170 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800171 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700172 const CaptureResultExtras& resultExtras);
173 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700174 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -0700175 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700176 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700177
178 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700179 * Interface used by independent components of CameraDeviceClient.
180 */
181protected:
182 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700183 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700184 virtual void detachDevice();
185
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700186 // Calculate the ANativeWindow transform from android.sensor.orientation
187 status_t getRotationTransformLocked(/*out*/int32_t* transform);
188
Igor Murashkine7ee7632013-06-11 18:10:18 -0700189private:
Shuzhen Wang0129d522016-10-30 22:43:41 -0700190 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
191 // streamId specifies the index of the stream the surface belongs to, and the
192 // surfaceId specifies the index of the surface within the stream. (one stream
193 // could contain multiple surfaces.)
194 class StreamSurfaceId final {
195 public:
196 StreamSurfaceId() {
197 mStreamId = -1;
198 mSurfaceId = -1;
199 }
200 StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
201 mStreamId = streamId;
202 mSurfaceId = surfaceId;
203 }
204 int32_t streamId() const {
205 return mStreamId;
206 }
207 int32_t surfaceId() const {
208 return mSurfaceId;
209 }
210
211 private:
212 int32_t mStreamId;
213 int32_t mSurfaceId;
214
215 }; // class StreamSurfaceId
216
217private:
Igor Murashkine7ee7632013-06-11 18:10:18 -0700218 /** ICameraDeviceUser interface-related private members */
219
220 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700221 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700222 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
223 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
224
Emilian Peev00420d22018-02-05 21:33:13 +0000225 std::vector<int32_t> mSupportedPhysicalRequestKeys;
226
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800227 template<typename TProviderPtr>
228 status_t initializeImpl(TProviderPtr providerPtr);
229
Igor Murashkine7ee7632013-06-11 18:10:18 -0700230 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800231 binder::Status checkPidStatus(const char* checkLocation);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700232 bool enforceRequestPermissions(CameraMetadata& metadata);
233
Ruben Brunkbba75572014-11-20 17:29:50 -0800234 // Find the square of the euclidean distance between two points
235 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
236
Zhijun He5d677d12016-05-29 16:52:39 -0700237 // Create an output stream with surface deferred for future.
238 binder::Status createDeferredSurfaceStreamLocked(
239 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800240 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700241 int* newStreamId = NULL);
242
243 // Set the stream transform flags to automatically rotate the camera stream for preview use
244 // cases.
245 binder::Status setStreamTransformLocked(int streamId);
246
Ruben Brunkbba75572014-11-20 17:29:50 -0800247 // Find the closest dimensions for a given format in available stream configurations with
248 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700249 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800250 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800251 android_dataspace dataSpace, const CameraMetadata& info,
252 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800253
Eman Copty6d7af0e2016-06-17 20:46:40 -0700254 //check if format is not custom format
255 static bool isPublicFormat(int32_t format);
256
Shuzhen Wang758c2152017-01-10 18:26:18 -0800257 // Create a Surface from an IGraphicBufferProducer. Returns error if
258 // IGraphicBufferProducer's property doesn't match with streamInfo
259 binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
260 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp);
261
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800262
263 // Utility method to insert the surface into SurfaceMap
264 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000265 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
266 /*out*/int32_t* currentStreamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800267
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800268 // Check that the physicalCameraId passed in is spported by the camera
269 // device.
270 bool checkPhysicalCameraId(const String8& physicalCameraId);
271
Shuzhen Wang0129d522016-10-30 22:43:41 -0700272 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
273 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700274
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800275 // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
276 KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
277
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700278 struct InputStreamConfiguration {
279 bool configured;
280 int32_t width;
281 int32_t height;
282 int32_t format;
283 int32_t id;
284 } mInputStream;
285
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700286 // Streaming request ID
287 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700288 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700289 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700290
291 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700292
Zhijun He5d677d12016-05-29 16:52:39 -0700293 // The list of output streams whose surfaces are deferred. We have to track them separately
294 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
295 // Surface is configured, the stream id will be moved to mStreamMap.
296 Vector<int32_t> mDeferredStreams;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700297
Shuzhen Wang758c2152017-01-10 18:26:18 -0800298 // stream ID -> outputStreamInfo mapping
299 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
300
Shuzhen Wang0129d522016-10-30 22:43:41 -0700301 static const int32_t MAX_SURFACES_PER_STREAM = 2;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700302};
303
304}; // namespace android
305
306#endif