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 "GCH_RgbirdDepthResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "rgbird_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<RgbirdDepthResultProcessor> RgbirdDepthResultProcessor::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<RgbirdDepthResultProcessor>(
38       new RgbirdDepthResultProcessor(internal_stream_manager));
39   if (result_processor == nullptr) {
40     ALOGE("%s: Failed to create RgbirdDepthResultProcessor.", __FUNCTION__);
41     return nullptr;
42   }
43 
44   return result_processor;
45 }
46 
RgbirdDepthResultProcessor(InternalStreamManager * internal_stream_manager)47 RgbirdDepthResultProcessor::RgbirdDepthResultProcessor(
48     InternalStreamManager* internal_stream_manager)
49     : internal_stream_manager_(internal_stream_manager) {
50 }
51 
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,ProcessBatchCaptureResultFunc)52 void RgbirdDepthResultProcessor::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 RgbirdDepthResultProcessor::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 RgbirdDepthResultProcessor::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     // If the stream id is invalid. The input buffer is only a place holder
102     // corresponding to the input buffer metadata for the rgb pipeline.
103     if (buffer.stream_id == kInvalidStreamId) {
104       continue;
105     }
106 
107     status_t res = internal_stream_manager_->ReturnStreamBuffer(buffer);
108     if (res != OK) {
109       ALOGE(
110           "%s: Failed to returned internal buffer[buffer_handle:%p, "
111           "stream_id:%d, buffer_id%" PRIu64 "].",
112           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
113     } else {
114       ALOGV(
115           "%s: Successfully returned internal buffer[buffer_handle:%p, "
116           "stream_id:%d, buffer_id%" PRIu64 "].",
117           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
118     }
119   }
120   result->input_buffers.clear();
121 
122   process_capture_result_(std::move(result));
123 }
124 
Notify(const ProcessBlockNotifyMessage & block_message)125 void RgbirdDepthResultProcessor::Notify(
126     const ProcessBlockNotifyMessage& block_message) {
127   ATRACE_CALL();
128   std::lock_guard<std::mutex> lock(callback_lock_);
129   const NotifyMessage& message = block_message.message;
130   if (notify_ == nullptr) {
131     ALOGE("%s: notify_ is null, dropping a message", __FUNCTION__);
132     return;
133   }
134 
135   if (message.type != MessageType::kError) {
136     ALOGE(
137         "%s: depth result processor is not supposed to return shutter, "
138         "dropping a message.",
139         __FUNCTION__);
140     return;
141   }
142 
143   notify_(message);
144 }
145 
FlushPendingRequests()146 status_t RgbirdDepthResultProcessor::FlushPendingRequests() {
147   ATRACE_CALL();
148   return INVALID_OPERATION;
149 }
150 
151 }  // namespace google_camera_hal
152 }  // namespace android