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 <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <inttypes.h>
24 
25 #include "basic_result_processor.h"
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)56 void BasicResultProcessor::SetResultCallback(
57     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify) {
58   ATRACE_CALL();
59   std::lock_guard<std::mutex> lock(callback_lock_);
60   process_capture_result_ = process_capture_result;
61   notify_ = notify;
62 }
63 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)64 status_t BasicResultProcessor::AddPendingRequests(
65     const std::vector<ProcessBlockRequest>& process_block_requests,
66     const CaptureRequest& remaining_session_request) {
67   ATRACE_CALL();
68   // This is the last result processor. Sanity check if requests contains
69   // all remaining output buffers.
70   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
71                                                   remaining_session_request)) {
72     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
73     return BAD_VALUE;
74   }
75 
76   return OK;
77 }
78 
ProcessResult(ProcessBlockResult block_result)79 void BasicResultProcessor::ProcessResult(ProcessBlockResult block_result) {
80   ATRACE_CALL();
81   std::lock_guard<std::mutex> lock(callback_lock_);
82   if (block_result.result == nullptr) {
83     ALOGW("%s: Received a nullptr result.", __FUNCTION__);
84     return;
85   }
86 
87   if (process_capture_result_ == nullptr) {
88     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
89           __FUNCTION__);
90     return;
91   }
92 
93   process_capture_result_(std::move(block_result.result));
94 }
95 
Notify(const ProcessBlockNotifyMessage & block_message)96 void BasicResultProcessor::Notify(const ProcessBlockNotifyMessage& block_message) {
97   ATRACE_CALL();
98   std::lock_guard<std::mutex> lock(callback_lock_);
99   if (notify_ == nullptr) {
100     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
101     return;
102   }
103 
104   notify_(block_message.message);
105 }
106 
FlushPendingRequests()107 status_t BasicResultProcessor::FlushPendingRequests() {
108   ATRACE_CALL();
109   return INVALID_OPERATION;
110 }
111 
112 }  // namespace google_camera_hal
113 }  // namespace android
114