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): |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 93 | mDevice(nullptr), |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 94 | mHidlDevice(nullptr), |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 95 | 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 104 | { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 107 | ~CameraHardwareInterface(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 108 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 109 | status_t initialize(sp<CameraProviderManager> manager); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 110 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 111 | /** Set the ANativeWindow to which preview frames are sent */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 112 | status_t setPreviewWindow(const sp<ANativeWindow>& buf); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 113 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 114 | status_t setPreviewScalingMode(int scalingMode); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 115 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 116 | status_t setPreviewTransform(int transform); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 117 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 118 | /** 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 Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 122 | data_callback_timestamp_batch data_cb_timestamp_batch, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 123 | void* user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 124 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 134 | void enableMsgType(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 135 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 146 | void disableMsgType(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 147 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 153 | int msgTypeEnabled(int32_t msgType); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Start preview mode. |
| 157 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 158 | status_t startPreview(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Stop a previously started preview. |
| 162 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 163 | void stopPreview(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Returns true if preview is enabled. |
| 167 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 168 | int previewEnabled(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 169 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 202 | status_t storeMetaDataInBuffers(int enable); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 203 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 213 | status_t startRecording(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Stop a previously started recording. |
| 217 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 218 | void stopRecording(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * Returns true if recording is enabled. |
| 222 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 223 | int recordingEnabled(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 224 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 235 | void releaseRecordingFrame(const sp<IMemory>& mem); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 238 | * 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 252 | * 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 256 | status_t autoFocus(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 257 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 264 | status_t cancelAutoFocus(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Take a picture. |
| 268 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 269 | status_t takePicture(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 270 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 275 | status_t cancelPicture(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Set the camera parameters. This returns BAD_VALUE if any parameter is |
| 279 | * invalid or not supported. */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 280 | status_t setParameters(const CameraParameters ¶ms); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 281 | |
| 282 | /** Return the camera parameters. */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 283 | CameraParameters getParameters() const; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Send command to camera driver. |
| 287 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 288 | status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * Release the hardware resources owned by this object. Note that this is |
| 292 | * *not* done in the destructor. |
| 293 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 294 | void release(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Dump state of the camera hardware |
| 298 | */ |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 299 | status_t dump(int fd, const Vector<String16>& /*args*/) const; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 300 | |
| 301 | private: |
| 302 | camera_device_t *mDevice; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 303 | sp<hardware::camera::device::V1_0::ICameraDevice> mHidlDevice; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 304 | String8 mName; |
| 305 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 306 | static void sNotifyCb(int32_t msg_type, int32_t ext1, |
| 307 | int32_t ext2, void *user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 308 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 309 | static void sDataCb(int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 310 | const camera_memory_t *data, unsigned int index, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 311 | camera_frame_metadata_t *metadata, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 312 | void *user); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 313 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 314 | static void sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 315 | const camera_memory_t *data, unsigned index, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 316 | void *user); |
| 317 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 318 | // 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 Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 321 | class CameraHeapMemory : public RefBase { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 322 | public: |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 323 | CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) : |
| 324 | mBufSize(buf_size), |
| 325 | mNumBufs(num_buffers) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 326 | { |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 327 | mHeap = new MemoryHeapBase(fd, buf_size * num_buffers); |
| 328 | commonInitialization(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 331 | explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) : |
| 332 | mBufSize(buf_size), |
| 333 | mNumBufs(num_buffers) |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 334 | { |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 351 | handle.release = sPutMemory; |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 352 | } |
| 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 364 | camera_memory_t handle; |
| 365 | }; |
| 366 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 367 | static camera_memory_t* sGetMemory(int fd, size_t buf_size, uint_t num_bufs, |
| 368 | void *user __attribute__((unused))); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 369 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 370 | static void sPutMemory(camera_memory_t *data); |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 371 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 372 | static ANativeWindow *sToAnw(void *user); |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 373 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 374 | static int sDequeueBuffer(struct preview_stream_ops* w, |
| 375 | buffer_handle_t** buffer, int *stride); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 376 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 377 | static int sLockBuffer(struct preview_stream_ops* w, |
| 378 | buffer_handle_t* /*buffer*/); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 379 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 380 | static int sEnqueueBuffer(struct preview_stream_ops* w, |
| 381 | buffer_handle_t* buffer); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 382 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 383 | static int sCancelBuffer(struct preview_stream_ops* w, |
| 384 | buffer_handle_t* buffer); |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 385 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 386 | static int sSetBufferCount(struct preview_stream_ops* w, int count); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 387 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 388 | static int sSetBuffersGeometry(struct preview_stream_ops* w, |
| 389 | int width, int height, int format); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 390 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 391 | static int sSetCrop(struct preview_stream_ops *w, |
| 392 | int left, int top, int right, int bottom); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 393 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 394 | static int sSetTimestamp(struct preview_stream_ops *w, |
| 395 | int64_t timestamp); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 396 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 397 | static int sSetUsage(struct preview_stream_ops* w, int usage); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 398 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 399 | static int sSetSwapInterval(struct preview_stream_ops *w, int interval); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 400 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 401 | static int sGetMinUndequeuedBufferCount( |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 402 | const struct preview_stream_ops *w, |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 403 | int *count); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 404 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 405 | void initHalPreviewWindow(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 406 | |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 407 | 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 Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 431 | 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 435 | |
| 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 Wu | 67a0c0e | 2017-04-06 13:37:01 -0700 | [diff] [blame] | 452 | setUsage(hardware::graphics::common::V1_0::BufferUsage usage) override; |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 453 | 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 Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 459 | |
| 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 Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 469 | notify_callback mNotifyCb; |
| 470 | data_callback mDataCb; |
| 471 | data_callback_timestamp mDataCbTimestamp; |
| 472 | data_callback_timestamp_batch mDataCbTimestampBatch; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 473 | void *mCbUser; |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 474 | |
| 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 Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 485 | |
| 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 Yeh | b2a6561 | 2017-09-07 16:30:46 -0700 | [diff] [blame^] | 526 | std::mutex mHidlMemPoolMapLock; // protecting mHidlMemPoolMap |
Yin-Chia Yeh | 4717c47 | 2017-02-13 10:56:40 -0800 | [diff] [blame] | 527 | std::unordered_map<int, camera_memory_t*> mHidlMemPoolMap; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 528 | }; |
| 529 | |
| 530 | }; // namespace android |
| 531 | |
| 532 | #endif |