1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "DualIrDepthResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <inttypes.h>
24 
25 #include "dual_ir_depth_result_processor.h"
26 #include "hal_utils.h"
27 
28 namespace android {
29 namespace google_camera_hal {
Create(InternalStreamManager * internal_stream_manager)30 std::unique_ptr<DualIrDepthResultProcessor> DualIrDepthResultProcessor::Create(
31     InternalStreamManager* internal_stream_manager) {
32   if (internal_stream_manager == nullptr) {
33     ALOGE("%s: internal_stream_manager is null.", __FUNCTION__);
34     return nullptr;
35   }
36 
37   auto result_processor = std::unique_ptr<DualIrDepthResultProcessor>(
38       new DualIrDepthResultProcessor(internal_stream_manager));
39   if (result_processor == nullptr) {
40     ALOGE("%s: Failed to create DualIrDepthResultProcessor.", __FUNCTION__);
41     return nullptr;
42   }
43 
44   return result_processor;
45 }
46 
DualIrDepthResultProcessor(InternalStreamManager * internal_stream_manager)47 DualIrDepthResultProcessor::DualIrDepthResultProcessor(
48     InternalStreamManager* internal_stream_manager)
49     : internal_stream_manager_(internal_stream_manager) {
50 }
51 
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify)52 void DualIrDepthResultProcessor::SetResultCallback(
53     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify) {
54   std::lock_guard<std::mutex> lock(callback_lock_);
55   process_capture_result_ = process_capture_result;
56   notify_ = notify;
57 }
58 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)59 status_t DualIrDepthResultProcessor::AddPendingRequests(
60     const std::vector<ProcessBlockRequest>& process_block_requests,
61     const CaptureRequest& remaining_session_request) {
62   ATRACE_CALL();
63   // This is the last result processor. Sanity check if requests contains
64   // all remaining output buffers.
65   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
66                                                   remaining_session_request)) {
67     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
68     return BAD_VALUE;
69   }
70 
71   return OK;
72 }
73 
ProcessResult(ProcessBlockResult block_result)74 void DualIrDepthResultProcessor::ProcessResult(ProcessBlockResult block_result) {
75   ATRACE_CALL();
76   std::lock_guard<std::mutex> lock(callback_lock_);
77   std::unique_ptr<CaptureResult> result = std::move(block_result.result);
78   if (result == nullptr) {
79     ALOGW("%s: block_result has a null result.", __FUNCTION__);
80     return;
81   }
82 
83   if (process_capture_result_ == nullptr) {
84     ALOGE("%s: process_capture_result_ is null, dropping a result.",
85           __FUNCTION__);
86     return;
87   }
88 
89   // Depth Process Block should not return result metadata
90   if (result->result_metadata != nullptr) {
91     ALOGE("%s: non-null result metadata received from the depth process block",
92           __FUNCTION__);
93     return;
94   }
95 
96   // Depth Process Block only returns depth stream buffer, so recycle any input
97   // buffers to internal stream manager and forward the depth buffer to the
98   // framework right away.
99   for (auto& buffer : result->input_buffers) {
100     status_t res = internal_stream_manager_->ReturnStreamBuffer(buffer);
101     if (res != OK) {
102       ALOGE(
103           "%s: Failed to returned internal buffer[buffer_handle:%p, "
104           "stream_id:%d, buffer_id%" PRIu64 "].",
105           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
106     } else {
107       ALOGV(
108           "%s: Successfully returned internal buffer[buffer_handle:%p, "
109           "stream_id:%d, buffer_id%" PRIu64 "].",
110           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
111     }
112   }
113 }
114 
Notify(const ProcessBlockNotifyMessage & block_message)115 void DualIrDepthResultProcessor::Notify(
116     const ProcessBlockNotifyMessage& block_message) {
117   ATRACE_CALL();
118   std::lock_guard<std::mutex> lock(callback_lock_);
119   const NotifyMessage& message = block_message.message;
120   if (notify_ == nullptr) {
121     ALOGE("%s: notify_ is null, dropping a message", __FUNCTION__);
122     return;
123   }
124 
125   if (message.type != MessageType::kError) {
126     ALOGE(
127         "%s: depth result processor is not supposed to return shutter, "
128         "dropping a message.",
129         __FUNCTION__);
130     return;
131   }
132 
133   notify_(message);
134 }
135 
FlushPendingRequests()136 status_t DualIrDepthResultProcessor::FlushPendingRequests() {
137   ATRACE_CALL();
138   return INVALID_OPERATION;
139 }
140 
141 }  // namespace google_camera_hal
142 }  // namespace android
143