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 <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <inttypes.h>
24 
25 #include "hal_utils.h"
26 #include "rgbird_depth_result_processor.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)52 void RgbirdDepthResultProcessor::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 RgbirdDepthResultProcessor::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 RgbirdDepthResultProcessor::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     // If the stream id is invalid. The input buffer is only a place holder
101     // corresponding to the input buffer metadata for the rgb pipeline.
102     if (buffer.stream_id == kInvalidStreamId) {
103       continue;
104     }
105 
106     status_t res = internal_stream_manager_->ReturnStreamBuffer(buffer);
107     if (res != OK) {
108       ALOGE(
109           "%s: Failed to returned internal buffer[buffer_handle:%p, "
110           "stream_id:%d, buffer_id%" PRIu64 "].",
111           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
112     } else {
113       ALOGV(
114           "%s: Successfully returned internal buffer[buffer_handle:%p, "
115           "stream_id:%d, buffer_id%" PRIu64 "].",
116           __FUNCTION__, buffer.buffer, buffer.stream_id, buffer.buffer_id);
117     }
118   }
119   result->input_buffers.clear();
120 
121   process_capture_result_(std::move(result));
122 }
123 
Notify(const ProcessBlockNotifyMessage & block_message)124 void RgbirdDepthResultProcessor::Notify(
125     const ProcessBlockNotifyMessage& block_message) {
126   ATRACE_CALL();
127   std::lock_guard<std::mutex> lock(callback_lock_);
128   const NotifyMessage& message = block_message.message;
129   if (notify_ == nullptr) {
130     ALOGE("%s: notify_ is null, dropping a message", __FUNCTION__);
131     return;
132   }
133 
134   if (message.type != MessageType::kError) {
135     ALOGE(
136         "%s: depth result processor is not supposed to return shutter, "
137         "dropping a message.",
138         __FUNCTION__);
139     return;
140   }
141 
142   notify_(message);
143 }
144 
FlushPendingRequests()145 status_t RgbirdDepthResultProcessor::FlushPendingRequests() {
146   ATRACE_CALL();
147   return INVALID_OPERATION;
148 }
149 
150 }  // namespace google_camera_hal
151 }  // namespace android