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_ZslResultDispatcher"
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 "utils.h"
26 #include "zsl_result_dispatcher.h"
27
28 namespace android {
29 namespace google_camera_hal {
30
Create(uint32_t partial_result_count,ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,const StreamConfiguration & stream_config)31 std::unique_ptr<ZslResultDispatcher> ZslResultDispatcher::Create(
32 uint32_t partial_result_count,
33 ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
34 const StreamConfiguration& stream_config) {
35 ATRACE_CALL();
36 auto dispatcher = std::unique_ptr<ZslResultDispatcher>(
37 new ZslResultDispatcher(process_capture_result, notify));
38 if (dispatcher == nullptr) {
39 ALOGE("%s: Creating ZslResultDispatcher failed.", __FUNCTION__);
40 return nullptr;
41 }
42
43 status_t res = dispatcher->Initialize(partial_result_count, stream_config);
44 if (res != OK) {
45 ALOGE("%s: Initialize failed.", __FUNCTION__);
46 return nullptr;
47 }
48
49 return dispatcher;
50 }
51
ZslResultDispatcher(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify)52 ZslResultDispatcher::ZslResultDispatcher(
53 ProcessCaptureResultFunc process_capture_result, NotifyFunc notify)
54 : device_session_process_capture_result_(process_capture_result),
55 device_session_notify_(notify) {
56 }
57
Initialize(uint32_t partial_result_count,const StreamConfiguration & stream_config)58 status_t ZslResultDispatcher::Initialize(
59 uint32_t partial_result_count, const StreamConfiguration& stream_config) {
60 ATRACE_CALL();
61 process_capture_result_ =
62 ProcessCaptureResultFunc([this](std::unique_ptr<CaptureResult> result) {
63 ProcessCaptureResult(std::move(result));
64 });
65 notify_ = NotifyFunc(
66 [this](const NotifyMessage& message) { NotifyHalMessage(message); });
67
68 normal_result_dispatcher_ = std::unique_ptr<ResultDispatcher>(
69 new ResultDispatcher(partial_result_count, process_capture_result_,
70 notify_, stream_config, "ZslNormalDispatcher"));
71 if (normal_result_dispatcher_ == nullptr) {
72 ALOGE("%s: Creating normal_result_dispatcher_ failed.", __FUNCTION__);
73 return BAD_VALUE;
74 }
75
76 zsl_result_dispatcher_ = std::unique_ptr<ResultDispatcher>(
77 new ResultDispatcher(partial_result_count, process_capture_result_,
78 notify_, stream_config, "ZslZslDispatcher"));
79 if (zsl_result_dispatcher_ == nullptr) {
80 ALOGE("%s: Creating zsl_result_dispatcher_ failed.", __FUNCTION__);
81 return BAD_VALUE;
82 }
83
84 return OK;
85 }
86
ProcessCaptureResult(std::unique_ptr<CaptureResult> result)87 void ZslResultDispatcher::ProcessCaptureResult(
88 std::unique_ptr<CaptureResult> result) {
89 std::lock_guard<std::mutex> lock(process_capture_result_lock_);
90 device_session_process_capture_result_(std::move(result));
91 }
92
IsZslFrame(uint32_t frame_number)93 bool ZslResultDispatcher::IsZslFrame(uint32_t frame_number) {
94 std::lock_guard<std::mutex> lock(zsl_frames_lock_);
95 if (zsl_frames_.empty()) {
96 return false;
97 }
98 if (std::find(zsl_frames_.begin(), zsl_frames_.end(), frame_number) ==
99 zsl_frames_.end()) {
100 return false;
101 }
102 return true;
103 }
104
NotifyHalMessage(const NotifyMessage & message)105 void ZslResultDispatcher::NotifyHalMessage(const NotifyMessage& message) {
106 std::lock_guard<std::mutex> lock(result_lock_);
107 device_session_notify_(message);
108 }
109
AddPendingRequest(const CaptureRequest & pending_request,bool is_zsl_request)110 status_t ZslResultDispatcher::AddPendingRequest(
111 const CaptureRequest& pending_request, bool is_zsl_request) {
112 ATRACE_CALL();
113 if (is_zsl_request) {
114 uint32_t frame_number = pending_request.frame_number;
115 {
116 std::lock_guard<std::mutex> lock(zsl_frames_lock_);
117 zsl_frames_.push_back(frame_number);
118 }
119
120 status_t res = zsl_result_dispatcher_->AddPendingRequest(pending_request);
121 if (res != OK) {
122 std::lock_guard<std::mutex> lock(zsl_frames_lock_);
123 zsl_frames_.erase(
124 std::find(zsl_frames_.begin(), zsl_frames_.end(), frame_number));
125 }
126 return res;
127 } else {
128 return normal_result_dispatcher_->AddPendingRequest(pending_request);
129 }
130 }
131
AddResult(std::unique_ptr<CaptureResult> result)132 status_t ZslResultDispatcher::AddResult(std::unique_ptr<CaptureResult> result) {
133 ATRACE_CALL();
134 if (result == nullptr) {
135 ALOGE("%s: result is nullptr", __FUNCTION__);
136 return BAD_VALUE;
137 }
138 uint32_t frame_number = result->frame_number;
139 bool is_zsl_request = IsZslFrame(frame_number);
140 if (is_zsl_request) {
141 return zsl_result_dispatcher_->AddResult(std::move(result));
142 } else {
143 return normal_result_dispatcher_->AddResult(std::move(result));
144 }
145 }
146
AddShutter(uint32_t frame_number,int64_t timestamp_ns,int64_t readout_timestamp_ns)147 status_t ZslResultDispatcher::AddShutter(uint32_t frame_number,
148 int64_t timestamp_ns,
149 int64_t readout_timestamp_ns) {
150 ATRACE_CALL();
151 bool is_zsl_request = IsZslFrame(frame_number);
152 if (is_zsl_request) {
153 return zsl_result_dispatcher_->AddShutter(frame_number, timestamp_ns,
154 readout_timestamp_ns);
155 } else {
156 return normal_result_dispatcher_->AddShutter(frame_number, timestamp_ns,
157 readout_timestamp_ns);
158 }
159 }
160
AddError(const ErrorMessage & error)161 status_t ZslResultDispatcher::AddError(const ErrorMessage& error) {
162 ATRACE_CALL();
163 uint32_t frame_number = error.frame_number;
164 bool is_zsl_request = IsZslFrame(frame_number);
165 if (is_zsl_request) {
166 return zsl_result_dispatcher_->AddError(error);
167 } else {
168 return normal_result_dispatcher_->AddError(error);
169 }
170 }
171
RemovePendingRequest(uint32_t frame_number)172 void ZslResultDispatcher::RemovePendingRequest(uint32_t frame_number) {
173 ATRACE_CALL();
174 bool is_zsl_request = IsZslFrame(frame_number);
175 if (is_zsl_request) {
176 zsl_result_dispatcher_->RemovePendingRequest(frame_number);
177 std::lock_guard<std::mutex> lock(zsl_frames_lock_);
178 zsl_frames_.erase(
179 std::find(zsl_frames_.begin(), zsl_frames_.end(), frame_number));
180 } else {
181 normal_result_dispatcher_->RemovePendingRequest(frame_number);
182 }
183 }
184
185 } // namespace google_camera_hal
186 } // namespace android
187