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_BasicResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "basic_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 {
30 
~BasicResultProcessor()31 BasicResultProcessor::~BasicResultProcessor() {
32   // Avoid a possible timing issue that could result
33   // in invalid memory access. Once 'process_capture_result_'
34   // in 'ProcessResult' returns on the very last buffer of the
35   // last pending request, camera service will be able to
36   // re-configure the camera streams at any time. Depending
37   // on scheduling the current 'BasicResultProcessor' instance
38   // might be destroyed before 'ProcessResult' is able to
39   // unlock 'callback_lock_' which will result in undefined behavior.
40   // To resolve this block the destructor until the result
41   // callback is able to correctly unlock the mutex.
42   std::lock_guard<std::mutex> lock(callback_lock_);
43 }
44 
Create()45 std::unique_ptr<BasicResultProcessor> BasicResultProcessor::Create() {
46   auto result_processor =
47       std::unique_ptr<BasicResultProcessor>(new BasicResultProcessor());
48   if (result_processor == nullptr) {
49     ALOGE("%s: Creating BasicResultProcessor failed.", __FUNCTION__);
50     return nullptr;
51   }
52 
53   return result_processor;
54 }
55 
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,ProcessBatchCaptureResultFunc process_batch_capture_result)56 void BasicResultProcessor::SetResultCallback(
57     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
58     ProcessBatchCaptureResultFunc process_batch_capture_result) {
59   ATRACE_CALL();
60   std::lock_guard<std::mutex> lock(callback_lock_);
61   process_capture_result_ = process_capture_result;
62   notify_ = notify;
63   process_batch_capture_result_ = process_batch_capture_result;
64 }
65 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)66 status_t BasicResultProcessor::AddPendingRequests(
67     const std::vector<ProcessBlockRequest>& process_block_requests,
68     const CaptureRequest& remaining_session_request) {
69   ATRACE_CALL();
70   // This is the last result processor. Sanity check if requests contains
71   // all remaining output buffers.
72   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
73                                                   remaining_session_request)) {
74     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
75     return BAD_VALUE;
76   }
77 
78   return OK;
79 }
80 
ProcessResult(ProcessBlockResult block_result)81 void BasicResultProcessor::ProcessResult(ProcessBlockResult block_result) {
82   ATRACE_CALL();
83   std::lock_guard<std::mutex> lock(callback_lock_);
84   if (block_result.result == nullptr) {
85     ALOGW("%s: Received a nullptr result.", __FUNCTION__);
86     return;
87   }
88 
89   if (process_capture_result_ == nullptr) {
90     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
91           __FUNCTION__);
92     return;
93   }
94 
95   process_capture_result_(std::move(block_result.result));
96 }
97 
ProcessBatchResult(std::vector<ProcessBlockResult> block_results)98 void BasicResultProcessor::ProcessBatchResult(
99     std::vector<ProcessBlockResult> block_results) {
100   ATRACE_CALL();
101   std::lock_guard<std::mutex> lock(callback_lock_);
102   if (process_capture_result_ == nullptr) {
103     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
104           __FUNCTION__);
105     return;
106   }
107 
108   std::vector<std::unique_ptr<CaptureResult>> capture_results;
109   capture_results.reserve(block_results.size());
110   for (auto& block_result : block_results) {
111     if (block_result.result == nullptr) {
112       ALOGW("%s: Received a nullptr result.", __FUNCTION__);
113       continue;
114     }
115     capture_results.push_back(std::move(block_result.result));
116   }
117 
118   process_batch_capture_result_(std::move(capture_results));
119 }
120 
Notify(const ProcessBlockNotifyMessage & block_message)121 void BasicResultProcessor::Notify(const ProcessBlockNotifyMessage& block_message) {
122   ATRACE_CALL();
123   std::lock_guard<std::mutex> lock(callback_lock_);
124   if (notify_ == nullptr) {
125     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
126     return;
127   }
128 
129   notify_(block_message.message);
130 }
131 
FlushPendingRequests()132 status_t BasicResultProcessor::FlushPendingRequests() {
133   ATRACE_CALL();
134   return INVALID_OPERATION;
135 }
136 
137 }  // namespace google_camera_hal
138 }  // namespace android
139