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 "dual_ir_depth_result_processor.h"
21 
22 #include <inttypes.h>
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 
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,ProcessBatchCaptureResultFunc)52 void DualIrDepthResultProcessor::SetResultCallback(
53     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
54     ProcessBatchCaptureResultFunc /*process_batch_capture_result*/) {
55   std::lock_guard<std::mutex> lock(callback_lock_);
56   process_capture_result_ = process_capture_result;
57   notify_ = notify;
58 }
59 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)60 status_t DualIrDepthResultProcessor::AddPendingRequests(
61     const std::vector<ProcessBlockRequest>& process_block_requests,
62     const CaptureRequest& remaining_session_request) {
63   ATRACE_CALL();
64   // This is the last result processor. Sanity check if requests contains
65   // all remaining output buffers.
66   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
67                                                   remaining_session_request)) {
68     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
69     return BAD_VALUE;
70   }
71 
72   return OK;
73 }
74 
ProcessResult(ProcessBlockResult block_result)75 void DualIrDepthResultProcessor::ProcessResult(ProcessBlockResult block_result) {
76   ATRACE_CALL();
77   std::lock_guard<std::mutex> lock(callback_lock_);
78   std::unique_ptr<CaptureResult> result = std::move(block_result.result);
79   if (result == nullptr) {
80     ALOGW("%s: block_result has a null result.", __FUNCTION__);
81     return;
82   }
83 
84   if (process_capture_result_ == nullptr) {
85     ALOGE("%s: process_capture_result_ is null, dropping a result.",
86           __FUNCTION__);
87     return;
88   }
89 
90   // Depth Process Block should not return result metadata
91   if (result->result_metadata != nullptr) {
92     ALOGE("%s: non-null result metadata received from the depth process block",
93           __FUNCTION__);
94     return;
95   }
96 
97   // Depth Process Block only returns depth stream buffer, so recycle any input
98   // buffers to internal stream manager and forward the depth buffer to the
99   // framework right away.
100   for (auto& buffer : result->input_buffers) {
101     status_t res = internal_stream_manager_->ReturnStreamBuffer(buffer);
102     if (res != OK) {
103       ALOGE(
104           "%s: Failed to returned internal buffer[buffer_handle:%p, "
105           "stream_id:%d, buffer_id%" PRIu64 "].",
106           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
107     } else {
108       ALOGV(
109           "%s: Successfully returned internal buffer[buffer_handle:%p, "
110           "stream_id:%d, buffer_id%" PRIu64 "].",
111           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
112     }
113   }
114 }
115 
Notify(const ProcessBlockNotifyMessage & block_message)116 void DualIrDepthResultProcessor::Notify(
117     const ProcessBlockNotifyMessage& block_message) {
118   ATRACE_CALL();
119   std::lock_guard<std::mutex> lock(callback_lock_);
120   const NotifyMessage& message = block_message.message;
121   if (notify_ == nullptr) {
122     ALOGE("%s: notify_ is null, dropping a message", __FUNCTION__);
123     return;
124   }
125 
126   if (message.type != MessageType::kError) {
127     ALOGE(
128         "%s: depth result processor is not supposed to return shutter, "
129         "dropping a message.",
130         __FUNCTION__);
131     return;
132   }
133 
134   notify_(message);
135 }
136 
FlushPendingRequests()137 status_t DualIrDepthResultProcessor::FlushPendingRequests() {
138   ATRACE_CALL();
139   return INVALID_OPERATION;
140 }
141 
142 }  // namespace google_camera_hal
143 }  // namespace android
144