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 #ifndef HARDWARE_GOOGLE_CAMERA_HAL_PIPELINE_REQUEST_ID_MANAGER_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_PIPELINE_REQUEST_ID_MANAGER_H_
19 
20 #include <utils/Errors.h>
21 #include <utility>
22 
23 #include "hal_types.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 // PipelineRequestIdManager manage mapping from frame number to request id for
29 // each pipeline.
30 class PipelineRequestIdManager {
31  public:
32   // Creates PipelineRequestIdManager
33   static std::unique_ptr<PipelineRequestIdManager> Create(
34       size_t max_pending_request = kDefaultMaxPendingRequest);
35 
36   // Set mapping between from frame number to request id.
37   status_t SetPipelineRequestId(uint32_t request_id, uint32_t frame_number,
38                                 uint32_t pipeline_id);
39 
40   // Get request id from pipeline id and frame number.
41   status_t GetPipelineRequestId(uint32_t pipeline_id, uint32_t frame_number,
42                                 uint32_t* request_id);
43 
44  protected:
45   PipelineRequestIdManager(size_t max_pending_request);
46 
47  private:
48   // Define a request id pack that bind request_id and frame_number.
49   struct RequestIdInfo {
50     // The request id set by client.
51     uint32_t request_id = 0;
52     // Frame number used to detect overflow of ring buffer.
53     uint32_t frame_number = 0;
54   };
55 
56   // Default max pending request if max_pending_request isn't provided while
57   // creating class. 64 should cover all the case.
58   static const size_t kDefaultMaxPendingRequest = 64;
59 
60   // Max pending request support in pipeline_request_ids_.
61   const size_t kMaxPendingRequest = 0;
62 
63   std::mutex pipeline_request_ids_mutex_;
64 
65   // Map from a HWL pipeline ID to a RequestIdInfo vector.
66   // Must be protected by pipeline_request_ids_mutex_.
67   std::unordered_map<uint32_t, std::vector<RequestIdInfo>> pipeline_request_ids_;
68 };
69 
70 }  // namespace google_camera_hal
71 }  // namespace android
72 
73 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_PIPELINE_REQUEST_ID_MANAGER_H_
74