14717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/*
24717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Copyright (C) 2017 The Android Open Source Project
34717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh *
44717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Licensed under the Apache License, Version 2.0 (the "License");
54717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * you may not use this file except in compliance with the License.
64717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * You may obtain a copy of the License at
74717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh *
84717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh *      http://www.apache.org/licenses/LICENSE-2.0
94717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh *
104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Unless required by applicable law or agreed to in writing, software
114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * distributed under the License is distributed on an "AS IS" BASIS,
124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * See the License for the specific language governing permissions and
144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * limitations under the License.
154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#define LOG_TAG "CameraHardwareInterface"
174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh//#define LOG_NDEBUG 0
184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#include <inttypes.h>
20067606686b9f8bf78580f07dbde78989b755dcbcYin-Chia Yeh#include <media/hardware/HardwareAPI.h> // For VideoNativeHandleMetadata
214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#include "CameraHardwareInterface.h"
224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
234717c470bf989e02f798857358471f8feb77660fYin-Chia Yehnamespace android {
244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
254717c470bf989e02f798857358471f8feb77660fYin-Chia Yehusing namespace hardware::camera::device::V1_0;
264717c470bf989e02f798857358471f8feb77660fYin-Chia Yehusing namespace hardware::camera::common::V1_0;
274717c470bf989e02f798857358471f8feb77660fYin-Chia Yehusing hardware::hidl_handle;
284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
294717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::~CameraHardwareInterface()
304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGI("Destroying camera %s", mName.string());
324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mDevice) {
334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        int rc = mDevice->common.close(&mDevice->common);
344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (rc != OK)
354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            ALOGE("Could not close camera %s: %d", mName.string(), rc);
364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mHidlDevice != nullptr) {
384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->close();
394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice.clear();
404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        cleanupCirculatingBuffers();
414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
444717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::initialize(sp<CameraProviderManager> manager) {
454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mDevice) {
464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: camera hardware interface has been initialized to libhardware path!",
474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                __FUNCTION__);
484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return INVALID_OPERATION;
494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGI("Opening camera %s", mName.string());
524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
535ff9c91a90d7d5fad49de38bc314910251b25774Steven Moreland    status_t ret = manager->openSession(mName.string(), this, &mHidlDevice);
544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (ret != OK) {
554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: openSession failed! %s (%d)", __FUNCTION__, strerror(-ret), ret);
564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return ret;
584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
604717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::setPreviewScalingMode(int scalingMode)
614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = OK;
634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewScalingMode = scalingMode;
644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mPreviewWindow != nullptr) {
654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        rc = native_window_set_scaling_mode(mPreviewWindow.get(),
664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                scalingMode);
674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
684717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return rc;
694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
714717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::setPreviewTransform(int transform) {
724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = OK;
734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewTransform = transform;
744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mPreviewWindow != nullptr) {
754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        rc = native_window_set_buffers_transform(mPreviewWindow.get(),
764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mPreviewTransform);
774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return rc;
794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Implementation of android::hardware::camera::device::V1_0::ICameraDeviceCallback
834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
844717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void> CameraHardwareInterface::notifyCallback(
854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        NotifyCallbackMsg msgType, int32_t ext1, int32_t ext2) {
864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sNotifyCb((int32_t) msgType, ext1, ext2, (void*) this);
874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
904717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<uint32_t> CameraHardwareInterface::registerMemory(
914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        const hardware::hidl_handle& descriptor,
924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        uint32_t bufferSize, uint32_t bufferCount) {
934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (descriptor->numFds != 1) {
944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: camera memory descriptor has numFds %d (expect 1)",
954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                __FUNCTION__, descriptor->numFds);
964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return 0;
974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (descriptor->data[0] < 0) {
994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: camera memory descriptor has FD %d (expect >= 0)",
1004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                __FUNCTION__, descriptor->data[0]);
1014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return 0;
1024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
1044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    camera_memory_t* mem = sGetMemory(descriptor->data[0], bufferSize, bufferCount, this);
1054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sp<CameraHeapMemory> camMem(static_cast<CameraHeapMemory *>(mem->handle));
1064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int memPoolId = camMem->mHeap->getHeapID();
1074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (memPoolId < 0) {
1084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: CameraHeapMemory has FD %d (expect >= 0)", __FUNCTION__, memPoolId);
1094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return 0;
1104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHidlMemPoolMap.insert(std::make_pair(memPoolId, mem));
1124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return memPoolId;
1134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
1144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
1154717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void> CameraHardwareInterface::unregisterMemory(uint32_t memId) {
1164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mHidlMemPoolMap.count(memId) == 0) {
1174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: memory pool ID %d not found", __FUNCTION__, memId);
1184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
1194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    camera_memory_t* mem = mHidlMemPoolMap.at(memId);
1214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sPutMemory(mem);
1224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHidlMemPoolMap.erase(memId);
1234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
1244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
1254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
1264717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void> CameraHardwareInterface::dataCallback(
1274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        DataCallbackMsg msgType, uint32_t data, uint32_t bufferIndex,
1284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        const hardware::camera::device::V1_0::CameraFrameMetadata& metadata) {
1294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mHidlMemPoolMap.count(data) == 0) {
1304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data);
1314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
1324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    camera_frame_metadata_t md;
1344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    md.number_of_faces = metadata.faces.size();
1354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    md.faces = (camera_face_t*) metadata.faces.data();
1364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sDataCb((int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, &md, this);
1374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
1384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
1394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
1404717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void> CameraHardwareInterface::dataCallbackTimestamp(
1414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        DataCallbackMsg msgType, uint32_t data,
1424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        uint32_t bufferIndex, int64_t timestamp) {
1434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mHidlMemPoolMap.count(data) == 0) {
1444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data);
1454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
1464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sDataCbTimestamp(timestamp, (int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, this);
1484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
1494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
1504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
1514717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void> CameraHardwareInterface::handleCallbackTimestamp(
1524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        DataCallbackMsg msgType, const hidl_handle& frameData, uint32_t data,
1534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        uint32_t bufferIndex, int64_t timestamp) {
1544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mHidlMemPoolMap.count(data) == 0) {
1554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: memory pool ID %d not found", __FUNCTION__, data);
1564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
1574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
1584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(mHidlMemPoolMap.at(data)->handle));
1594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*)
1604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mem->mBuffers[bufferIndex]->pointer();
1614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    md->pHandle = const_cast<native_handle_t*>(frameData.getNativeHandle());
1624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sDataCbTimestamp(timestamp, (int32_t) msgType, mHidlMemPoolMap.at(data), bufferIndex, this);
1634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
1644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
1654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
166b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yehhardware::Return<void> CameraHardwareInterface::handleCallbackTimestampBatch(
167b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        DataCallbackMsg msgType,
168b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        const hardware::hidl_vec<hardware::camera::device::V1_0::HandleTimestampMessage>& messages) {
169b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    std::vector<android::HandleTimestampMessage> msgs;
170b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    msgs.reserve(messages.size());
171b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
172b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    for (const auto& hidl_msg : messages) {
173b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        if (mHidlMemPoolMap.count(hidl_msg.data) == 0) {
174b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            ALOGE("%s: memory pool ID %d not found", __FUNCTION__, hidl_msg.data);
175b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            return hardware::Void();
176b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        }
177b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        sp<CameraHeapMemory> mem(
178b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                static_cast<CameraHeapMemory *>(mHidlMemPoolMap.at(hidl_msg.data)->handle));
179b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
180b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        if (hidl_msg.bufferIndex >= mem->mNumBufs) {
181b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
182b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                 hidl_msg.bufferIndex, mem->mNumBufs);
183b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            return hardware::Void();
184b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        }
185b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*)
186b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                mem->mBuffers[hidl_msg.bufferIndex]->pointer();
187b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        md->pHandle = const_cast<native_handle_t*>(hidl_msg.frameData.getNativeHandle());
188b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
189b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        msgs.push_back({hidl_msg.timestamp, mem->mBuffers[hidl_msg.bufferIndex]});
190b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    }
191b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
192b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    mDataCbTimestampBatch((int32_t) msgType, msgs, mCbUser);
193b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    return hardware::Void();
194b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh}
195b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
1964717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstd::pair<bool, uint64_t> CameraHardwareInterface::getBufferId(
1974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ANativeWindowBuffer* anb) {
1984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    std::lock_guard<std::mutex> lock(mBufferIdMapLock);
1994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    buffer_handle_t& buf = anb->handle;
2014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    auto it = mBufferIdMap.find(buf);
2024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (it == mBufferIdMap.end()) {
2034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        uint64_t bufId = mNextBufferId++;
2044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mBufferIdMap[buf] = bufId;
2054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mReversedBufMap[bufId] = anb;
2064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return std::make_pair(true, bufId);
2074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else {
2084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return std::make_pair(false, it->second);
2094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
2114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2124717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::cleanupCirculatingBuffers() {
2134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    std::lock_guard<std::mutex> lock(mBufferIdMapLock);
2144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mBufferIdMap.clear();
2154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mReversedBufMap.clear();
2164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
2174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2184717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void>
2194717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::dequeueBuffer(dequeueBuffer_cb _hidl_cb) {
2204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
2214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
2224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
2234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
2244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindowBuffer* anb;
2264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_dequeue_buffer_and_wait(a, &anb);
2274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
2284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    uint64_t bufferId = 0;
2294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    uint32_t stride = 0;
2304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hidl_handle buf = nullptr;
2314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
2324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
2334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        auto pair = getBufferId(anb);
2344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        buf = (pair.first) ? anb->handle : nullptr;
2354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        bufferId = pair.second;
2364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        stride = anb->stride;
2374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    _hidl_cb(s, bufferId, buf, stride);
2404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
2414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
2424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2434717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
2444717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::enqueueBuffer(uint64_t bufferId) {
2454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
2464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
2474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
2484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::INTERNAL_ERROR;
2494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mReversedBufMap.count(bufferId) == 0) {
2514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: bufferId %" PRIu64 " not found", __FUNCTION__, bufferId);
2524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::ILLEGAL_ARGUMENT;
2534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = a->queueBuffer(a, mReversedBufMap.at(bufferId), -1);
2554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == 0) {
2564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::OK;
2574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return Status::INTERNAL_ERROR;
2594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
2604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2614717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
2624717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::cancelBuffer(uint64_t bufferId) {
2634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
2644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
2654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
2664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::INTERNAL_ERROR;
2674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2684717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mReversedBufMap.count(bufferId) == 0) {
2694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: bufferId %" PRIu64 " not found", __FUNCTION__, bufferId);
2704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::ILLEGAL_ARGUMENT;
2714717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = a->cancelBuffer(a, mReversedBufMap.at(bufferId), -1);
2734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == 0) {
2744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::OK;
2754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
2764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return Status::INTERNAL_ERROR;
2774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
2784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
2794717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
2804717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::setBufferCount(uint32_t count) {
2814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
2824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a != nullptr) {
2834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Workaround for b/27039775
2844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Previously, setting the buffer count would reset the buffer
2854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // queue's flag that allows for all buffers to be dequeued on the
2864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // producer side, instead of just the producer's declared max count,
2874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // if no filled buffers have yet been queued by the producer.  This
2884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // reset no longer happens, but some HALs depend on this behavior,
2894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // so it needs to be maintained for HAL backwards compatibility.
2904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Simulate the prior behavior by disconnecting/reconnecting to the
2914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // window and setting the values again.  This has the drawback of
2924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // actually causing memory reallocation, which may not have happened
2934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // in the past.
2944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA);
2954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA);
2964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewScalingMode != NOT_SET) {
2974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_scaling_mode(a, mPreviewScalingMode);
2984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
2994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewTransform != NOT_SET) {
3004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_transform(a, mPreviewTransform);
3014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
3024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewWidth != NOT_SET) {
3034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_dimensions(a,
3044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    mPreviewWidth, mPreviewHeight);
3054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_format(a, mPreviewFormat);
3064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
3074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewUsage != 0) {
3084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_usage(a, mPreviewUsage);
3094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
3104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewSwapInterval != NOT_SET) {
3114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            a->setSwapInterval(a, mPreviewSwapInterval);
3124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
3134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mPreviewCrop.left != NOT_SET) {
3144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_crop(a, &(mPreviewCrop));
3154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
3164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_set_buffer_count(a, count);
3184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        cleanupCirculatingBuffers();
3204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return Status::OK;
3214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return Status::INTERNAL_ERROR;
3234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
3244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
3254717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
3264717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::setBuffersGeometry(
3274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        uint32_t w, uint32_t h, hardware::graphics::common::V1_0::PixelFormat format) {
3284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
3294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
3304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
3314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
3324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return s;
3334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewWidth = w;
3354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewHeight = h;
3364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewFormat = (int) format;
3374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_set_buffers_dimensions(a, w, h);
3384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        rc = native_window_set_buffers_format(a, mPreviewFormat);
3404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        cleanupCirculatingBuffers();
3434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
3444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return s;
3464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
3474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
3484717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
3494717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::setCrop(int32_t left, int32_t top, int32_t right, int32_t bottom) {
3504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
3514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
3524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
3534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
3544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return s;
3554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewCrop.left = left;
3574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewCrop.top = top;
3584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewCrop.right = right;
3594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewCrop.bottom = bottom;
3604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_set_crop(a, &mPreviewCrop);
3614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
3634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return s;
3654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
3664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
3674717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
36867a0c0e77dce4dccc9155f4bfce490d167457c8aChia-I WuCameraHardwareInterface::setUsage(hardware::graphics::common::V1_0::BufferUsage usage) {
3694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
3704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
3714717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
3724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
3734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return s;
3744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
37547cf8e62b81770c0896aac444ef22a840b3a2c5eYin-Chia Yeh    mPreviewUsage = (int) usage;
3764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_set_usage(a, mPreviewUsage);
3774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        cleanupCirculatingBuffers();
3794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
3804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return s;
3824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
3834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
3844717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
3854717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::setSwapInterval(int32_t interval) {
3864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
3874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
3884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
3894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
3904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return s;
3914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mPreviewSwapInterval = interval;
3934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = a->setSwapInterval(a, interval);
3944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
3954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
3964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
3974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return s;
3984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
3994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4004717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<void>
4014717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::getMinUndequeuedBufferCount(getMinUndequeuedBufferCount_cb _hidl_cb) {
4024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
4034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
4044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
4054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return hardware::Void();
4064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int count = 0;
4084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &count);
4094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
4104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
4114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
4124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    _hidl_cb(s, count);
4144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return hardware::Void();
4154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
4164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4174717c470bf989e02f798857358471f8feb77660fYin-Chia Yehhardware::Return<Status>
4184717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraHardwareInterface::setTimestamp(int64_t timestamp) {
4194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    Status s = Status::INTERNAL_ERROR;
4204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = mPreviewWindow.get();
4214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a == nullptr) {
4224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: preview window is null", __FUNCTION__);
4234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return s;
4244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc = native_window_set_buffers_timestamp(a, timestamp);
4264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
4274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        s = Status::OK;
4284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return s;
4304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
4314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4324717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::setPreviewWindow(const sp<ANativeWindow>& buf)
4334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
4344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
4354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
4364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mPreviewWindow = buf;
4374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (buf != nullptr) {
4384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            if (mPreviewScalingMode != NOT_SET) {
4394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                setPreviewScalingMode(mPreviewScalingMode);
4404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            }
4414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            if (mPreviewTransform != NOT_SET) {
4424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                setPreviewTransform(mPreviewTransform);
4434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            }
4444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
4454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
4464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->setPreviewWindow(buf.get() ? this : nullptr));
4474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice) {
4484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mDevice->ops->set_preview_window) {
4494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mPreviewWindow = buf;
4504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            if (buf != nullptr) {
4514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                if (mPreviewScalingMode != NOT_SET) {
4524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    setPreviewScalingMode(mPreviewScalingMode);
4534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                }
4544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                if (mPreviewTransform != NOT_SET) {
4554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    setPreviewTransform(mPreviewTransform);
4564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                }
4574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            }
4584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mHalPreviewWindow.user = this;
4594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p",__FUNCTION__,
4604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    &mHalPreviewWindow, mHalPreviewWindow.user);
4614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            return mDevice->ops->set_preview_window(mDevice,
4624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    buf.get() ? &mHalPreviewWindow.nw : 0);
4634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
4644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
4664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
4674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4684717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::setCallbacks(notify_callback notify_cb,
4694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        data_callback data_cb,
4704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        data_callback_timestamp data_cb_timestamp,
471b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        data_callback_timestamp_batch data_cb_timestamp_batch,
4724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        void* user)
4734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
4744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mNotifyCb = notify_cb;
4754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mDataCb = data_cb;
4764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mDataCbTimestamp = data_cb_timestamp;
477b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    mDataCbTimestampBatch = data_cb_timestamp_batch;
4784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mCbUser = user;
4794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
4814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (mDevice && mDevice->ops->set_callbacks) {
4834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->set_callbacks(mDevice,
4844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                               sNotifyCb,
4854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                               sDataCb,
4864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                               sDataCbTimestamp,
4874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                               sGetMemory,
4884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                               this);
4894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
4904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
4914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
4924717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::enableMsgType(int32_t msgType)
4934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
4944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
4954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
4964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->enableMsgType(msgType);
4974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->enable_msg_type) {
4984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->enable_msg_type(mDevice, msgType);
4994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5024717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::disableMsgType(int32_t msgType)
5034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->disableMsgType(msgType);
5074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->disable_msg_type) {
5084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->disable_msg_type(mDevice, msgType);
5094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5124717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::msgTypeEnabled(int32_t msgType)
5134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mHidlDevice->msgTypeEnabled(msgType);
5174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->msg_type_enabled) {
5184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->msg_type_enabled(mDevice, msgType);
5194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return false;
5214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5234717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::startPreview()
5244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
5284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->startPreview());
5294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->start_preview) {
5304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->start_preview(mDevice);
5314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
5334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5354717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::stopPreview()
5364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->stopPreview();
5404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->stop_preview) {
5414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->stop_preview(mDevice);
5424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5454717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::previewEnabled()
5464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mHidlDevice->previewEnabled();
5504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->preview_enabled) {
5514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->preview_enabled(mDevice);
5524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return false;
5544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5564717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::storeMetaDataInBuffers(int enable)
5574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
5614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->storeMetaDataInBuffers(enable));
5624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->store_meta_data_in_buffers) {
5634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
5644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return enable ? INVALID_OPERATION: OK;
5664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5684717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::startRecording()
5694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5714717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
5734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->startRecording());
5744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->start_recording) {
5754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->start_recording(mDevice);
5764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
5784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
5814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Stop a previously started recording.
5824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
5834717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::stopRecording()
5844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
5874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->stopRecording();
5884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->stop_recording) {
5894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->stop_recording(mDevice);
5904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
5914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
5924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
5934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
5944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Returns true if recording is enabled.
5954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
5964717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::recordingEnabled()
5974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
5984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
5994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
6004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mHidlDevice->recordingEnabled();
6014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->recording_enabled) {
6024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->recording_enabled(mDevice);
6034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
6044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return false;
6054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
6064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
6074717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::releaseRecordingFrame(const sp<IMemory>& mem)
6084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
6094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
6104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ssize_t offset;
6114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    size_t size;
6124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
6134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int heapId = heap->getHeapID();
6144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int bufferIndex = offset / size;
6154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
6164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (size == sizeof(VideoNativeHandleMetadata)) {
6174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) mem->pointer();
6184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            // Caching the handle here because md->pHandle will be subject to HAL's edit
6194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_handle_t* nh = md->pHandle;
6204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            hidl_handle frame = nh;
6214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mHidlDevice->releaseRecordingFrameHandle(heapId, bufferIndex, frame);
6224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_handle_close(nh);
6234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_handle_delete(nh);
6244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        } else {
6254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mHidlDevice->releaseRecordingFrame(heapId, bufferIndex);
6264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
6274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->release_recording_frame) {
6284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        void *data = ((uint8_t *)heap->base()) + offset;
6294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->release_recording_frame(mDevice, data);
6304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
6314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
6324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
633b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yehvoid CameraHardwareInterface::releaseRecordingFrameBatch(const std::vector<sp<IMemory>>& frames)
634b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh{
635b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
636b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    size_t n = frames.size();
637b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    std::vector<VideoFrameMessage> msgs;
638b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    msgs.reserve(n);
639b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    for (auto& mem : frames) {
640b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        if (CC_LIKELY(mHidlDevice != nullptr)) {
641b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            ssize_t offset;
642b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            size_t size;
643b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
644b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            if (size == sizeof(VideoNativeHandleMetadata)) {
645b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                uint32_t heapId = heap->getHeapID();
646b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                uint32_t bufferIndex = offset / size;
647b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) mem->pointer();
648b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                // Caching the handle here because md->pHandle will be subject to HAL's edit
649b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                native_handle_t* nh = md->pHandle;
650b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                VideoFrameMessage msg;
651b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                msgs.push_back({nh, heapId, bufferIndex});
652b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            } else {
653b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                ALOGE("%s only supports VideoNativeHandleMetadata mode", __FUNCTION__);
654b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh                return;
655b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            }
656b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        } else {
657b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            ALOGE("Non HIDL mode do not support %s", __FUNCTION__);
658b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh            return;
659b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        }
660b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    }
661b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
662b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    mHidlDevice->releaseRecordingFrameHandleBatch(msgs);
663b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
664b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    for (auto& msg : msgs) {
665b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        native_handle_t* nh = const_cast<native_handle_t*>(msg.frameData.getNativeHandle());
666b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        native_handle_close(nh);
667b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh        native_handle_delete(nh);
668b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh    }
669b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh}
670b5df547bce900fa5764d735bee304e79c001d60dYin-Chia Yeh
6714717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::autoFocus()
6724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
6734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
6744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
6754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
6764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->autoFocus());
6774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->auto_focus) {
6784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->auto_focus(mDevice);
6794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
6804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
6814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
6824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
6834717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::cancelAutoFocus()
6844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
6854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
6864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
6874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
6884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->cancelAutoFocus());
6894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->cancel_auto_focus) {
6904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->cancel_auto_focus(mDevice);
6914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
6924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
6934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
6944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
6954717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::takePicture()
6964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
6974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
6984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
6994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
7004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->takePicture());
7014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->take_picture) {
7024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->take_picture(mDevice);
7034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
7054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7074717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::cancelPicture()
7084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
7094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
7124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->cancelPicture());
7134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->cancel_picture) {
7144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->cancel_picture(mDevice);
7154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
7174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7194717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::setParameters(const CameraParameters &params)
7204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
7214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
7244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->setParameters(params.flatten().string()));
7254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->set_parameters) {
7264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->set_parameters(mDevice, params.flatten().string());
7274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
7294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7314717c470bf989e02f798857358471f8feb77660fYin-Chia YehCameraParameters CameraHardwareInterface::getParameters() const
7324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
7334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraParameters parms;
7354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        hardware::hidl_string outParam;
7374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->getParameters(
7384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                [&outParam](const auto& outStr) {
7394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    outParam = outStr;
7404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                });
7414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        String8 tmp(outParam.c_str());
7424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        parms.unflatten(tmp);
7434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->get_parameters) {
7444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        char *temp = mDevice->ops->get_parameters(mDevice);
7454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        String8 str_parms(temp);
7464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (mDevice->ops->put_parameters)
7474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            mDevice->ops->put_parameters(mDevice, temp);
7484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        else
7494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            free(temp);
7504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        parms.unflatten(str_parms);
7514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return parms;
7534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7554717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
7564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
7574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(
7604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                mHidlDevice->sendCommand((CommandType) cmd, arg1, arg2));
7614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->send_command) {
7624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
7634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return INVALID_OPERATION;
7654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
7684717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Release the hardware resources owned by this object.  Note that this is
7694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * *not* done in the destructor.
7704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
7714717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::release() {
7724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice->close();
7754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mHidlDevice.clear();
7764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->release) {
7774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mDevice->ops->release(mDevice);
7784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
7824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Dump state of the camera hardware
7834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
7844717c470bf989e02f798857358471f8feb77660fYin-Chia Yehstatus_t CameraHardwareInterface::dump(int fd, const Vector<String16>& /*args*/) const
7854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
7864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s(%s)", __FUNCTION__, mName.string());
7874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (CC_LIKELY(mHidlDevice != nullptr)) {
7884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_handle_t* handle = native_handle_create(1,0);
7894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        handle->data[0] = fd;
7904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        Status s = mHidlDevice->dumpState(handle);
7914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_handle_delete(handle);
7924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return CameraProviderManager::mapToStatusT(s);
7934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else if (mDevice && mDevice->ops->dump) {
7944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return mDevice->ops->dump(mDevice, fd);
7954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
7964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return OK; // It's fine if the HAL doesn't implement dump()
7974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
7984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
7994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh/**
8004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh * Methods for legacy (non-HIDL) path follows
8014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh */
8024717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::sNotifyCb(int32_t msg_type, int32_t ext1,
8034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                        int32_t ext2, void *user)
8044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s", __FUNCTION__);
8064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *object =
8074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            static_cast<CameraHardwareInterface *>(user);
8084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    object->mNotifyCb(msg_type, ext1, ext2, object->mCbUser);
8094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8114717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::sDataCb(int32_t msg_type,
8124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                      const camera_memory_t *data, unsigned int index,
8134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                      camera_frame_metadata_t *metadata,
8144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                      void *user)
8154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s", __FUNCTION__);
8174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *object =
8184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            static_cast<CameraHardwareInterface *>(user);
8194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
8204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (index >= mem->mNumBufs) {
8214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
8224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh             index, mem->mNumBufs);
8234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return;
8244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
8254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    object->mDataCb(msg_type, mem->mBuffers[index], metadata, object->mCbUser);
8264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8284717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
8294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                         const camera_memory_t *data, unsigned index,
8304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                         void *user)
8314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ALOGV("%s", __FUNCTION__);
8334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *object =
8344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            static_cast<CameraHardwareInterface *>(user);
8354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    // Start refcounting the heap object from here on.  When the clients
8364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    // drop all references, it will be destroyed (as well as the enclosed
8374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    // MemoryHeapBase.
8384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
8394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (index >= mem->mNumBufs) {
8404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
8414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh             index, mem->mNumBufs);
8424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return;
8434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
8444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    object->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], object->mCbUser);
8454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8474717c470bf989e02f798857358471f8feb77660fYin-Chia Yehcamera_memory_t* CameraHardwareInterface::sGetMemory(
8484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        int fd, size_t buf_size, uint_t num_bufs,
8494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        void *user __attribute__((unused)))
8504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHeapMemory *mem;
8524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (fd < 0) {
8534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mem = new CameraHeapMemory(buf_size, num_bufs);
8544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    } else {
8554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        mem = new CameraHeapMemory(fd, buf_size, num_bufs);
8564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
8574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mem->incStrong(mem);
8584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return &mem->handle;
8594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8614717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::sPutMemory(camera_memory_t *data)
8624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (!data) {
8644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        return;
8654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
8664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
8684717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mem->decStrong(mem);
8694717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8714717c470bf989e02f798857358471f8feb77660fYin-Chia YehANativeWindow* CameraHardwareInterface::sToAnw(void *user)
8724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *object =
8744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            reinterpret_cast<CameraHardwareInterface *>(user);
8754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return object->mPreviewWindow.get();
8764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#define anw(n) sToAnw(((struct camera_preview_window *)(n))->user)
8784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\
8794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ((struct camera_preview_window *)(n))->user)
8804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8814717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sDequeueBuffer(struct preview_stream_ops* w,
8824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                            buffer_handle_t** buffer, int *stride)
8834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
8844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc;
8854717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
8864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindowBuffer* anb;
8874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    rc = native_window_dequeue_buffer_and_wait(a, &anb);
8884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
8894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        *buffer = &anb->handle;
8904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        *stride = anb->stride;
8914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
8924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return rc;
8934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
8944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
8954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#ifndef container_of
8964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#define container_of(ptr, type, member) ({                      \
8974717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    const __typeof__(((type *) 0)->member) *__mptr = (ptr);     \
8984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
8994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh#endif
9004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9014717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sLockBuffer(struct preview_stream_ops* w,
9024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  buffer_handle_t* /*buffer*/)
9034717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    (void)a;
9064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return 0;
9074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9094717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sEnqueueBuffer(struct preview_stream_ops* w,
9104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  buffer_handle_t* buffer)
9114717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return a->queueBuffer(a,
9144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh              container_of(buffer, ANativeWindowBuffer, handle), -1);
9154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9174717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sCancelBuffer(struct preview_stream_ops* w,
9184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  buffer_handle_t* buffer)
9194717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return a->cancelBuffer(a,
9224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh              container_of(buffer, ANativeWindowBuffer, handle), -1);
9234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9254717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetBufferCount(struct preview_stream_ops* w, int count)
9264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9274717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (a != nullptr) {
9304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Workaround for b/27039775
9314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Previously, setting the buffer count would reset the buffer
9324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // queue's flag that allows for all buffers to be dequeued on the
9334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // producer side, instead of just the producer's declared max count,
9344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // if no filled buffers have yet been queued by the producer.  This
9354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // reset no longer happens, but some HALs depend on this behavior,
9364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // so it needs to be maintained for HAL backwards compatibility.
9374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // Simulate the prior behavior by disconnecting/reconnecting to the
9384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // window and setting the values again.  This has the drawback of
9394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // actually causing memory reallocation, which may not have happened
9404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        // in the past.
9414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        CameraHardwareInterface *hw = hwi(w);
9424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA);
9434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA);
9444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewScalingMode != NOT_SET) {
9454717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_scaling_mode(a, hw->mPreviewScalingMode);
9464717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9474717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewTransform != NOT_SET) {
9484717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_transform(a, hw->mPreviewTransform);
9494717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9504717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewWidth != NOT_SET) {
9514717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_dimensions(a,
9524717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                    hw->mPreviewWidth, hw->mPreviewHeight);
9534717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_buffers_format(a, hw->mPreviewFormat);
9544717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9554717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewUsage != 0) {
9564717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_usage(a, hw->mPreviewUsage);
9574717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9584717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewSwapInterval != NOT_SET) {
9594717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            a->setSwapInterval(a, hw->mPreviewSwapInterval);
9604717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9614717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        if (hw->mPreviewCrop.left != NOT_SET) {
9624717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            native_window_set_crop(a, &(hw->mPreviewCrop));
9634717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        }
9644717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
9654717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9664717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return native_window_set_buffer_count(a, count);
9674717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9684717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9694717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetBuffersGeometry(struct preview_stream_ops* w,
9704717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  int width, int height, int format)
9714717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9724717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    int rc;
9734717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9744717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *hw = hwi(w);
9754717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewWidth = width;
9764717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewHeight = height;
9774717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewFormat = format;
9784717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    rc = native_window_set_buffers_dimensions(a, width, height);
9794717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    if (rc == OK) {
9804717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh        rc = native_window_set_buffers_format(a, format);
9814717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    }
9824717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return rc;
9834717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9844717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9854717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetCrop(struct preview_stream_ops *w,
9864717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  int left, int top, int right, int bottom)
9874717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
9884717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
9894717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *hw = hwi(w);
9904717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewCrop.left = left;
9914717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewCrop.top = top;
9924717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewCrop.right = right;
9934717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewCrop.bottom = bottom;
9944717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return native_window_set_crop(a, &(hw->mPreviewCrop));
9954717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
9964717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
9974717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetTimestamp(struct preview_stream_ops *w,
9984717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                           int64_t timestamp) {
9994717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
10004717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return native_window_set_buffers_timestamp(a, timestamp);
10014717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
10024717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10034717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetUsage(struct preview_stream_ops* w, int usage)
10044717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
10054717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
10064717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *hw = hwi(w);
10074717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewUsage = usage;
10084717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return native_window_set_usage(a, usage);
10094717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
10104717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10114717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sSetSwapInterval(struct preview_stream_ops *w, int interval)
10124717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
10134717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
10144717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    CameraHardwareInterface *hw = hwi(w);
10154717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    hw->mPreviewSwapInterval = interval;
10164717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return a->setSwapInterval(a, interval);
10174717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
10184717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10194717c470bf989e02f798857358471f8feb77660fYin-Chia Yehint CameraHardwareInterface::sGetMinUndequeuedBufferCount(
10204717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  const struct preview_stream_ops *w,
10214717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh                  int *count)
10224717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
10234717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    ANativeWindow *a = anw(w);
10244717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
10254717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
10264717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10274717c470bf989e02f798857358471f8feb77660fYin-Chia Yehvoid CameraHardwareInterface::initHalPreviewWindow()
10284717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh{
10294717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.cancel_buffer = sCancelBuffer;
10304717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.lock_buffer = sLockBuffer;
10314717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.dequeue_buffer = sDequeueBuffer;
10324717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.enqueue_buffer = sEnqueueBuffer;
10334717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_buffer_count = sSetBufferCount;
10344717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_buffers_geometry = sSetBuffersGeometry;
10354717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_crop = sSetCrop;
10364717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_timestamp = sSetTimestamp;
10374717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_usage = sSetUsage;
10384717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.set_swap_interval = sSetSwapInterval;
10394717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10404717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh    mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
10414717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh            sGetMinUndequeuedBufferCount;
10424717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}
10434717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh
10444717c470bf989e02f798857358471f8feb77660fYin-Chia Yeh}; // namespace android
1045