1 /*
2  * Copyright (C) 2013-2018 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_CAMERADEVICEBASE_H
18 #define ANDROID_SERVERS_CAMERA_CAMERADEVICEBASE_H
19 
20 #include <list>
21 
22 #include <utils/RefBase.h>
23 #include <utils/String16.h>
24 #include <utils/Vector.h>
25 #include <utils/KeyedVector.h>
26 #include <utils/Timers.h>
27 #include <utils/List.h>
28 
29 #include "hardware/camera2.h"
30 #include "camera/CameraMetadata.h"
31 #include "camera/CaptureResult.h"
32 #include "gui/IGraphicBufferProducer.h"
33 #include "device3/Camera3StreamInterface.h"
34 #include "device3/StatusTracker.h"
35 #include "binder/Status.h"
36 #include "FrameProducer.h"
37 #include "utils/IPCTransport.h"
38 #include "utils/SessionConfigurationUtils.h"
39 
40 #include "CameraOfflineSessionBase.h"
41 
42 namespace android {
43 
44 namespace camera3 {
45 
46 typedef enum camera_stream_configuration_mode {
47     CAMERA_STREAM_CONFIGURATION_NORMAL_MODE = 0,
48     CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE = 1,
49     CAMERA_VENDOR_STREAM_CONFIGURATION_MODE_START = 0x8000
50 } camera_stream_configuration_mode_t;
51 
52 // Matches definition of camera3_jpeg_blob in camera3.h and HIDL definition
53 // device@3.2:types.hal, needs to stay around till HIDL support is removed (for
54 // HIDL -> AIDL cameraBlob translation)
55 typedef struct camera_jpeg_blob {
56     uint16_t jpeg_blob_id;
57     uint32_t jpeg_size;
58 } camera_jpeg_blob_t;
59 
60 enum {
61     CAMERA_JPEG_BLOB_ID = 0x00FF,
62     CAMERA_JPEG_APP_SEGMENTS_BLOB_ID = 0x0100,
63 };
64 
65 } // namespace camera3
66 
67 using camera3::camera_request_template_t;;
68 using camera3::camera_stream_configuration_mode_t;
69 using camera3::camera_stream_rotation_t;
70 
71 class CameraProviderManager;
72 
73 // Mapping of output stream index to surface ids
74 typedef std::unordered_map<int, std::vector<size_t> > SurfaceMap;
75 
76 /**
77  * Base interface for version >= 2 camera device classes, which interface to
78  * camera HAL device versions >= 2.
79  */
80 class CameraDeviceBase : public virtual FrameProducer {
81   public:
82     virtual ~CameraDeviceBase();
83 
84     virtual IPCTransport getTransportType() const = 0;
85 
86     /**
87      * The device vendor tag ID
88      */
89     virtual metadata_vendor_id_t getVendorTagId() const = 0;
90 
91     virtual status_t initialize(sp<CameraProviderManager> manager,
92             const std::string& monitorTags) = 0;
93     virtual status_t disconnect() = 0;
94 
95     virtual status_t dump(int fd, const Vector<String16> &args) = 0;
96     virtual status_t startWatchingTags(const std::string &tags) = 0;
97     virtual status_t stopWatchingTags() = 0;
98     virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out) = 0;
99 
100     /**
101      * The physical camera device's static characteristics metadata buffer, or
102      * the logical camera's static characteristics if physical id is empty.
103      */
104     virtual const CameraMetadata& infoPhysical(const std::string& physicalId) const = 0;
105 
isCompositeJpegRDisabled()106     virtual bool isCompositeJpegRDisabled() const { return false; };
107 
108     struct PhysicalCameraSettings {
109         std::string cameraId;
110         CameraMetadata metadata;
111 
112         // Whether the physical camera supports testPatternMode/testPatternData
113         bool mHasTestPatternModeTag = true;
114         bool mHasTestPatternDataTag = true;
115 
116         // Original value of TEST_PATTERN_MODE and DATA so that they can be
117         // restored when sensor muting is turned off
118         int32_t mOriginalTestPatternMode = 0;
119         int32_t mOriginalTestPatternData[4] = {};
120 
121         // Original value of SETTINGS_OVERRIDE so that they can be restored if
122         // camera service isn't overwriting the app value.
123         int32_t mOriginalSettingsOverride = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
124     };
125     typedef List<PhysicalCameraSettings> PhysicalCameraSettingsList;
126 
127     /**
128      * Submit request for capture. The CameraDevice takes ownership of the
129      * passed-in buffer.
130      * Output lastFrameNumber is the expected frame number of this request.
131      */
132     virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
133 
134     /**
135      * Submit a list of requests.
136      * Output lastFrameNumber is the expected last frame number of the list of requests.
137      */
138     virtual status_t captureList(const List<const PhysicalCameraSettingsList> &requests,
139                                  const std::list<const SurfaceMap> &surfaceMaps,
140                                  int64_t *lastFrameNumber = NULL) = 0;
141 
142     /**
143      * Submit request for streaming. The CameraDevice makes a copy of the
144      * passed-in buffer and the caller retains ownership.
145      * Output lastFrameNumber is the last frame number of the previous streaming request.
146      */
147     virtual status_t setStreamingRequest(const CameraMetadata &request,
148                                          int64_t *lastFrameNumber = NULL) = 0;
149 
150     /**
151      * Submit a list of requests for streaming.
152      * Output lastFrameNumber is the last frame number of the previous streaming request.
153      */
154     virtual status_t setStreamingRequestList(const List<const PhysicalCameraSettingsList> &requests,
155                                              const std::list<const SurfaceMap> &surfaceMaps,
156                                              int64_t *lastFrameNumber = NULL) = 0;
157 
158     /**
159      * Clear the streaming request slot.
160      * Output lastFrameNumber is the last frame number of the previous streaming request.
161      */
162     virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
163 
164     /**
165      * Wait until a request with the given ID has been dequeued by the
166      * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
167      * immediately if the latest request received by the HAL has this id.
168      */
169     virtual status_t waitUntilRequestReceived(int32_t requestId,
170             nsecs_t timeout) = 0;
171 
172     /**
173      * Create an output stream of the requested size, format, rotation and dataspace
174      *
175      * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
176      * logical dimensions of the buffer, not the number of bytes.
177      */
178     virtual status_t createStream(sp<Surface> consumer,
179             uint32_t width, uint32_t height, int format,
180             android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
181             const std::string& physicalCameraId,
182             const std::unordered_set<int32_t>  &sensorPixelModesUsed,
183             std::vector<int> *surfaceIds = nullptr,
184             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
185             bool isShared = false, bool isMultiResolution = false,
186             uint64_t consumerUsage = 0,
187             int64_t dynamicProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
188             int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT,
189             int timestampBase = OutputConfiguration::TIMESTAMP_BASE_DEFAULT,
190             int mirrorMode = OutputConfiguration::MIRROR_MODE_AUTO,
191             int32_t colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED,
192             bool useReadoutTimestamp = false)
193             = 0;
194 
195     /**
196      * Create an output stream of the requested size, format, rotation and
197      * dataspace with a number of consumers.
198      *
199      * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
200      * logical dimensions of the buffer, not the number of bytes.
201      */
202     virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
203             bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
204             android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
205             const std::string& physicalCameraId,
206             const std::unordered_set<int32_t> &sensorPixelModesUsed,
207             std::vector<int> *surfaceIds = nullptr,
208             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
209             bool isShared = false, bool isMultiResolution = false,
210             uint64_t consumerUsage = 0,
211             int64_t dynamicProfile = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
212             int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT,
213             int timestampBase = OutputConfiguration::TIMESTAMP_BASE_DEFAULT,
214             int mirrorMode = OutputConfiguration::MIRROR_MODE_AUTO,
215             int32_t colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED,
216             bool useReadoutTimestamp = false)
217             = 0;
218 
219     /**
220      * Create an input stream of width, height, and format.
221      *
222      * Return value is the stream ID if non-negative and an error if negative.
223      */
224     virtual status_t createInputStream(uint32_t width, uint32_t height,
225             int32_t format, bool multiResolution, /*out*/ int32_t *id) = 0;
226 
227     struct StreamInfo {
228         uint32_t width;
229         uint32_t height;
230 
231         uint32_t format;
232         bool formatOverridden;
233         uint32_t originalFormat;
234 
235         android_dataspace dataSpace;
236         bool dataSpaceOverridden;
237         android_dataspace originalDataSpace;
238         int64_t dynamicRangeProfile;
239         int32_t colorSpace;
240 
StreamInfoStreamInfo241         StreamInfo() : width(0), height(0), format(0), formatOverridden(false), originalFormat(0),
242                 dataSpace(HAL_DATASPACE_UNKNOWN), dataSpaceOverridden(false),
243                 originalDataSpace(HAL_DATASPACE_UNKNOWN),
244                 dynamicRangeProfile(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
245                 colorSpace(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED) {}
246         /**
247          * Check whether the format matches the current or the original one in case
248          * it got overridden.
249          */
matchFormatStreamInfo250         bool matchFormat(uint32_t clientFormat) const {
251             if ((formatOverridden && (originalFormat == clientFormat)) ||
252                     (format == clientFormat)) {
253                 return true;
254             }
255             return false;
256         }
257 
258         /**
259          * Check whether the dataspace matches the current or the original one in case
260          * it got overridden.
261          */
matchDataSpaceStreamInfo262         bool matchDataSpace(android_dataspace clientDataSpace) const {
263             if ((dataSpaceOverridden && (originalDataSpace == clientDataSpace)) ||
264                     (dataSpace == clientDataSpace)) {
265                 return true;
266             }
267             return false;
268         }
269 
270     };
271 
272     /**
273      * Get information about a given stream.
274      */
275     virtual status_t getStreamInfo(int id, StreamInfo *streamInfo) = 0;
276 
277     /**
278      * Set stream gralloc buffer transform
279      */
280     virtual status_t setStreamTransform(int id, int transform) = 0;
281 
282     /**
283      * Delete stream. Must not be called if there are requests in flight which
284      * reference that stream.
285      */
286     virtual status_t deleteStream(int id) = 0;
287 
288     /**
289      * Take the currently-defined set of streams and configure the HAL to use
290      * them. This is a long-running operation (may be several hundered ms).
291      *
292      * The device must be idle (see waitUntilDrained) before calling this.
293      *
294      * Returns OK on success; otherwise on error:
295      * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
296      * - INVALID_OPERATION if the device was in the wrong state
297      */
298     virtual status_t configureStreams(const CameraMetadata& sessionParams,
299             int operatingMode =
300             camera_stream_configuration_mode_t::CAMERA_STREAM_CONFIGURATION_NORMAL_MODE) = 0;
301 
302     /**
303      * Retrieve a list of all stream ids that were advertised as capable of
304      * supporting offline processing mode by Hal after the last stream configuration.
305      */
306     virtual void getOfflineStreamIds(std::vector<int> *offlineStreamIds) = 0;
307 
308     // get the buffer producer of the input stream
309     virtual status_t getInputBufferProducer(
310             sp<IGraphicBufferProducer> *producer) = 0;
311 
312     /**
313      * Create a metadata buffer with fields that the HAL device believes are
314      * best for the given use case
315      */
316     virtual status_t createDefaultRequest(camera_request_template_t templateId,
317             CameraMetadata *request) = 0;
318 
319     /**
320      * Wait until all requests have been processed. Returns INVALID_OPERATION if
321      * the streaming slot is not empty, or TIMED_OUT if the requests haven't
322      * finished processing in 10 seconds.
323      */
324     virtual status_t waitUntilDrained() = 0;
325 
326     /**
327      * Get Jpeg buffer size for a given jpeg resolution.
328      * Negative values are error codes.
329      */
330     virtual ssize_t getJpegBufferSize(const CameraMetadata &info, uint32_t width,
331             uint32_t height) const = 0;
332 
333     /**
334      * Connect HAL notifications to a listener. Overwrites previous
335      * listener. Set to NULL to stop receiving notifications.
336      */
337     virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
338 
339     /**
340      * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
341      * and notifyAutoWhitebalance; if this returns false, the client must
342      * synthesize these notifications from received frame metadata.
343      */
344     virtual bool     willNotify3A() = 0;
345 
346     /**
347      * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
348      * autofocus call will be returned by the HAL in all subsequent AF
349      * notifications.
350      */
351     virtual status_t triggerAutofocus(uint32_t id) = 0;
352 
353     /**
354      * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
355      * autofocus call will be returned by the HAL in all subsequent AF
356      * notifications.
357      */
358     virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
359 
360     /**
361      * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
362      * call will be returned by the HAL in all subsequent AE and AWB
363      * notifications.
364      */
365     virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
366 
367     /**
368      * Flush all pending and in-flight requests. Blocks until flush is
369      * complete.
370      * Output lastFrameNumber is the last frame number of the previous streaming request.
371      */
372     virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
373 
374     /**
375      * Prepare stream by preallocating buffers for it asynchronously.
376      * Calls notifyPrepared() once allocation is complete.
377      */
378     virtual status_t prepare(int streamId) = 0;
379 
380     /**
381      * Free stream resources by dumping its unused gralloc buffers.
382      */
383     virtual status_t tearDown(int streamId) = 0;
384 
385     /**
386      * Add buffer listener for a particular stream in the device.
387      */
388     virtual status_t addBufferListenerForStream(int streamId,
389             wp<camera3::Camera3StreamBufferListener> listener) = 0;
390 
391     /**
392      * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
393      * Calls notifyPrepared() once allocation is complete.
394      */
395     virtual status_t prepare(int maxCount, int streamId) = 0;
396 
397     /**
398      * Set the deferred consumer surface and finish the rest of the stream configuration.
399      */
400     virtual status_t setConsumerSurfaces(int streamId,
401             const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds /*out*/) = 0;
402 
403     /**
404      * Update a given stream.
405      */
406     virtual status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
407             const std::vector<android::camera3::OutputStreamInfo> &outputInfo,
408             const std::vector<size_t> &removedSurfaceIds,
409             KeyedVector<sp<Surface>, size_t> *outputMap/*out*/) = 0;
410 
411     /**
412      * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
413      * drop buffers for stream of streamId.
414      */
415     virtual status_t dropStreamBuffers(bool /*dropping*/, int /*streamId*/) = 0;
416 
417     /**
418      * Returns the maximum expected time it'll take for all currently in-flight
419      * requests to complete, based on their settings
420      */
421     virtual nsecs_t getExpectedInFlightDuration() = 0;
422 
423     /**
424      * switch to offline session
425      */
426     virtual status_t switchToOffline(
427             const std::vector<int32_t>& streamsToKeep,
428             /*out*/ sp<CameraOfflineSessionBase>* session) = 0;
429 
430     /**
431      * Set the current behavior for the ROTATE_AND_CROP control when in AUTO.
432      *
433      * The value must be one of the ROTATE_AND_CROP_* values besides AUTO,
434      * and defaults to NONE.
435      */
436     virtual status_t setRotateAndCropAutoBehavior(
437             camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue,
438             bool fromHal = false) = 0;
439 
440     /**
441      * Set the current behavior for the AUTOFRAMING control when in AUTO.
442      *
443      * The value must be one of the AUTOFRAMING_* values besides AUTO.
444      */
445     virtual status_t setAutoframingAutoBehavior(
446             camera_metadata_enum_android_control_autoframing_t autoframingValue) = 0;
447 
448     /**
449      * Whether camera muting (producing black-only output) is supported.
450      *
451      * Calling setCameraMute(true) when this returns false will return an
452      * INVALID_OPERATION error.
453      */
454     virtual bool supportsCameraMute() = 0;
455 
456     /**
457      * Mute the camera.
458      *
459      * When muted, black image data is output on all output streams.
460      */
461     virtual status_t setCameraMute(bool enabled) = 0;
462 
463     /**
464      * Whether the camera device supports zoom override.
465      */
466     virtual bool supportsZoomOverride() = 0;
467 
468     // Set/reset zoom override
469     virtual status_t setZoomOverride(int32_t zoomOverride) = 0;
470 
471     /**
472      * Enable/disable camera service watchdog
473      */
474     virtual status_t setCameraServiceWatchdog(bool enabled) = 0;
475 
476     /**
477      * Get the status tracker of the camera device
478      */
479     virtual wp<camera3::StatusTracker> getStatusTracker() = 0;
480 
481     /**
482      * If the device is in eror state
483      */
484     virtual bool hasDeviceError() = 0;
485 
486     /**
487      * Set bitmask for image dump flag
488      */
setImageDumpMask(int mask)489     void setImageDumpMask(int mask) { mImageDumpMask = mask; }
490 
491     /**
492      * Set stream use case overrides
493      */
setStreamUseCaseOverrides(const std::vector<int64_t> & useCaseOverrides)494     void setStreamUseCaseOverrides(const std::vector<int64_t>& useCaseOverrides) {
495           mStreamUseCaseOverrides = useCaseOverrides;
496     }
497 
clearStreamUseCaseOverrides()498     void clearStreamUseCaseOverrides() {}
499 
500     /**
501      * The injection camera session to replace the internal camera
502      * session.
503      */
504     virtual status_t injectCamera(const std::string& injectedCamId,
505             sp<CameraProviderManager> manager) = 0;
506 
507     /**
508      * Stop the injection camera and restore to internal camera session.
509      */
510     virtual status_t stopInjection() = 0;
511 
512     // Inject session parameters into an existing client.
513     virtual status_t injectSessionParams(
514         const CameraMetadata& sessionParams) = 0;
515 
516 protected:
517     bool mImageDumpMask = 0;
518     std::vector<int64_t> mStreamUseCaseOverrides;
519 };
520 
521 }; // namespace android
522 
523 #endif
524