1 /*
2  * Copyright (C) 2016 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 #include "request_tracker.h"
18 
19 #include <gtest/gtest.h>
20 
21 using testing::Test;
22 
23 namespace default_camera_hal {
24 
25 class RequestTrackerTest : public Test {
26  protected:
SetUp()27   void SetUp() {
28     stream1_.max_buffers = 3;
29     stream2_.max_buffers = 3;
30     dut_.reset(new RequestTracker());
31     streams_ = {&stream1_, &stream2_};
32     camera3_stream_configuration_t config{
33         static_cast<uint32_t>(streams_.size()),
34         streams_.data(),
35         0,
36         nullptr};
37     dut_->SetStreamConfiguration(config);
38   }
39 
GenerateCaptureRequest(uint32_t frame,std::vector<camera3_stream_t * > streams)40   std::shared_ptr<CaptureRequest> GenerateCaptureRequest(
41       uint32_t frame, std::vector<camera3_stream_t*> streams) {
42     std::shared_ptr<CaptureRequest> request =
43         std::make_shared<CaptureRequest>();
44 
45     // Set the frame number and buffers.
46     request->frame_number = frame;
47     for (const auto stream : streams) {
48       // All we really care about for the buffers is which stream they're for.
49       camera3_stream_buffer_t buffer{stream, nullptr, 0, -1, -1};
50       request->output_buffers.push_back(buffer);
51     }
52 
53     return request;
54   }
55 
AddRequest(uint32_t frame,std::vector<camera3_stream_t * > streams,bool expected=true)56   void AddRequest(uint32_t frame,
57                   std::vector<camera3_stream_t*> streams,
58                   bool expected = true) {
59     std::shared_ptr<CaptureRequest> request =
60         GenerateCaptureRequest(frame, streams);
61     EXPECT_EQ(dut_->CanAddRequest(*request), expected);
62     if (expected) {
63       EXPECT_FALSE(dut_->InFlight(frame));
64     }
65     EXPECT_EQ(dut_->Add(request), expected);
66     if (expected) {
67       EXPECT_TRUE(dut_->InFlight(frame));
68     }
69   }
70 
71   camera3_stream_t stream1_;
72   camera3_stream_t stream2_;
73   std::vector<camera3_stream_t*> streams_;
74   std::shared_ptr<RequestTracker> dut_;
75 };
76 
TEST_F(RequestTrackerTest,AddValid)77 TEST_F(RequestTrackerTest, AddValid) {
78   uint32_t frame = 34;
79   EXPECT_FALSE(dut_->InFlight(frame));
80   AddRequest(frame, {&stream1_});
81 }
82 
TEST_F(RequestTrackerTest,AddInput)83 TEST_F(RequestTrackerTest, AddInput) {
84   EXPECT_TRUE(dut_->Empty());
85 
86   // Add a request
87   uint32_t frame = 42;
88   std::shared_ptr<CaptureRequest> expected = GenerateCaptureRequest(frame, {});
89   // Set the input buffer instead of any outputs.
90   expected->input_buffer.reset(
91       new camera3_stream_buffer_t{&stream1_, nullptr, 0, -1, -1});
92   stream1_.max_buffers = 1;
93 
94   EXPECT_TRUE(dut_->Add(expected));
95   EXPECT_TRUE(dut_->InFlight(frame));
96   // Should have added to the count of buffers for stream 1.
97   EXPECT_TRUE(dut_->StreamFull(&stream1_));
98 }
99 
TEST_F(RequestTrackerTest,AddMultipleStreams)100 TEST_F(RequestTrackerTest, AddMultipleStreams) {
101   stream1_.max_buffers = 1;
102   stream2_.max_buffers = 1;
103 
104   EXPECT_FALSE(dut_->StreamFull(&stream1_));
105   EXPECT_FALSE(dut_->StreamFull(&stream2_));
106 
107   // Add a request using both streams.
108   AddRequest(99, {&stream1_, &stream2_});
109 
110   // Should both have been counted.
111   EXPECT_TRUE(dut_->StreamFull(&stream1_));
112   EXPECT_TRUE(dut_->StreamFull(&stream2_));
113 }
114 
TEST_F(RequestTrackerTest,AddUnconfigured)115 TEST_F(RequestTrackerTest, AddUnconfigured) {
116   camera3_stream_t stream;
117   // Unconfigured should be considered full.
118   EXPECT_TRUE(dut_->StreamFull(&stream));
119   AddRequest(1, {&stream}, false);
120 }
121 
TEST_F(RequestTrackerTest,AddPastCapacity)122 TEST_F(RequestTrackerTest, AddPastCapacity) {
123   // Set the limit of stream 2 to 1.
124   stream2_.max_buffers = 1;
125 
126   for (size_t i = 0; i < stream1_.max_buffers; ++i) {
127     EXPECT_FALSE(dut_->StreamFull(&stream1_));
128     EXPECT_FALSE(dut_->StreamFull(&stream2_));
129     AddRequest(i, {&stream1_});
130   }
131   // Filled up stream 1.
132   EXPECT_TRUE(dut_->StreamFull(&stream1_));
133   // Stream 2 should still not be full since nothing was added.
134   EXPECT_FALSE(dut_->StreamFull(&stream2_));
135 
136   // Limit has been hit, can't add more.
137   AddRequest(stream1_.max_buffers, {&stream1_, &stream2_}, false);
138   EXPECT_TRUE(dut_->StreamFull(&stream1_));
139   // Should not have added to the count of stream 2.
140   EXPECT_FALSE(dut_->StreamFull(&stream2_));
141 }
142 
TEST_F(RequestTrackerTest,AddDuplicate)143 TEST_F(RequestTrackerTest, AddDuplicate) {
144   uint32_t frame = 42;
145   AddRequest(frame, {&stream1_});
146   // Can't add a duplicate.
147   AddRequest(frame, {&stream2_}, false);
148 }
149 
TEST_F(RequestTrackerTest,RemoveValid)150 TEST_F(RequestTrackerTest, RemoveValid) {
151   EXPECT_TRUE(dut_->Empty());
152 
153   // Add a request
154   uint32_t frame = 42;
155   std::shared_ptr<CaptureRequest> request =
156       GenerateCaptureRequest(frame, {&stream1_});
157   EXPECT_TRUE(dut_->Add(request));
158   EXPECT_TRUE(dut_->InFlight(frame));
159   AddRequest(frame + 1, {&stream1_});
160   EXPECT_FALSE(dut_->Empty());
161 
162   // Remove it.
163   EXPECT_TRUE(dut_->Remove(request));
164   // Should have removed only the desired request.
165   EXPECT_FALSE(dut_->Empty());
166 }
167 
TEST_F(RequestTrackerTest,RemoveInvalidFrame)168 TEST_F(RequestTrackerTest, RemoveInvalidFrame) {
169   EXPECT_TRUE(dut_->Empty());
170 
171   // Add a request
172   uint32_t frame = 42;
173   AddRequest(frame, {&stream1_});
174   EXPECT_FALSE(dut_->Empty());
175 
176   // Try to remove a different one.
177   uint32_t bad_frame = frame + 1;
178   std::shared_ptr<CaptureRequest> bad_request =
179       GenerateCaptureRequest(bad_frame, {&stream1_});
180   EXPECT_FALSE(dut_->InFlight(bad_frame));
181   EXPECT_FALSE(dut_->Remove(bad_request));
182   EXPECT_FALSE(dut_->Empty());
183 }
184 
TEST_F(RequestTrackerTest,RemoveInvalidData)185 TEST_F(RequestTrackerTest, RemoveInvalidData) {
186   EXPECT_TRUE(dut_->Empty());
187 
188   // Add a request
189   uint32_t frame = 42;
190   AddRequest(frame, {&stream1_});
191   EXPECT_FALSE(dut_->Empty());
192 
193   // Try to remove a different one.
194   // Even though this request looks the same, that fact that it is
195   // a pointer to a different object means it should fail.
196   std::shared_ptr<CaptureRequest> bad_request =
197       GenerateCaptureRequest(frame, {&stream1_});
198   EXPECT_TRUE(dut_->InFlight(frame));
199   EXPECT_FALSE(dut_->Remove(bad_request));
200   EXPECT_FALSE(dut_->Empty());
201 }
202 
TEST_F(RequestTrackerTest,RemoveNull)203 TEST_F(RequestTrackerTest, RemoveNull) {
204   EXPECT_FALSE(dut_->Remove(nullptr));
205 }
206 
TEST_F(RequestTrackerTest,ClearRequests)207 TEST_F(RequestTrackerTest, ClearRequests) {
208   // Create some requests.
209   uint32_t frame1 = 42;
210   uint32_t frame2 = frame1 + 1;
211   std::shared_ptr<CaptureRequest> request1 =
212       GenerateCaptureRequest(frame1, {&stream1_});
213   std::shared_ptr<CaptureRequest> request2 =
214       GenerateCaptureRequest(frame2, {&stream2_});
215   std::set<std::shared_ptr<CaptureRequest>> expected;
216   expected.insert(request1);
217   expected.insert(request2);
218 
219   // Insert them.
220   EXPECT_TRUE(dut_->Add(request1));
221   EXPECT_TRUE(dut_->Add(request2));
222   EXPECT_TRUE(dut_->InFlight(frame1));
223   EXPECT_TRUE(dut_->InFlight(frame2));
224   EXPECT_FALSE(dut_->Empty());
225   std::set<std::shared_ptr<CaptureRequest>> actual;
226 
227   // Clear them out.
228   dut_->Clear(&actual);
229   EXPECT_TRUE(dut_->Empty());
230   EXPECT_EQ(actual, expected);
231 
232   // Configuration (max values) should not have been cleared.
233   EXPECT_TRUE(dut_->Add(request1));
234 }
235 
TEST_F(RequestTrackerTest,ClearRequestsNoResult)236 TEST_F(RequestTrackerTest, ClearRequestsNoResult) {
237   // Add some requests.
238   EXPECT_TRUE(dut_->Empty());
239   AddRequest(1, {&stream1_});
240   AddRequest(2, {&stream2_});
241   EXPECT_FALSE(dut_->Empty());
242   // Don't bother getting the cleared requests.
243   dut_->Clear();
244   EXPECT_TRUE(dut_->Empty());
245 }
246 
TEST_F(RequestTrackerTest,ClearConfiguration)247 TEST_F(RequestTrackerTest, ClearConfiguration) {
248   EXPECT_FALSE(dut_->StreamFull(&stream1_));
249   EXPECT_FALSE(dut_->StreamFull(&stream2_));
250 
251   // Clear the configuration.
252   dut_->ClearStreamConfiguration();
253 
254   // Both streams should be considered full now, since neither is configured.
255   EXPECT_TRUE(dut_->StreamFull(&stream1_));
256   EXPECT_TRUE(dut_->StreamFull(&stream2_));
257 }
258 
259 }  // namespace default_camera_hal
260