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