blob: 4bd879fa569580470c2114c9c94d939fcfaed26c [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/CameraModule.h>
32#include <common/CameraProviderManager.h>
33
Iliyan Malchev8951a972011-04-14 16:55:59 -070034namespace android {
35
36typedef void (*notify_callback)(int32_t msgType,
37 int32_t ext1,
38 int32_t ext2,
39 void* user);
40
41typedef void (*data_callback)(int32_t msgType,
42 const sp<IMemory> &dataPtr,
Wu-cheng Liff09ef82011-07-28 05:30:59 +080043 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -070044 void* user);
45
46typedef void (*data_callback_timestamp)(nsecs_t timestamp,
47 int32_t msgType,
48 const sp<IMemory> &dataPtr,
49 void *user);
50
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070051struct HandleTimestampMessage {
52 nsecs_t timestamp;
53 const sp<IMemory> dataPtr;
54};
55
56typedef void (*data_callback_timestamp_batch)(
57 int32_t msgType,
58 const std::vector<HandleTimestampMessage>&, void* user);
59
Iliyan Malchev8951a972011-04-14 16:55:59 -070060/**
61 * CameraHardwareInterface.h defines the interface to the
62 * camera hardware abstraction layer, used for setting and getting
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080063 * parameters, live previewing, and taking pictures. It is used for
64 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only.
Iliyan Malchev8951a972011-04-14 16:55:59 -070065 *
66 * It is a referenced counted interface with RefBase as its base class.
67 * CameraService calls openCameraHardware() to retrieve a strong pointer to the
68 * instance of this interface and may be called multiple times. The
69 * following steps describe a typical sequence:
70 *
71 * -# After CameraService calls openCameraHardware(), getParameters() and
72 * setParameters() are used to initialize the camera instance.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080073 * -# startPreview() is called.
Iliyan Malchev8951a972011-04-14 16:55:59 -070074 *
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080075 * Prior to taking a picture, CameraService often calls autofocus(). When auto
Iliyan Malchev8951a972011-04-14 16:55:59 -070076 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification,
77 * which informs the application whether focusing was successful. The camera instance
78 * only sends this message once and it is up to the application to call autoFocus()
79 * again if refocusing is desired.
80 *
81 * CameraService calls takePicture() to request the camera instance take a
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080082 * picture. At this point, if a shutter, postview, raw, and/or compressed
83 * callback is desired, the corresponding message must be enabled. Any memory
84 * provided in a data callback must be copied if it's needed after returning.
Iliyan Malchev8951a972011-04-14 16:55:59 -070085 */
86
Yin-Chia Yeh4717c472017-02-13 10:56:40 -080087class CameraHardwareInterface :
88 public virtual RefBase,
89 public virtual hardware::camera::device::V1_0::ICameraDeviceCallback,
90 public virtual hardware::camera::device::V1_0::ICameraDevicePreviewCallback {
91
Iliyan Malchev8951a972011-04-14 16:55:59 -070092public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -070093 explicit CameraHardwareInterface(const char *name):
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080094 mDevice(nullptr),
Yin-Chia Yeh4717c472017-02-13 10:56:40 -080095 mHidlDevice(nullptr),
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080096 mName(name),
97 mPreviewScalingMode(NOT_SET),
98 mPreviewTransform(NOT_SET),
99 mPreviewWidth(NOT_SET),
100 mPreviewHeight(NOT_SET),
101 mPreviewFormat(NOT_SET),
102 mPreviewUsage(0),
103 mPreviewSwapInterval(NOT_SET),
104 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET}
Iliyan Malchev8951a972011-04-14 16:55:59 -0700105 {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700106 }
107
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800108 ~CameraHardwareInterface();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500109
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800110 status_t initialize(CameraModule *module);
111 status_t initialize(sp<CameraProviderManager> manager);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800112
Iliyan Malchev8951a972011-04-14 16:55:59 -0700113 /** Set the ANativeWindow to which preview frames are sent */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800114 status_t setPreviewWindow(const sp<ANativeWindow>& buf);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700115
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800116 status_t setPreviewScalingMode(int scalingMode);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800117
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800118 status_t setPreviewTransform(int transform);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800119
Iliyan Malchev8951a972011-04-14 16:55:59 -0700120 /** Set the notification and data callbacks */
121 void setCallbacks(notify_callback notify_cb,
122 data_callback data_cb,
123 data_callback_timestamp data_cb_timestamp,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700124 data_callback_timestamp_batch data_cb_timestamp_batch,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800125 void* user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700126
127 /**
128 * The following three functions all take a msgtype,
129 * which is a bitmask of the messages defined in
130 * include/ui/Camera.h
131 */
132
133 /**
134 * Enable a message, or set of messages.
135 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800136 void enableMsgType(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700137
138 /**
139 * Disable a message, or a set of messages.
140 *
141 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
142 * should not rely on its client to call releaseRecordingFrame() to release
143 * video recording frames sent out by the cameral hal before and after the
144 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
145 * modify/access any video recording frame after calling
146 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
147 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800148 void disableMsgType(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700149
150 /**
151 * Query whether a message, or a set of messages, is enabled.
152 * Note that this is operates as an AND, if any of the messages
153 * queried are off, this will return false.
154 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800155 int msgTypeEnabled(int32_t msgType);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700156
157 /**
158 * Start preview mode.
159 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800160 status_t startPreview();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700161
162 /**
163 * Stop a previously started preview.
164 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800165 void stopPreview();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700166
167 /**
168 * Returns true if preview is enabled.
169 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800170 int previewEnabled();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700171
172 /**
173 * Request the camera hal to store meta data or real YUV data in
174 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
175 * recording session. If it is not called, the default camera
176 * hal behavior is to store real YUV data in the video buffers.
177 *
178 * This method should be called before startRecording() in order
179 * to be effective.
180 *
181 * If meta data is stored in the video buffers, it is up to the
182 * receiver of the video buffers to interpret the contents and
183 * to find the actual frame data with the help of the meta data
184 * in the buffer. How this is done is outside of the scope of
185 * this method.
186 *
187 * Some camera hal may not support storing meta data in the video
188 * buffers, but all camera hal should support storing real YUV data
189 * in the video buffers. If the camera hal does not support storing
190 * the meta data in the video buffers when it is requested to do
191 * do, INVALID_OPERATION must be returned. It is very useful for
192 * the camera hal to pass meta data rather than the actual frame
193 * data directly to the video encoder, since the amount of the
194 * uncompressed frame data can be very large if video size is large.
195 *
196 * @param enable if true to instruct the camera hal to store
197 * meta data in the video buffers; false to instruct
198 * the camera hal to store real YUV data in the video
199 * buffers.
200 *
201 * @return OK on success.
202 */
203
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800204 status_t storeMetaDataInBuffers(int enable);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700205
206 /**
207 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
208 * message is sent with the corresponding frame. Every record frame must be released
209 * by a cameral hal client via releaseRecordingFrame() before the client calls
210 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
211 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
212 * to manage the life-cycle of the video recording frames, and the client must
213 * not modify/access any video recording frames.
214 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800215 status_t startRecording();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700216
217 /**
218 * Stop a previously started recording.
219 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800220 void stopRecording();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700221
222 /**
223 * Returns true if recording is enabled.
224 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800225 int recordingEnabled();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700226
227 /**
228 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
229 *
230 * It is camera hal client's responsibility to release video recording
231 * frames sent out by the camera hal before the camera hal receives
232 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
233 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
234 * responsibility of managing the life-cycle of the video recording
235 * frames.
236 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800237 void releaseRecordingFrame(const sp<IMemory>& mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700238
239 /**
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700240 * Release a batch of recording frames previously returned by
241 * CAMERA_MSG_VIDEO_FRAME. This method only supports frames that are
242 * stored as VideoNativeHandleMetadata.
243 *
244 * It is camera hal client's responsibility to release video recording
245 * frames sent out by the camera hal before the camera hal receives
246 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
247 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
248 * responsibility of managing the life-cycle of the video recording
249 * frames.
250 */
251 void releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames);
252
253 /**
Iliyan Malchev8951a972011-04-14 16:55:59 -0700254 * Start auto focus, the notification callback routine is called
255 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
256 * will be called again if another auto focus is needed.
257 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800258 status_t autoFocus();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700259
260 /**
261 * Cancels auto-focus function. If the auto-focus is still in progress,
262 * this function will cancel it. Whether the auto-focus is in progress
263 * or not, this function will return the focus position to the default.
264 * If the camera does not support auto-focus, this is a no-op.
265 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800266 status_t cancelAutoFocus();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700267
268 /**
269 * Take a picture.
270 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800271 status_t takePicture();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700272
273 /**
274 * Cancel a picture that was started with takePicture. Calling this
275 * method when no picture is being taken is a no-op.
276 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800277 status_t cancelPicture();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700278
279 /**
280 * Set the camera parameters. This returns BAD_VALUE if any parameter is
281 * invalid or not supported. */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800282 status_t setParameters(const CameraParameters &params);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700283
284 /** Return the camera parameters. */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800285 CameraParameters getParameters() const;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700286
287 /**
288 * Send command to camera driver.
289 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800290 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700291
292 /**
293 * Release the hardware resources owned by this object. Note that this is
294 * *not* done in the destructor.
295 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800296 void release();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700297
298 /**
299 * Dump state of the camera hardware
300 */
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800301 status_t dump(int fd, const Vector<String16>& /*args*/) const;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700302
303private:
304 camera_device_t *mDevice;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800305 sp<hardware::camera::device::V1_0::ICameraDevice> mHidlDevice;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700306 String8 mName;
307
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800308 static void sNotifyCb(int32_t msg_type, int32_t ext1,
309 int32_t ext2, void *user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700310
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800311 static void sDataCb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700312 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800313 camera_frame_metadata_t *metadata,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800314 void *user);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700315
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800316 static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700317 const camera_memory_t *data, unsigned index,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800318 void *user);
319
320 // TODO: b/35625849
321 // Meta data buffer layout for passing a native_handle to codec
322 // matching frameworks/native/include/media/hardware/MetadataBufferType.h and
323 // frameworks/native/include/media/hardware/HardwareAPI.h
324 struct VideoNativeHandleMetadata {
325 static const uint32_t kMetadataBufferTypeNativeHandleSource = 3;
326 uint32_t eType; // must be kMetadataBufferTypeNativeHandleSource
327 native_handle_t* pHandle;
328 };
Iliyan Malchev8951a972011-04-14 16:55:59 -0700329
330 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
331 // in one. Since we tend to use them in a one-to-one relationship, this is
332 // handy.
Iliyan Malchev26adde82011-06-06 17:21:32 -0700333 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700334 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700335 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
336 mBufSize(buf_size),
337 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700338 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700339 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
340 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700341 }
342
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700343 explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
344 mBufSize(buf_size),
345 mNumBufs(num_buffers)
Iliyan Malchev26adde82011-06-06 17:21:32 -0700346 {
347 mHeap = new MemoryHeapBase(buf_size * num_buffers);
348 commonInitialization();
349 }
350
351 void commonInitialization()
352 {
353 handle.data = mHeap->base();
354 handle.size = mBufSize * mNumBufs;
355 handle.handle = this;
356
357 mBuffers = new sp<MemoryBase>[mNumBufs];
358 for (uint_t i = 0; i < mNumBufs; i++)
359 mBuffers[i] = new MemoryBase(mHeap,
360 i * mBufSize,
361 mBufSize);
362
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800363 handle.release = sPutMemory;
Iliyan Malchev26adde82011-06-06 17:21:32 -0700364 }
365
366 virtual ~CameraHeapMemory()
367 {
368 delete [] mBuffers;
369 }
370
371 size_t mBufSize;
372 uint_t mNumBufs;
373 sp<MemoryHeapBase> mHeap;
374 sp<MemoryBase> *mBuffers;
375
Iliyan Malchev8951a972011-04-14 16:55:59 -0700376 camera_memory_t handle;
377 };
378
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800379 static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs,
380 void *user __attribute__((unused)));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700381
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800382 static void sPutMemory(camera_memory_t *data);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700383
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800384 static ANativeWindow *sToAnw(void *user);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700385
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800386 static int sDequeueBuffer(struct preview_stream_ops* w,
387 buffer_handle_t** buffer, int *stride);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700388
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800389 static int sLockBuffer(struct preview_stream_ops* w,
390 buffer_handle_t* /*buffer*/);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700391
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800392 static int sEnqueueBuffer(struct preview_stream_ops* w,
393 buffer_handle_t* buffer);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700394
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800395 static int sCancelBuffer(struct preview_stream_ops* w,
396 buffer_handle_t* buffer);
Sundar Raman1e06f432011-06-17 09:05:09 -0500397
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800398 static int sSetBufferCount(struct preview_stream_ops* w, int count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700399
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800400 static int sSetBuffersGeometry(struct preview_stream_ops* w,
401 int width, int height, int format);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700402
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800403 static int sSetCrop(struct preview_stream_ops *w,
404 int left, int top, int right, int bottom);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800405
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800406 static int sSetTimestamp(struct preview_stream_ops *w,
407 int64_t timestamp);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800408
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800409 static int sSetUsage(struct preview_stream_ops* w, int usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700410
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800411 static int sSetSwapInterval(struct preview_stream_ops *w, int interval);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700412
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800413 static int sGetMinUndequeuedBufferCount(
Iliyan Malchev8951a972011-04-14 16:55:59 -0700414 const struct preview_stream_ops *w,
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800415 int *count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700416
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800417 void initHalPreviewWindow();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700418
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800419 std::pair<bool, uint64_t> getBufferId(ANativeWindowBuffer* anb);
420 void cleanupCirculatingBuffers();
421
422 /**
423 * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback
424 */
425 hardware::Return<void> notifyCallback(
426 hardware::camera::device::V1_0::NotifyCallbackMsg msgType,
427 int32_t ext1, int32_t ext2) override;
428 hardware::Return<uint32_t> registerMemory(
429 const hardware::hidl_handle& descriptor,
430 uint32_t bufferSize, uint32_t bufferCount) override;
431 hardware::Return<void> unregisterMemory(uint32_t memId) override;
432 hardware::Return<void> dataCallback(
433 hardware::camera::device::V1_0::DataCallbackMsg msgType,
434 uint32_t data, uint32_t bufferIndex,
435 const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) override;
436 hardware::Return<void> dataCallbackTimestamp(
437 hardware::camera::device::V1_0::DataCallbackMsg msgType,
438 uint32_t data, uint32_t bufferIndex, int64_t timestamp) override;
439 hardware::Return<void> handleCallbackTimestamp(
440 hardware::camera::device::V1_0::DataCallbackMsg msgType,
441 const hardware::hidl_handle& frameData, uint32_t data,
442 uint32_t bufferIndex, int64_t timestamp) override;
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700443 hardware::Return<void> handleCallbackTimestampBatch(
444 hardware::camera::device::V1_0::DataCallbackMsg msgType,
445 const hardware::hidl_vec<
446 hardware::camera::device::V1_0::HandleTimestampMessage>&) override;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800447
448 /**
449 * Implementation of android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback
450 */
451 hardware::Return<void> dequeueBuffer(dequeueBuffer_cb _hidl_cb) override;
452 hardware::Return<hardware::camera::common::V1_0::Status>
453 enqueueBuffer(uint64_t bufferId) override;
454 hardware::Return<hardware::camera::common::V1_0::Status>
455 cancelBuffer(uint64_t bufferId) override;
456 hardware::Return<hardware::camera::common::V1_0::Status>
457 setBufferCount(uint32_t count) override;
458 hardware::Return<hardware::camera::common::V1_0::Status>
459 setBuffersGeometry(uint32_t w, uint32_t h,
460 hardware::graphics::common::V1_0::PixelFormat format) override;
461 hardware::Return<hardware::camera::common::V1_0::Status>
462 setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) override;
463 hardware::Return<hardware::camera::common::V1_0::Status>
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700464 setUsage(hardware::graphics::common::V1_0::BufferUsage usage) override;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800465 hardware::Return<hardware::camera::common::V1_0::Status>
466 setSwapInterval(int32_t interval) override;
467 hardware::Return<void> getMinUndequeuedBufferCount(
468 getMinUndequeuedBufferCount_cb _hidl_cb) override;
469 hardware::Return<hardware::camera::common::V1_0::Status>
470 setTimestamp(int64_t timestamp) override;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700471
472 sp<ANativeWindow> mPreviewWindow;
473
474 struct camera_preview_window {
475 struct preview_stream_ops nw;
476 void *user;
477 };
478
479 struct camera_preview_window mHalPreviewWindow;
480
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700481 notify_callback mNotifyCb;
482 data_callback mDataCb;
483 data_callback_timestamp mDataCbTimestamp;
484 data_callback_timestamp_batch mDataCbTimestampBatch;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700485 void *mCbUser;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800486
487 // Cached values for preview stream parameters
488 static const int NOT_SET = -1;
489 int mPreviewScalingMode;
490 int mPreviewTransform;
491 int mPreviewWidth;
492 int mPreviewHeight;
493 int mPreviewFormat;
494 int mPreviewUsage;
495 int mPreviewSwapInterval;
496 android_native_rect_t mPreviewCrop;
Yin-Chia Yeh4717c472017-02-13 10:56:40 -0800497
498 struct BufferHasher {
499 size_t operator()(const buffer_handle_t& buf) const {
500 if (buf == nullptr)
501 return 0;
502
503 size_t result = 1;
504 result = 31 * result + buf->numFds;
505 result = 31 * result + buf->numInts;
506 int length = buf->numFds + buf->numInts;
507 for (int i = 0; i < length; i++) {
508 result = 31 * result + buf->data[i];
509 }
510 return result;
511 }
512 };
513
514 struct BufferComparator {
515 bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
516 if (buf1->numFds == buf2->numFds && buf1->numInts == buf2->numInts) {
517 int length = buf1->numFds + buf1->numInts;
518 for (int i = 0; i < length; i++) {
519 if (buf1->data[i] != buf2->data[i]) {
520 return false;
521 }
522 }
523 return true;
524 }
525 return false;
526 }
527 };
528
529 std::mutex mBufferIdMapLock; // protecting mBufferIdMap and mNextBufferId
530 typedef std::unordered_map<const buffer_handle_t, uint64_t,
531 BufferHasher, BufferComparator> BufferIdMap;
532 // stream ID -> per stream buffer ID map
533 BufferIdMap mBufferIdMap;
534 std::unordered_map<uint64_t, ANativeWindowBuffer*> mReversedBufMap;
535 uint64_t mNextBufferId = 1;
536 static const uint64_t BUFFER_ID_NO_BUFFER = 0;
537
538 std::unordered_map<int, camera_memory_t*> mHidlMemPoolMap;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700539};
540
541}; // namespace android
542
543#endif