| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 | #include <hardware/camera.h> | 
|  | 17 |  | 
|  | 18 | #include <hidl/AidlCameraDeviceCallbacks.h> | 
| Jayant Chowdhary | 2f68652 | 2022-05-09 14:52:16 +0000 | [diff] [blame] | 19 | #include <hidl/Utils.h> | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | namespace android { | 
|  | 22 | namespace frameworks { | 
|  | 23 | namespace cameraservice { | 
|  | 24 | namespace device { | 
|  | 25 | namespace V2_0 { | 
|  | 26 | namespace implementation { | 
|  | 27 |  | 
|  | 28 | using hardware::hidl_vec; | 
|  | 29 | using HCaptureResultExtras = android::frameworks::cameraservice::device::V2_0::CaptureResultExtras; | 
|  | 30 | using HPhysicalCaptureResultInfo = android::frameworks::cameraservice::device::V2_0::PhysicalCaptureResultInfo; | 
|  | 31 | using HCameraMetadata = android::frameworks::cameraservice::device::V2_0::CameraMetadata; | 
|  | 32 |  | 
|  | 33 | const char *H2BCameraDeviceCallbacks::kResultKey = "CaptureResult"; | 
|  | 34 |  | 
|  | 35 | H2BCameraDeviceCallbacks::H2BCameraDeviceCallbacks(const sp<HalInterface>& base) : CBase(base) { } | 
|  | 36 |  | 
| Jayant Chowdhary | 2f68652 | 2022-05-09 14:52:16 +0000 | [diff] [blame] | 37 | bool H2BCameraDeviceCallbacks::initializeLooper(int vndkVersion) { | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 38 | mCbLooper = new ALooper; | 
|  | 39 | mCbLooper->setName("cs-looper"); | 
|  | 40 | status_t err = mCbLooper->start(/*runOnCallingThread*/ false, /*canCallJava*/ false, | 
|  | 41 | PRIORITY_DEFAULT); | 
|  | 42 | if (err !=OK) { | 
|  | 43 | ALOGE("Unable to start camera device callback looper"); | 
|  | 44 | return false; | 
|  | 45 | } | 
| Jayant Chowdhary | 2f68652 | 2022-05-09 14:52:16 +0000 | [diff] [blame] | 46 | mHandler = new CallbackHandler(this, vndkVersion); | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 47 | mCbLooper->registerHandler(mHandler); | 
|  | 48 | return true; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | H2BCameraDeviceCallbacks::~H2BCameraDeviceCallbacks() { | 
|  | 52 | if (mCbLooper != nullptr) { | 
|  | 53 | if (mHandler != nullptr) { | 
|  | 54 | mCbLooper->unregisterHandler(mHandler->id()); | 
|  | 55 | } | 
|  | 56 | mCbLooper->stop(); | 
|  | 57 | } | 
|  | 58 | mCbLooper.clear(); | 
|  | 59 | mHandler.clear(); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | binder::Status H2BCameraDeviceCallbacks::onDeviceError( | 
|  | 63 | int32_t errorCode, const CaptureResultExtras& resultExtras) { | 
|  | 64 | using hardware::cameraservice::utils::conversion::convertToHidl; | 
|  | 65 | HCaptureResultExtras hCaptureResultExtras = convertToHidl(resultExtras); | 
|  | 66 | auto ret = mBase->onDeviceError(convertToHidl(errorCode), hCaptureResultExtras); | 
|  | 67 | if (!ret.isOk()) { | 
|  | 68 | ALOGE("%s OnDeviceError callback failed due to %s",__FUNCTION__, | 
|  | 69 | ret.description().c_str()); | 
|  | 70 | } | 
|  | 71 | return binder::Status::ok(); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | binder::Status H2BCameraDeviceCallbacks::onDeviceIdle() { | 
|  | 75 | auto ret = mBase->onDeviceIdle(); | 
|  | 76 | if (!ret.isOk()) { | 
|  | 77 | ALOGE("%s OnDeviceIdle callback failed due to %s",__FUNCTION__, | 
|  | 78 | ret.description().c_str()); | 
|  | 79 | } | 
|  | 80 | return binder::Status::ok(); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | binder::Status H2BCameraDeviceCallbacks::onCaptureStarted( | 
|  | 84 | const CaptureResultExtras& resultExtras, int64_t timestamp) { | 
|  | 85 | using hardware::cameraservice::utils::conversion::convertToHidl; | 
|  | 86 | HCaptureResultExtras hCaptureResultExtras = convertToHidl(resultExtras); | 
|  | 87 | auto ret = mBase->onCaptureStarted(hCaptureResultExtras, timestamp); | 
|  | 88 | if (!ret.isOk()) { | 
|  | 89 | ALOGE("%s OnCaptureCallback failed due to %s",__FUNCTION__, | 
|  | 90 | ret.description().c_str()); | 
|  | 91 | } | 
|  | 92 | return binder::Status::ok(); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | void H2BCameraDeviceCallbacks::convertResultMetadataToHidl(const camera_metadata_t *rawMetadata, | 
|  | 96 | FmqSizeOrMetadata *hResultMetadata) { | 
|  | 97 | // First try writing to fmq. | 
|  | 98 | size_t metadata_size = get_camera_metadata_size(rawMetadata); | 
|  | 99 | if ((metadata_size > 0) && | 
|  | 100 | (mCaptureResultMetadataQueue->availableToWrite() > 0)) { | 
|  | 101 | if (mCaptureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) { | 
|  | 102 | hResultMetadata->fmqMetadataSize(metadata_size); | 
|  | 103 | } else { | 
|  | 104 | ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__); | 
|  | 105 | HCameraMetadata metadata; | 
|  | 106 | hardware::cameraservice::utils::conversion::convertToHidl(rawMetadata, &metadata); | 
|  | 107 | hResultMetadata->metadata(std::move(metadata)); | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | void H2BCameraDeviceCallbacks::CallbackHandler::onMessageReceived(const sp<AMessage> &msg) { | 
|  | 113 | sp<RefBase> obj = nullptr; | 
|  | 114 | sp<ResultWrapper> resultWrapper = nullptr; | 
|  | 115 | bool found = false; | 
|  | 116 | switch (msg->what()) { | 
|  | 117 | case kWhatResultReceived: | 
|  | 118 | found = msg->findObject(kResultKey, &obj); | 
|  | 119 | if (!found || obj == nullptr) { | 
|  | 120 | ALOGE("Cannot find result object in callback message"); | 
|  | 121 | return; | 
|  | 122 | } | 
|  | 123 | resultWrapper = static_cast<ResultWrapper *>(obj.get()); | 
|  | 124 | processResultMessage(resultWrapper); | 
|  | 125 | break; | 
|  | 126 | default: | 
|  | 127 | ALOGE("Unknown callback sent"); | 
|  | 128 | break; | 
|  | 129 | } | 
|  | 130 | return; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | void H2BCameraDeviceCallbacks::CallbackHandler::processResultMessage( | 
|  | 134 | sp<ResultWrapper> &resultWrapper) { | 
|  | 135 | sp<H2BCameraDeviceCallbacks> converter = mConverter.promote(); | 
|  | 136 | if (converter == nullptr) { | 
|  | 137 | ALOGE("Callback wrapper has died, result callback cannot be made"); | 
|  | 138 | return; | 
|  | 139 | } | 
|  | 140 | CameraMetadataNative &result = resultWrapper->mResult; | 
|  | 141 | auto resultExtras = resultWrapper->mResultExtras; | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 142 | HCaptureResultExtras hResultExtras = | 
|  | 143 | hardware::cameraservice::utils::conversion::convertToHidl(resultExtras); | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 144 |  | 
|  | 145 | // Convert Metadata into HCameraMetadata; | 
|  | 146 | FmqSizeOrMetadata hResult; | 
| Jayant Chowdhary | 2f68652 | 2022-05-09 14:52:16 +0000 | [diff] [blame] | 147 | using hardware::cameraservice::utils::conversion::filterVndkKeys; | 
|  | 148 | if (filterVndkKeys(mVndkVersion, result, /*isStatic*/false) != OK) { | 
|  | 149 | ALOGE("%s: filtering vndk keys from result failed, not sending onResultReceived callback", | 
|  | 150 | __FUNCTION__); | 
|  | 151 | return; | 
|  | 152 | } | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 153 | const camera_metadata_t *rawMetadata = result.getAndLock(); | 
|  | 154 | converter->convertResultMetadataToHidl(rawMetadata, &hResult); | 
|  | 155 | result.unlock(rawMetadata); | 
| Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 156 | auto &physicalCaptureResultInfos = resultWrapper->mPhysicalCaptureResultInfos; | 
|  | 157 | hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos = | 
|  | 158 | hardware::cameraservice::utils::conversion::convertToHidl( | 
|  | 159 | physicalCaptureResultInfos, converter->mCaptureResultMetadataQueue); | 
| Jayant Chowdhary | 0c94727 | 2018-08-15 14:42:04 -0700 | [diff] [blame] | 160 | auto ret = converter->mBase->onResultReceived(hResult, hResultExtras, | 
|  | 161 | hPhysicalCaptureResultInfos); | 
|  | 162 | if (!ret.isOk()) { | 
|  | 163 | ALOGE("%s OnResultReceived callback failed due to %s",__FUNCTION__, | 
|  | 164 | ret.description().c_str()); | 
|  | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | binder::Status H2BCameraDeviceCallbacks::onResultReceived( | 
|  | 169 | const CameraMetadataNative& result, | 
|  | 170 | const CaptureResultExtras& resultExtras, | 
|  | 171 | const ::std::vector<PhysicalCaptureResultInfo>& physicalCaptureResultInfos) { | 
|  | 172 | // Wrap CameraMetadata, resultExtras and physicalCaptureResultInfos in on | 
|  | 173 | // sp<RefBase>-able structure and post it. | 
|  | 174 | sp<ResultWrapper> resultWrapper = new ResultWrapper(const_cast<CameraMetadataNative &>(result), | 
|  | 175 | resultExtras, physicalCaptureResultInfos); | 
|  | 176 | sp<AMessage> msg = new AMessage(kWhatResultReceived, mHandler); | 
|  | 177 | msg->setObject(kResultKey, resultWrapper); | 
|  | 178 | msg->post(); | 
|  | 179 | return binder::Status::ok(); | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | binder::Status H2BCameraDeviceCallbacks::onPrepared(int32_t streamId) { | 
|  | 183 | // not implemented | 
|  | 184 | // To silence Wunused-parameter. | 
|  | 185 | (void) streamId; | 
|  | 186 | return binder::Status::ok(); | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | binder::Status H2BCameraDeviceCallbacks::onRepeatingRequestError( | 
|  | 190 | int64_t lastFrameNumber, | 
|  | 191 | int32_t repeatingRequestId) { | 
|  | 192 | auto ret = | 
|  | 193 | mBase->onRepeatingRequestError(lastFrameNumber, repeatingRequestId); | 
|  | 194 | if (!ret.isOk()) { | 
|  | 195 | ALOGE("%s OnRepeatingRequestEror callback failed due to %s",__FUNCTION__, | 
|  | 196 | ret.description().c_str()); | 
|  | 197 | } | 
|  | 198 | return binder::Status::ok(); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | binder::Status H2BCameraDeviceCallbacks::onRequestQueueEmpty() { | 
|  | 202 | // not implemented | 
|  | 203 | return binder::Status::ok(); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | } // implementation | 
|  | 207 | } // V2_0 | 
|  | 208 | } // device | 
|  | 209 | } // cameraservice | 
|  | 210 | } // frameworks | 
|  | 211 | } // android |