Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 20 | #include <unordered_map> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 21 | #include <binder/IMemory.h> |
| 22 | #include <binder/MemoryBase.h> |
| 23 | #include <binder/MemoryHeapBase.h> |
| 24 | #include <utils/RefBase.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 25 | #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 Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 31 | #include <common/CameraProviderManager.h> |
| 32 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | typedef void (*notify_callback)(int32_t msgType, |
| 36 | int32_t ext1, |
| 37 | int32_t ext2, |
| 38 | void* user); |
| 39 | |
| 40 | typedef void (*data_callback)(int32_t msgType, |
| 41 | const sp<IMemory> &dataPtr, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 42 | camera_frame_metadata_t *metadata, |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 43 | void* user); |
| 44 | |
| 45 | typedef void (*data_callback_timestamp)(nsecs_t timestamp, |
| 46 | int32_t msgType, |
| 47 | const sp<IMemory> &dataPtr, |
| 48 | void *user); |
| 49 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 50 | struct HandleTimestampMessage { |
| 51 | nsecs_t timestamp; |
| 52 | const sp<IMemory> dataPtr; |
| 53 | }; |
| 54 | |
| 55 | typedef void (*data_callback_timestamp_batch)( |
| 56 | int32_t msgType, |
| 57 | const std::vector<HandleTimestampMessage>&, void* user); |
| 58 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 59 | /** |
| 60 | * CameraHardwareInterface.h defines the interface to the |
| 61 | * camera hardware abstraction layer, used for setting and getting |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 62 | * parameters, live previewing, and taking pictures. It is used for |
| 63 | * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only. |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 64 | * |
| 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 Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 72 | * -# startPreview() is called. |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 73 | * |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 74 | * Prior to taking a picture, CameraService often calls autofocus(). When auto |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 75 | * 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 Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 81 | * 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 84 | */ |
| 85 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 86 | class 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 91 | public: |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 92 | explicit CameraHardwareInterface(const char *name): |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 93 | mHidlDevice(nullptr), |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 94 | mName(name), |
| 95 | mPreviewScalingMode(NOT_SET), |
| 96 | mPreviewTransform(NOT_SET), |
| 97 | mPreviewWidth(NOT_SET), |
| 98 | mPreviewHeight(NOT_SET), |
| 99 | mPreviewFormat(NOT_SET), |
| 100 | mPreviewUsage(0), |
| 101 | mPreviewSwapInterval(NOT_SET), |
| 102 | mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET} |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 103 | { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 106 | ~CameraHardwareInterface(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 107 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 108 | status_t initialize(sp<CameraProviderManager> manager); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 109 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 110 | /** Set the ANativeWindow to which preview frames are sent */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 111 | status_t setPreviewWindow(const sp<ANativeWindow>& buf); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 112 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 113 | status_t setPreviewScalingMode(int scalingMode); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 114 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 115 | status_t setPreviewTransform(int transform); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 116 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 117 | /** Set the notification and data callbacks */ |
| 118 | void setCallbacks(notify_callback notify_cb, |
| 119 | data_callback data_cb, |
| 120 | data_callback_timestamp data_cb_timestamp, |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 121 | data_callback_timestamp_batch data_cb_timestamp_batch, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 122 | void* user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * The following three functions all take a msgtype, |
| 126 | * which is a bitmask of the messages defined in |
| 127 | * include/ui/Camera.h |
| 128 | */ |
| 129 | |
| 130 | /** |
| 131 | * Enable a message, or set of messages. |
| 132 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 133 | void enableMsgType(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Disable a message, or a set of messages. |
| 137 | * |
| 138 | * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal |
| 139 | * should not rely on its client to call releaseRecordingFrame() to release |
| 140 | * video recording frames sent out by the cameral hal before and after the |
| 141 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not |
| 142 | * modify/access any video recording frame after calling |
| 143 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME). |
| 144 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 145 | void disableMsgType(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Query whether a message, or a set of messages, is enabled. |
| 149 | * Note that this is operates as an AND, if any of the messages |
| 150 | * queried are off, this will return false. |
| 151 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 152 | int msgTypeEnabled(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Start preview mode. |
| 156 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 157 | status_t startPreview(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Stop a previously started preview. |
| 161 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 162 | void stopPreview(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Returns true if preview is enabled. |
| 166 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 167 | int previewEnabled(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Request the camera hal to store meta data or real YUV data in |
| 171 | * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a |
| 172 | * recording session. If it is not called, the default camera |
| 173 | * hal behavior is to store real YUV data in the video buffers. |
| 174 | * |
| 175 | * This method should be called before startRecording() in order |
| 176 | * to be effective. |
| 177 | * |
| 178 | * If meta data is stored in the video buffers, it is up to the |
| 179 | * receiver of the video buffers to interpret the contents and |
| 180 | * to find the actual frame data with the help of the meta data |
| 181 | * in the buffer. How this is done is outside of the scope of |
| 182 | * this method. |
| 183 | * |
| 184 | * Some camera hal may not support storing meta data in the video |
| 185 | * buffers, but all camera hal should support storing real YUV data |
| 186 | * in the video buffers. If the camera hal does not support storing |
| 187 | * the meta data in the video buffers when it is requested to do |
| 188 | * do, INVALID_OPERATION must be returned. It is very useful for |
| 189 | * the camera hal to pass meta data rather than the actual frame |
| 190 | * data directly to the video encoder, since the amount of the |
| 191 | * uncompressed frame data can be very large if video size is large. |
| 192 | * |
| 193 | * @param enable if true to instruct the camera hal to store |
| 194 | * meta data in the video buffers; false to instruct |
| 195 | * the camera hal to store real YUV data in the video |
| 196 | * buffers. |
| 197 | * |
| 198 | * @return OK on success. |
| 199 | */ |
| 200 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 201 | status_t storeMetaDataInBuffers(int enable); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME |
| 205 | * message is sent with the corresponding frame. Every record frame must be released |
| 206 | * by a cameral hal client via releaseRecordingFrame() before the client calls |
| 207 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls |
| 208 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility |
| 209 | * to manage the life-cycle of the video recording frames, and the client must |
| 210 | * not modify/access any video recording frames. |
| 211 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 212 | status_t startRecording(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Stop a previously started recording. |
| 216 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 217 | void stopRecording(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Returns true if recording is enabled. |
| 221 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 222 | int recordingEnabled(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. |
| 226 | * |
| 227 | * It is camera hal client's responsibility to release video recording |
| 228 | * frames sent out by the camera hal before the camera hal receives |
| 229 | * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives |
| 230 | * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's |
| 231 | * responsibility of managing the life-cycle of the video recording |
| 232 | * frames. |
| 233 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 234 | void releaseRecordingFrame(const sp<IMemory>& mem); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 235 | |
| 236 | /** |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 237 | * Release a batch of recording frames previously returned by |
| 238 | * CAMERA_MSG_VIDEO_FRAME. This method only supports frames that are |
| 239 | * stored as VideoNativeHandleMetadata. |
| 240 | * |
| 241 | * It is camera hal client's responsibility to release video recording |
| 242 | * frames sent out by the camera hal before the camera hal receives |
| 243 | * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives |
| 244 | * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's |
| 245 | * responsibility of managing the life-cycle of the video recording |
| 246 | * frames. |
| 247 | */ |
| 248 | void releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames); |
| 249 | |
| 250 | /** |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 251 | * Start auto focus, the notification callback routine is called |
| 252 | * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() |
| 253 | * will be called again if another auto focus is needed. |
| 254 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 255 | status_t autoFocus(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Cancels auto-focus function. If the auto-focus is still in progress, |
| 259 | * this function will cancel it. Whether the auto-focus is in progress |
| 260 | * or not, this function will return the focus position to the default. |
| 261 | * If the camera does not support auto-focus, this is a no-op. |
| 262 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 263 | status_t cancelAutoFocus(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Take a picture. |
| 267 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 268 | status_t takePicture(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Cancel a picture that was started with takePicture. Calling this |
| 272 | * method when no picture is being taken is a no-op. |
| 273 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 274 | status_t cancelPicture(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Set the camera parameters. This returns BAD_VALUE if any parameter is |
| 278 | * invalid or not supported. */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 279 | status_t setParameters(const CameraParameters ¶ms); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 280 | |
| 281 | /** Return the camera parameters. */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 282 | CameraParameters getParameters() const; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Send command to camera driver. |
| 286 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 287 | status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Release the hardware resources owned by this object. Note that this is |
| 291 | * *not* done in the destructor. |
| 292 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 293 | void release(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Dump state of the camera hardware |
| 297 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 298 | status_t dump(int fd, const Vector<String16>& /*args*/) const; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 299 | |
| 300 | private: |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 301 | sp<hardware::camera::device::V1_0::ICameraDevice> mHidlDevice; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 302 | String8 mName; |
| 303 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 304 | static void sNotifyCb(int32_t msg_type, int32_t ext1, |
| 305 | int32_t ext2, void *user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 306 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 307 | static void sDataCb(int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 308 | const camera_memory_t *data, unsigned int index, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 309 | camera_frame_metadata_t *metadata, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 310 | void *user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 311 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 312 | static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 313 | const camera_memory_t *data, unsigned index, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 314 | void *user); |
| 315 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 316 | // This is a utility class that combines a MemoryHeapBase and a MemoryBase |
| 317 | // in one. Since we tend to use them in a one-to-one relationship, this is |
| 318 | // handy. |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 319 | class CameraHeapMemory : public RefBase { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 320 | public: |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 321 | CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) : |
| 322 | mBufSize(buf_size), |
| 323 | mNumBufs(num_buffers) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 324 | { |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 325 | mHeap = new MemoryHeapBase(fd, buf_size * num_buffers); |
| 326 | commonInitialization(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 329 | explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) : |
| 330 | mBufSize(buf_size), |
| 331 | mNumBufs(num_buffers) |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 332 | { |
| 333 | mHeap = new MemoryHeapBase(buf_size * num_buffers); |
| 334 | commonInitialization(); |
| 335 | } |
| 336 | |
| 337 | void commonInitialization() |
| 338 | { |
| 339 | handle.data = mHeap->base(); |
| 340 | handle.size = mBufSize * mNumBufs; |
| 341 | handle.handle = this; |
| 342 | |
| 343 | mBuffers = new sp<MemoryBase>[mNumBufs]; |
| 344 | for (uint_t i = 0; i < mNumBufs; i++) |
| 345 | mBuffers[i] = new MemoryBase(mHeap, |
| 346 | i * mBufSize, |
| 347 | mBufSize); |
| 348 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 349 | handle.release = sPutMemory; |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | virtual ~CameraHeapMemory() |
| 353 | { |
| 354 | delete [] mBuffers; |
| 355 | } |
| 356 | |
| 357 | size_t mBufSize; |
| 358 | uint_t mNumBufs; |
| 359 | sp<MemoryHeapBase> mHeap; |
| 360 | sp<MemoryBase> *mBuffers; |
| 361 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 362 | camera_memory_t handle; |
| 363 | }; |
| 364 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 365 | static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs, |
| 366 | void *user __attribute__((unused))); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 367 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 368 | static void sPutMemory(camera_memory_t *data); |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 369 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 370 | std::pair<bool, uint64_t> getBufferId(ANativeWindowBuffer* anb); |
| 371 | void cleanupCirculatingBuffers(); |
| 372 | |
| 373 | /** |
| 374 | * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback |
| 375 | */ |
| 376 | hardware::Return<void> notifyCallback( |
| 377 | hardware::camera::device::V1_0::NotifyCallbackMsg msgType, |
| 378 | int32_t ext1, int32_t ext2) override; |
| 379 | hardware::Return<uint32_t> registerMemory( |
| 380 | const hardware::hidl_handle& descriptor, |
| 381 | uint32_t bufferSize, uint32_t bufferCount) override; |
| 382 | hardware::Return<void> unregisterMemory(uint32_t memId) override; |
| 383 | hardware::Return<void> dataCallback( |
| 384 | hardware::camera::device::V1_0::DataCallbackMsg msgType, |
| 385 | uint32_t data, uint32_t bufferIndex, |
| 386 | const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) override; |
| 387 | hardware::Return<void> dataCallbackTimestamp( |
| 388 | hardware::camera::device::V1_0::DataCallbackMsg msgType, |
| 389 | uint32_t data, uint32_t bufferIndex, int64_t timestamp) override; |
| 390 | hardware::Return<void> handleCallbackTimestamp( |
| 391 | hardware::camera::device::V1_0::DataCallbackMsg msgType, |
| 392 | const hardware::hidl_handle& frameData, uint32_t data, |
| 393 | uint32_t bufferIndex, int64_t timestamp) override; |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 394 | hardware::Return<void> handleCallbackTimestampBatch( |
| 395 | hardware::camera::device::V1_0::DataCallbackMsg msgType, |
| 396 | const hardware::hidl_vec< |
| 397 | hardware::camera::device::V1_0::HandleTimestampMessage>&) override; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 398 | |
| 399 | /** |
| 400 | * Implementation of android::hardware::camera::device::V1_0::ICameraDevicePreviewCallback |
| 401 | */ |
| 402 | hardware::Return<void> dequeueBuffer(dequeueBuffer_cb _hidl_cb) override; |
| 403 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 404 | enqueueBuffer(uint64_t bufferId) override; |
| 405 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 406 | cancelBuffer(uint64_t bufferId) override; |
| 407 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 408 | setBufferCount(uint32_t count) override; |
| 409 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 410 | setBuffersGeometry(uint32_t w, uint32_t h, |
| 411 | hardware::graphics::common::V1_0::PixelFormat format) override; |
| 412 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 413 | setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) override; |
| 414 | hardware::Return<hardware::camera::common::V1_0::Status> |
Chia-I Wu | 67a0c0e | 2017-04-06 13:37:01 -0700 | [diff] [blame] | 415 | setUsage(hardware::graphics::common::V1_0::BufferUsage usage) override; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 416 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 417 | setSwapInterval(int32_t interval) override; |
| 418 | hardware::Return<void> getMinUndequeuedBufferCount( |
| 419 | getMinUndequeuedBufferCount_cb _hidl_cb) override; |
| 420 | hardware::Return<hardware::camera::common::V1_0::Status> |
| 421 | setTimestamp(int64_t timestamp) override; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 422 | |
| 423 | sp<ANativeWindow> mPreviewWindow; |
| 424 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 425 | notify_callback mNotifyCb; |
| 426 | data_callback mDataCb; |
| 427 | data_callback_timestamp mDataCbTimestamp; |
| 428 | data_callback_timestamp_batch mDataCbTimestampBatch; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 429 | void *mCbUser; |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 430 | |
| 431 | // Cached values for preview stream parameters |
| 432 | static const int NOT_SET = -1; |
| 433 | int mPreviewScalingMode; |
| 434 | int mPreviewTransform; |
| 435 | int mPreviewWidth; |
| 436 | int mPreviewHeight; |
| 437 | int mPreviewFormat; |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 438 | uint64_t mPreviewUsage; |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 439 | int mPreviewSwapInterval; |
| 440 | android_native_rect_t mPreviewCrop; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 441 | |
| 442 | struct BufferHasher { |
| 443 | size_t operator()(const buffer_handle_t& buf) const { |
| 444 | if (buf == nullptr) |
| 445 | return 0; |
| 446 | |
| 447 | size_t result = 1; |
| 448 | result = 31 * result + buf->numFds; |
| 449 | result = 31 * result + buf->numInts; |
| 450 | int length = buf->numFds + buf->numInts; |
| 451 | for (int i = 0; i < length; i++) { |
| 452 | result = 31 * result + buf->data[i]; |
| 453 | } |
| 454 | return result; |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | struct BufferComparator { |
| 459 | bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const { |
| 460 | if (buf1->numFds == buf2->numFds && buf1->numInts == buf2->numInts) { |
| 461 | int length = buf1->numFds + buf1->numInts; |
| 462 | for (int i = 0; i < length; i++) { |
| 463 | if (buf1->data[i] != buf2->data[i]) { |
| 464 | return false; |
| 465 | } |
| 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | }; |
| 472 | |
| 473 | std::mutex mBufferIdMapLock; // protecting mBufferIdMap and mNextBufferId |
| 474 | typedef std::unordered_map<const buffer_handle_t, uint64_t, |
| 475 | BufferHasher, BufferComparator> BufferIdMap; |
| 476 | // stream ID -> per stream buffer ID map |
| 477 | BufferIdMap mBufferIdMap; |
| 478 | std::unordered_map<uint64_t, ANativeWindowBuffer*> mReversedBufMap; |
| 479 | uint64_t mNextBufferId = 1; |
| 480 | static const uint64_t BUFFER_ID_NO_BUFFER = 0; |
| 481 | |
Yin-Chia Yeh | b2a6561 | 2017-09-07 16:30:46 -0700 | [diff] [blame] | 482 | std::mutex mHidlMemPoolMapLock; // protecting mHidlMemPoolMap |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 483 | std::unordered_map<int, camera_memory_t*> mHidlMemPoolMap; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | }; // namespace android |
| 487 | |
| 488 | #endif |