blob: 2f1e2e98e41d9ff533b522d6ffed8b3024219aeb [file] [log] [blame]
Iliyan Malchev8951a972011-04-14 16:55:59 -07001/*
2 * Copyright (C) 2008 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_HARDWARE_CAMERA_HARDWARE_INTERFACE_H
18#define ANDROID_HARDWARE_CAMERA_HARDWARE_INTERFACE_H
19
Yin-Chia Yeh4717c472017-02-13 10:56:40 -080020#include <unordered_map>
Iliyan Malchev8951a972011-04-14 16:55:59 -070021#include <binder/IMemory.h>
22#include <binder/MemoryBase.h>
23#include <binder/MemoryHeapBase.h>
24#include <utils/RefBase.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070025#include <ui/GraphicBuffer.h>
26#include <camera/Camera.h>
27#include <camera/CameraParameters.h>
28#include <system/window.h>
29#include <hardware/camera.h>
30
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080031#include <common/CameraProviderManager.h>
32
Iliyan Malchev8951a972011-04-14 16:55:59 -070033namespace android {
34
35typedef void (*notify_callback)(int32_t msgType,
36 int32_t ext1,
37 int32_t ext2,
38 void* user);
39
40typedef void (*data_callback)(int32_t msgType,
41 const sp<IMemory> &dataPtr,
Wu-cheng Liff09ef82011-07-28 05:30:59 +080042 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -070043 void* user);
44
45typedef void (*data_callback_timestamp)(nsecs_t timestamp,
46 int32_t msgType,
47 const sp<IMemory> &dataPtr,
48 void *user);
49
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070050struct HandleTimestampMessage {
51 nsecs_t timestamp;
52 const sp<IMemory> dataPtr;
53};
54
55typedef void (*data_callback_timestamp_batch)(
56 int32_t msgType,
57 const std::vector<HandleTimestampMessage>&, void* user);
58
Iliyan Malchev8951a972011-04-14 16:55:59 -070059/**
60 * CameraHardwareInterface.h defines the interface to the
61 * camera hardware abstraction layer, used for setting and getting
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080062 * parameters, live previewing, and taking pictures. It is used for
63 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only.
Iliyan Malchev8951a972011-04-14 16:55:59 -070064 *
65 * It is a referenced counted interface with RefBase as its base class.
66 * CameraService calls openCameraHardware() to retrieve a strong pointer to the
67 * instance of this interface and may be called multiple times. The
68 * following steps describe a typical sequence:
69 *
70 * -# After CameraService calls openCameraHardware(), getParameters() and
71 * setParameters() are used to initialize the camera instance.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080072 * -# startPreview() is called.
Iliyan Malchev8951a972011-04-14 16:55:59 -070073 *
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080074 * Prior to taking a picture, CameraService often calls autofocus(). When auto
Iliyan Malchev8951a972011-04-14 16:55:59 -070075 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification,
76 * which informs the application whether focusing was successful. The camera instance
77 * only sends this message once and it is up to the application to call autoFocus()
78 * again if refocusing is desired.
79 *
80 * CameraService calls takePicture() to request the camera instance take a
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080081 * picture. At this point, if a shutter, postview, raw, and/or compressed
82 * callback is desired, the corresponding message must be enabled. Any memory
83 * provided in a data callback must be copied if it's needed after returning.
Iliyan Malchev8951a972011-04-14 16:55:59 -070084 */
85
Yin-Chia Yeh4717c472017-02-13 10:56:40 -080086class CameraHardwareInterface :
87 public virtual RefBase,
88 public virtual hardware::camera::device::V1_0::ICameraDeviceCallback,
89 public virtual hardware::camera::device::V1_0::ICameraDevicePreviewCallback {
90
Iliyan Malchev8951a972011-04-14 16:55:59 -070091public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -070092 explicit CameraHardwareInterface(const char *name):
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080093 mDevice(nullptr),
Yin-Chia Yeh4717c472017-02-13 10:56:40 -080094 mHidlDevice(nullptr),
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080095 mName(name),
96 mPreviewScalingMode(NOT_SET),
97 mPreviewTransform(NOT_SET),
98 mPreviewWidth(NOT_SET),
99 mPreviewHeight(NOT_SET),
100 mPreviewFormat(NOT_SET),
101 mPreviewUsage(0),
102 mPreviewSwapInterval(NOT_SET),
103 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET}
Iliyan Malchev8951a972011-04-14 16:55:59 -0700104 {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700105 }
106
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800107 ~CameraHardwareInterface();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500108
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800109 status_t initialize(sp<CameraProviderManager> manager);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800110
Iliyan Malchev8951a972011-04-14 16:55:59 -0700111 /** Set the ANativeWindow to which preview frames are sent */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800112 status_t setPreviewWindow(const sp<ANativeWindow>& buf);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700113
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800114 status_t setPreviewScalingMode(int scalingMode);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800115
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800116 status_t setPreviewTransform(int transform);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800117
Iliyan Malchev8951a972011-04-14 16:55:59 -0700118 /** Set the notification and data callbacks */
119 void setCallbacks(notify_callback notify_cb,
120 data_callback data_cb,
121 data_callback_timestamp data_cb_timestamp,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700122 data_callback_timestamp_batch data_cb_timestamp_batch,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800123 void* user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700124
125 /**
126 * The following three functions all take a msgtype,
127 * which is a bitmask of the messages defined in
128 * include/ui/Camera.h
129 */
130
131 /**
132 * Enable a message, or set of messages.
133 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800134 void enableMsgType(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700135
136 /**
137 * Disable a message, or a set of messages.
138 *
139 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
140 * should not rely on its client to call releaseRecordingFrame() to release
141 * video recording frames sent out by the cameral hal before and after the
142 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
143 * modify/access any video recording frame after calling
144 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
145 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800146 void disableMsgType(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700147
148 /**
149 * Query whether a message, or a set of messages, is enabled.
150 * Note that this is operates as an AND, if any of the messages
151 * queried are off, this will return false.
152 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800153 int msgTypeEnabled(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700154
155 /**
156 * Start preview mode.
157 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800158 status_t startPreview();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700159
160 /**
161 * Stop a previously started preview.
162 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800163 void stopPreview();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700164
165 /**
166 * Returns true if preview is enabled.
167 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800168 int previewEnabled();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700169
170 /**
171 * Request the camera hal to store meta data or real YUV data in
172 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
173 * recording session. If it is not called, the default camera
174 * hal behavior is to store real YUV data in the video buffers.
175 *
176 * This method should be called before startRecording() in order
177 * to be effective.
178 *
179 * If meta data is stored in the video buffers, it is up to the
180 * receiver of the video buffers to interpret the contents and
181 * to find the actual frame data with the help of the meta data
182 * in the buffer. How this is done is outside of the scope of
183 * this method.
184 *
185 * Some camera hal may not support storing meta data in the video
186 * buffers, but all camera hal should support storing real YUV data
187 * in the video buffers. If the camera hal does not support storing
188 * the meta data in the video buffers when it is requested to do
189 * do, INVALID_OPERATION must be returned. It is very useful for
190 * the camera hal to pass meta data rather than the actual frame
191 * data directly to the video encoder, since the amount of the
192 * uncompressed frame data can be very large if video size is large.
193 *
194 * @param enable if true to instruct the camera hal to store
195 * meta data in the video buffers; false to instruct
196 * the camera hal to store real YUV data in the video
197 * buffers.
198 *
199 * @return OK on success.
200 */
201
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800202 status_t storeMetaDataInBuffers(int enable);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700203
204 /**
205 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
206 * message is sent with the corresponding frame. Every record frame must be released
207 * by a cameral hal client via releaseRecordingFrame() before the client calls
208 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
209 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
210 * to manage the life-cycle of the video recording frames, and the client must
211 * not modify/access any video recording frames.
212 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800213 status_t startRecording();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700214
215 /**
216 * Stop a previously started recording.
217 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800218 void stopRecording();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700219
220 /**
221 * Returns true if recording is enabled.
222 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800223 int recordingEnabled();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700224
225 /**
226 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
227 *
228 * It is camera hal client's responsibility to release video recording
229 * frames sent out by the camera hal before the camera hal receives
230 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
231 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
232 * responsibility of managing the life-cycle of the video recording
233 * frames.
234 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800235 void releaseRecordingFrame(const sp<IMemory>& mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700236
237 /**
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700238 * Release a batch of recording frames previously returned by
239 * CAMERA_MSG_VIDEO_FRAME. This method only supports frames that are
240 * stored as VideoNativeHandleMetadata.
241 *
242 * It is camera hal client's responsibility to release video recording
243 * frames sent out by the camera hal before the camera hal receives
244 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
245 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
246 * responsibility of managing the life-cycle of the video recording
247 * frames.
248 */
249 void releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames);
250
251 /**
Iliyan Malchev8951a972011-04-14 16:55:59 -0700252 * Start auto focus, the notification callback routine is called
253 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
254 * will be called again if another auto focus is needed.
255 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800256 status_t autoFocus();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700257
258 /**
259 * Cancels auto-focus function. If the auto-focus is still in progress,
260 * this function will cancel it. Whether the auto-focus is in progress
261 * or not, this function will return the focus position to the default.
262 * If the camera does not support auto-focus, this is a no-op.
263 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800264 status_t cancelAutoFocus();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700265
266 /**
267 * Take a picture.
268 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800269 status_t takePicture();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700270
271 /**
272 * Cancel a picture that was started with takePicture. Calling this
273 * method when no picture is being taken is a no-op.
274 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800275 status_t cancelPicture();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700276
277 /**
278 * Set the camera parameters. This returns BAD_VALUE if any parameter is
279 * invalid or not supported. */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800280 status_t setParameters(const CameraParameters &params);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700281
282 /** Return the camera parameters. */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800283 CameraParameters getParameters() const;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700284
285 /**
286 * Send command to camera driver.
287 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800288 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700289
290 /**
291 * Release the hardware resources owned by this object. Note that this is
292 * *not* done in the destructor.
293 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800294 void release();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700295
296 /**
297 * Dump state of the camera hardware
298 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800299 status_t dump(int fd, const Vector<String16>& /*args*/) const;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700300
301private:
302 camera_device_t *mDevice;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800303 sp<hardware::camera::device::V1_0::ICameraDevice> mHidlDevice;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700304 String8 mName;
305
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800306 static void sNotifyCb(int32_t msg_type, int32_t ext1,
307 int32_t ext2, void *user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700308
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800309 static void sDataCb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700310 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800311 camera_frame_metadata_t *metadata,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800312 void *user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700313
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800314 static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700315 const camera_memory_t *data, unsigned index,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800316 void *user);
317
Iliyan Malchev8951a972011-04-14 16:55:59 -0700318 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
319 // in one. Since we tend to use them in a one-to-one relationship, this is
320 // handy.
Iliyan Malchev26adde82011-06-06 17:21:32 -0700321 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700322 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700323 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
324 mBufSize(buf_size),
325 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700326 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700327 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
328 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700329 }
330
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700331 explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
332 mBufSize(buf_size),
333 mNumBufs(num_buffers)
Iliyan Malchev26adde82011-06-06 17:21:32 -0700334 {
335 mHeap = new MemoryHeapBase(buf_size * num_buffers);
336 commonInitialization();
337 }
338
339 void commonInitialization()
340 {
341 handle.data = mHeap->base();
342 handle.size = mBufSize * mNumBufs;
343 handle.handle = this;
344
345 mBuffers = new sp<MemoryBase>[mNumBufs];
346 for (uint_t i = 0; i < mNumBufs; i++)
347 mBuffers[i] = new MemoryBase(mHeap,
348 i * mBufSize,
349 mBufSize);
350
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800351 handle.release = sPutMemory;
Iliyan Malchev26adde82011-06-06 17:21:32 -0700352 }
353
354 virtual ~CameraHeapMemory()
355 {
356 delete [] mBuffers;
357 }
358
359 size_t mBufSize;
360 uint_t mNumBufs;
361 sp<MemoryHeapBase> mHeap;
362 sp<MemoryBase> *mBuffers;
363
Iliyan Malchev8951a972011-04-14 16:55:59 -0700364 camera_memory_t handle;
365 };
366
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800367 static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs,
368 void *user __attribute__((unused)));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700369
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800370 static void sPutMemory(camera_memory_t *data);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700371
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800372 static ANativeWindow *sToAnw(void *user);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700373
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800374 static int sDequeueBuffer(struct preview_stream_ops* w,
375 buffer_handle_t** buffer, int *stride);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700376
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800377 static int sLockBuffer(struct preview_stream_ops* w,
378 buffer_handle_t* /*buffer*/);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700379
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800380 static int sEnqueueBuffer(struct preview_stream_ops* w,
381 buffer_handle_t* buffer);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700382
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800383 static int sCancelBuffer(struct preview_stream_ops* w,
384 buffer_handle_t* buffer);
Sundar Raman1e06f432011-06-17 09:05:09 -0500385
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800386 static int sSetBufferCount(struct preview_stream_ops* w, int count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700387
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800388 static int sSetBuffersGeometry(struct preview_stream_ops* w,
389 int width, int height, int format);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700390
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800391 static int sSetCrop(struct preview_stream_ops *w,
392 int left, int top, int right, int bottom);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800393
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800394 static int sSetTimestamp(struct preview_stream_ops *w,
395 int64_t timestamp);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800396
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800397 static int sSetUsage(struct preview_stream_ops* w, int usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700398
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800399 static int sSetSwapInterval(struct preview_stream_ops *w, int interval);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700400
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800401 static int sGetMinUndequeuedBufferCount(
Iliyan Malchev8951a972011-04-14 16:55:59 -0700402 const struct preview_stream_ops *w,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800403 int *count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700404
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800405 void initHalPreviewWindow();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700406
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800407 std::pair<bool, uint64_t> getBufferId(ANativeWindowBuffer* anb);
408 void cleanupCirculatingBuffers();
409
410 /**
411 * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback
412 */
413 hardware::Return<void> notifyCallback(
414 hardware::camera::device::V1_0::NotifyCallbackMsg msgType,
415 int32_t ext1, int32_t ext2) override;
416 hardware::Return<uint32_t> registerMemory(
417 const hardware::hidl_handle& descriptor,
418 uint32_t bufferSize, uint32_t bufferCount) override;
419 hardware::Return<void> unregisterMemory(uint32_t memId) override;
420 hardware::Return<void> dataCallback(
421 hardware::camera::device::V1_0::DataCallbackMsg msgType,
422 uint32_t data, uint32_t bufferIndex,
423 const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) override;
424 hardware::Return<void> dataCallbackTimestamp(
425 hardware::camera::device::V1_0::DataCallbackMsg msgType,
426 uint32_t data, uint32_t bufferIndex, int64_t timestamp) override;
427 hardware::Return<void> handleCallbackTimestamp(
428 hardware::camera::device::V1_0::DataCallbackMsg msgType,
429 const hardware::hidl_handle& frameData, uint32_t data,
430 uint32_t bufferIndex, int64_t timestamp) override;
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700431 hardware::Return<void> handleCallbackTimestampBatch(
432 hardware::camera::device::V1_0::DataCallbackMsg msgType,
433 const hardware::hidl_vec<
434 hardware::camera::device::V1_0::HandleTimestampMessage>&) override;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800435
436 /**
437 * Implementation of android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback
438 */
439 hardware::Return<void> dequeueBuffer(dequeueBuffer_cb _hidl_cb) override;
440 hardware::Return<hardware::camera::common::V1_0::Status>
441 enqueueBuffer(uint64_t bufferId) override;
442 hardware::Return<hardware::camera::common::V1_0::Status>
443 cancelBuffer(uint64_t bufferId) override;
444 hardware::Return<hardware::camera::common::V1_0::Status>
445 setBufferCount(uint32_t count) override;
446 hardware::Return<hardware::camera::common::V1_0::Status>
447 setBuffersGeometry(uint32_t w, uint32_t h,
448 hardware::graphics::common::V1_0::PixelFormat format) override;
449 hardware::Return<hardware::camera::common::V1_0::Status>
450 setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) override;
451 hardware::Return<hardware::camera::common::V1_0::Status>
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700452 setUsage(hardware::graphics::common::V1_0::BufferUsage usage) override;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800453 hardware::Return<hardware::camera::common::V1_0::Status>
454 setSwapInterval(int32_t interval) override;
455 hardware::Return<void> getMinUndequeuedBufferCount(
456 getMinUndequeuedBufferCount_cb _hidl_cb) override;
457 hardware::Return<hardware::camera::common::V1_0::Status>
458 setTimestamp(int64_t timestamp) override;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700459
460 sp<ANativeWindow> mPreviewWindow;
461
462 struct camera_preview_window {
463 struct preview_stream_ops nw;
464 void *user;
465 };
466
467 struct camera_preview_window mHalPreviewWindow;
468
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700469 notify_callback mNotifyCb;
470 data_callback mDataCb;
471 data_callback_timestamp mDataCbTimestamp;
472 data_callback_timestamp_batch mDataCbTimestampBatch;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700473 void *mCbUser;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800474
475 // Cached values for preview stream parameters
476 static const int NOT_SET = -1;
477 int mPreviewScalingMode;
478 int mPreviewTransform;
479 int mPreviewWidth;
480 int mPreviewHeight;
481 int mPreviewFormat;
482 int mPreviewUsage;
483 int mPreviewSwapInterval;
484 android_native_rect_t mPreviewCrop;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800485
486 struct BufferHasher {
487 size_t operator()(const buffer_handle_t& buf) const {
488 if (buf == nullptr)
489 return 0;
490
491 size_t result = 1;
492 result = 31 * result + buf->numFds;
493 result = 31 * result + buf->numInts;
494 int length = buf->numFds + buf->numInts;
495 for (int i = 0; i < length; i++) {
496 result = 31 * result + buf->data[i];
497 }
498 return result;
499 }
500 };
501
502 struct BufferComparator {
503 bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
504 if (buf1->numFds == buf2->numFds && buf1->numInts == buf2->numInts) {
505 int length = buf1->numFds + buf1->numInts;
506 for (int i = 0; i < length; i++) {
507 if (buf1->data[i] != buf2->data[i]) {
508 return false;
509 }
510 }
511 return true;
512 }
513 return false;
514 }
515 };
516
517 std::mutex mBufferIdMapLock; // protecting mBufferIdMap and mNextBufferId
518 typedef std::unordered_map<const buffer_handle_t, uint64_t,
519 BufferHasher, BufferComparator> BufferIdMap;
520 // stream ID -> per stream buffer ID map
521 BufferIdMap mBufferIdMap;
522 std::unordered_map<uint64_t, ANativeWindowBuffer*> mReversedBufMap;
523 uint64_t mNextBufferId = 1;
524 static const uint64_t BUFFER_ID_NO_BUFFER = 0;
525
Yin-Chia Yehb2a65612017-09-07 16:30:46 -0700526 std::mutex mHidlMemPoolMapLock; // protecting mHidlMemPoolMap
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800527 std::unordered_map<int, camera_memory_t*> mHidlMemPoolMap;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700528};
529
530}; // namespace android
531
532#endif