10dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh/*
20dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Copyright (C) 2015 The Android Open Source Project
30dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh *
40dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Licensed under the Apache License, Version 2.0 (the "License");
50dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * you may not use this file except in compliance with the License.
60dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * You may obtain a copy of the License at
70dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh *
80dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh *      http://www.apache.org/licenses/LICENSE-2.0
90dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh *
100dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Unless required by applicable law or agreed to in writing, software
110dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * distributed under the License is distributed on an "AS IS" BASIS,
120dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * See the License for the specific language governing permissions and
140dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * limitations under the License.
150dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh */
160dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
170dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh//#define LOG_NDEBUG 0
180dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#define LOG_TAG "NdkCaptureRequest"
190dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#define ATRACE_TAG ATRACE_TAG_CAMERA
200dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
210dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <utils/Log.h>
220dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <utils/Trace.h>
230dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
240dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include "NdkCaptureRequest.h"
250dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include "impl/ACameraMetadata.h"
260dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include "impl/ACaptureRequest.h"
270dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
280dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
290dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehcamera_status_t ACameraOutputTarget_create(
300dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ANativeWindow* window, ACameraOutputTarget** out) {
310dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (window == nullptr) {
330dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ALOGE("%s: Error: input window is null", __FUNCTION__);
340dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;
350dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
360dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    *out = new ACameraOutputTarget(window);
370dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return ACAMERA_OK;
380dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
390dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
400dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
410dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehvoid ACameraOutputTarget_free(ACameraOutputTarget* target) {
420dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
430dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (target != nullptr) {
440dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        delete target;
450dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
460dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return;
470dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
480dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
490dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
500dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehcamera_status_t ACaptureRequest_addTarget(
510dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ACaptureRequest* req, const ACameraOutputTarget* target) {
520dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
530dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (req == nullptr || req->targets == nullptr || target == nullptr) {
54ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        ALOGE("%s: Error: invalid input: req %p, req-targets %p, target %p",
550dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh                __FUNCTION__, req, req->targets, target);
560dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;
570dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
580dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    auto pair = req->targets->mOutputs.insert(*target);
590dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (!pair.second) {
60ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        ALOGW("%s: target %p already exists!", __FUNCTION__, target);
610dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
620dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return ACAMERA_OK;
630dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
640dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
650dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
660dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehcamera_status_t ACaptureRequest_removeTarget(
670dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ACaptureRequest* req, const ACameraOutputTarget* target) {
680dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
690dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (req == nullptr || req->targets == nullptr || target == nullptr) {
70ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        ALOGE("%s: Error: invalid input: req %p, req-targets %p, target %p",
710dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh                __FUNCTION__, req, req->targets, target);
720dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;
730dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
740dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    req->targets->mOutputs.erase(*target);
750dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return ACAMERA_OK;
760dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
770dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
780dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
790dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehcamera_status_t ACaptureRequest_getConstEntry(
800dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        const ACaptureRequest* req, uint32_t tag, ACameraMetadata_const_entry* entry) {
810dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
820dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (req == nullptr || entry == nullptr) {
830dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ALOGE("%s: invalid argument! req 0x%p, tag 0x%x, entry 0x%p",
840dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh               __FUNCTION__, req, tag, entry);
850dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;
860dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
870dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return req->settings->getConstEntry(tag, entry);
880dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
890dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
908aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia YehEXPORT
918aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yehcamera_status_t ACaptureRequest_getAllTags(
928aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh        const ACaptureRequest* req, /*out*/int32_t* numTags, /*out*/const uint32_t** tags) {
938aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh    ATRACE_CALL();
948aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh    if (req == nullptr || numTags == nullptr || tags == nullptr) {
958aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh        ALOGE("%s: invalid argument! request %p, numTags %p, tags %p",
968aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh               __FUNCTION__, req, numTags, tags);
978aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;
988aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh    }
998aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh    return req->settings->getTags(numTags, tags);
1008aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh}
1018aac03f4d6c66749803d5708086ba5f509aa88c6Yin-Chia Yeh
1020dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#define SET_ENTRY(NAME,NDK_TYPE)                                                        \
1030dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT                                                                                  \
1040dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehcamera_status_t ACaptureRequest_setEntry_##NAME(                                        \
1050dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ACaptureRequest* req, uint32_t tag, uint32_t count, const NDK_TYPE* data) {     \
1060dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();                                                                      \
1070dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (req == nullptr || (count > 0 && data == nullptr)) {                             \
1080dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        ALOGE("%s: invalid argument! req %p, tag 0x%x, count %d, data 0x%p",            \
1090dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh               __FUNCTION__, req, tag, count, data);                                    \
1100dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return ACAMERA_ERROR_INVALID_PARAMETER;                                         \
1110dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }                                                                                   \
1120dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return req->settings->update(tag, count, data);                                     \
1130dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
1140dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
1150dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(u8,uint8_t)
1160dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(i32,int32_t)
1170dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(float,float)
1180dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(double,double)
1190dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(i64,int64_t)
1200dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehSET_ENTRY(rational,ACameraMetadata_rational)
1210dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
1220dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#undef SET_ENTRY
1230dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
1240dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia YehEXPORT
1250dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehvoid ACaptureRequest_free(ACaptureRequest* request) {
1260dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ATRACE_CALL();
1270dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    if (request == nullptr) {
1280dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return;
1290dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
1300dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    delete request->settings;
1310dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    delete request->targets;
1320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    delete request;
1330dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    return;
1340dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}
135