blob: e1a11db463396421305f86cbb5ea48f63b4f6877 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
2 * Copyright (C) 2013 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_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,
Igor Murashkine7ee7632013-06-11 18:10:18 -070048 int cameraFacing,
49 int clientPid,
50 uid_t clientUid,
51 int servicePid);
52
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070054};
55
56/**
57 * Implements the binder ICameraDeviceUser API,
58 * meant for HAL3-public implementation of
59 * android.hardware.photography.CameraDevice
60 */
61class CameraDeviceClient :
62 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070063 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070064{
65public:
66 /**
67 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
68 */
69
70 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 virtual binder::Status submitRequest(
72 const hardware::camera2::CaptureRequest& request,
73 bool streaming = false,
74 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080075 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Jianing Wei90e59c92014-03-12 18:29:36 -070076 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080077 virtual binder::Status submitRequestList(
78 const std::vector<hardware::camera2::CaptureRequest>& requests,
79 bool streaming = false,
80 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080081 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082 virtual binder::Status cancelRequest(int requestId,
83 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080084 int64_t* lastFrameNumber = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070085
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080086 virtual binder::Status beginConfigure() override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070087
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080088 virtual binder::Status endConfigure(int operatingMode) override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070089
Yin-Chia Yeh5090c732017-07-20 16:05:29 -070090 // Returns -EBUSY if device is not idle or in error state
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080091 virtual binder::Status deleteStream(int streamId) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070092
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080093 virtual binder::Status createStream(
94 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
95 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080096 int32_t* newStreamId = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070097
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070098 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080099 virtual binder::Status createInputStream(int width, int height, int format,
100 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800101 int32_t* newStreamId = NULL) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700102
103 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800104 virtual binder::Status getInputSurface(
105 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800106 view::Surface *inputSurface) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700107
Igor Murashkine7ee7632013-06-11 18:10:18 -0700108 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800109 virtual binder::Status createDefaultRequest(int templateId,
110 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800111 hardware::camera2::impl::CameraMetadataNative* request) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112
113 // Get the static metadata for the camera
114 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115 virtual binder::Status getCameraInfo(
116 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800117 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700118
Zhijun He2ab500c2013-07-23 08:02:53 -0700119 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800120 virtual binder::Status waitUntilIdle() override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700121
122 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800123 virtual binder::Status flush(
124 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800125 int64_t* lastFrameNumber = NULL) override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700126
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700127 // Prepare stream by preallocating its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800128 virtual binder::Status prepare(int32_t streamId) override;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700129
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700130 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800131 virtual binder::Status tearDown(int32_t streamId) override;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700132
Ruben Brunkc78ac262015-08-13 17:58:46 -0700133 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800134 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700135
Emilian Peev40ead602017-09-26 15:46:36 +0100136 // Update an output configuration
137 virtual binder::Status updateOutputConfiguration(int streamId,
138 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
139
Shuzhen Wang758c2152017-01-10 18:26:18 -0800140 // Finalize the output configurations with surfaces not added before.
141 virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800142 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
Zhijun He5d677d12016-05-29 16:52:39 -0700143
Igor Murashkine7ee7632013-06-11 18:10:18 -0700144 /**
145 * Interface used by CameraService
146 */
147
148 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800149 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700150 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800151 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700152 int cameraFacing,
153 int clientPid,
154 uid_t clientUid,
155 int servicePid);
156 virtual ~CameraDeviceClient();
157
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800158 virtual status_t initialize(sp<CameraProviderManager> manager) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700159
160 virtual status_t dump(int fd, const Vector<String16>& args);
161
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800162 virtual status_t dumpClient(int fd, const Vector<String16>& args);
163
Igor Murashkine7ee7632013-06-11 18:10:18 -0700164 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700165 * Device listener interface
166 */
167
168 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800169 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700170 const CaptureResultExtras& resultExtras);
171 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700172 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -0700173 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700174 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700175
176 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700177 * Interface used by independent components of CameraDeviceClient.
178 */
179protected:
180 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700181 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700182 virtual void detachDevice();
183
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700184 // Calculate the ANativeWindow transform from android.sensor.orientation
185 status_t getRotationTransformLocked(/*out*/int32_t* transform);
186
Igor Murashkine7ee7632013-06-11 18:10:18 -0700187private:
Shuzhen Wang0129d522016-10-30 22:43:41 -0700188 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
189 // streamId specifies the index of the stream the surface belongs to, and the
190 // surfaceId specifies the index of the surface within the stream. (one stream
191 // could contain multiple surfaces.)
192 class StreamSurfaceId final {
193 public:
194 StreamSurfaceId() {
195 mStreamId = -1;
196 mSurfaceId = -1;
197 }
198 StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
199 mStreamId = streamId;
200 mSurfaceId = surfaceId;
201 }
202 int32_t streamId() const {
203 return mStreamId;
204 }
205 int32_t surfaceId() const {
206 return mSurfaceId;
207 }
208
209 private:
210 int32_t mStreamId;
211 int32_t mSurfaceId;
212
213 }; // class StreamSurfaceId
214
215private:
Igor Murashkine7ee7632013-06-11 18:10:18 -0700216 /** ICameraDeviceUser interface-related private members */
217
218 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700219 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700220 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
221 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
222
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800223 template<typename TProviderPtr>
224 status_t initializeImpl(TProviderPtr providerPtr);
225
Igor Murashkine7ee7632013-06-11 18:10:18 -0700226 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800227 binder::Status checkPidStatus(const char* checkLocation);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700228 bool enforceRequestPermissions(CameraMetadata& metadata);
229
Ruben Brunkbba75572014-11-20 17:29:50 -0800230 // Find the square of the euclidean distance between two points
231 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
232
Zhijun He5d677d12016-05-29 16:52:39 -0700233 // Create an output stream with surface deferred for future.
234 binder::Status createDeferredSurfaceStreamLocked(
235 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800236 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700237 int* newStreamId = NULL);
238
239 // Set the stream transform flags to automatically rotate the camera stream for preview use
240 // cases.
241 binder::Status setStreamTransformLocked(int streamId);
242
Ruben Brunkbba75572014-11-20 17:29:50 -0800243 // Find the closest dimensions for a given format in available stream configurations with
244 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700245 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800246 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800247 android_dataspace dataSpace, const CameraMetadata& info,
248 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800249
Eman Copty6d7af0e2016-06-17 20:46:40 -0700250 //check if format is not custom format
251 static bool isPublicFormat(int32_t format);
252
Shuzhen Wang758c2152017-01-10 18:26:18 -0800253 // Create a Surface from an IGraphicBufferProducer. Returns error if
254 // IGraphicBufferProducer's property doesn't match with streamInfo
255 binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
256 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp);
257
Shuzhen Wang0129d522016-10-30 22:43:41 -0700258 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
259 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700260
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700261 struct InputStreamConfiguration {
262 bool configured;
263 int32_t width;
264 int32_t height;
265 int32_t format;
266 int32_t id;
267 } mInputStream;
268
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700269 // Streaming request ID
270 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700271 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700272 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700273
274 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700275
Zhijun He5d677d12016-05-29 16:52:39 -0700276 // The list of output streams whose surfaces are deferred. We have to track them separately
277 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
278 // Surface is configured, the stream id will be moved to mStreamMap.
279 Vector<int32_t> mDeferredStreams;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700280
Shuzhen Wang758c2152017-01-10 18:26:18 -0800281 // stream ID -> outputStreamInfo mapping
282 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
283
Shuzhen Wang0129d522016-10-30 22:43:41 -0700284 static const int32_t MAX_SURFACES_PER_STREAM = 2;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700285};
286
287}; // namespace android
288
289#endif