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_UTILS_ZSL_BUFFER_MANAGER_H
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_BUFFER_MANAGER_H
19 
20 #include <utils/Errors.h>
21 #include <deque>
22 #include <functional>
23 #include <list>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <vector>
28 
29 #include "gralloc_buffer_allocator.h"
30 #include "hal_buffer_allocator.h"
31 
32 #include "hal_types.h"
33 
34 namespace android {
35 namespace google_camera_hal {
36 
37 // ZslBufferManager creates and manages ZSL buffers.
38 class ZslBufferManager {
39  public:
40   // allocator will be used to allocate buffers. If allocator is nullptr,
41   // GrallocBufferAllocator will be used to allocate buffers.
42   ZslBufferManager(IHalBufferAllocator* allocator = nullptr,
43                    int partial_result_count = 1);
44   virtual ~ZslBufferManager();
45 
46   // Defines a ZSL buffer.
47   struct ZslBuffer {
48     // Original frame number of this ZSL buffer captured by HAL.
49     uint32_t frame_number = 0;
50     // Buffer
51     StreamBuffer buffer;
52     // Original result metadata of this ZSL buffer captured by HAL.
53     std::unique_ptr<HalCameraMetadata> metadata;
54     // Last partial result received
55     int partial_result = 0;
56   };
57 
58   // Allocate buffers. This can only be called once.
59   // The second call will return ALREADY_EXISTS.
60   status_t AllocateBuffers(const HalBufferDescriptor& buffer_descriptor);
61 
62   // Get an empty buffer for capture. The caller can modify the buffer data
63   // but the buffer is owned by ZslBufferManager and
64   // must not be freed by the caller.
65   buffer_handle_t GetEmptyBuffer();
66 
67   // Return an empty buffer that was previously obtained by GetEmptyBuffer().
68   status_t ReturnEmptyBuffer(buffer_handle_t buffer);
69 
70   // Return the buffer part of a filled buffer
71   // that was previously obtained by GetEmptyBuffer().
72   status_t ReturnFilledBuffer(uint32_t frame_number, const StreamBuffer& buffer);
73 
74   // Return the metadata part of a filled buffer
75   // that was previously obtained by GetEmptyBuffer().
76   // ZSL buffer manager will make a copy of metadata.
77   // The caller still owns metadata.
78   status_t ReturnMetadata(uint32_t frame_number,
79                           const HalCameraMetadata* metadata, int partial_result);
80 
81   // Get a number of the most recent ZSL buffers.
82   // If numBuffers is larger than available ZSL buffers,
83   // zslBuffers will contain all available ZSL buffers,
84   // i.e. zslBuffers.size() may be smaller than numBuffers.
85   // The buffer and metadata are owned by ZslBufferManager and
86   // must not be freed by the caller.
87   // minBuffers is the minimum number of buffers the
88   // zsl buffer manager should return. If this can not be satisfied
89   // (i.e. not enough ZSL buffers exist),
90   // this GetMostRecentZslBuffers returns an empty vector.
91   void GetMostRecentZslBuffers(std::vector<ZslBuffer>* zsl_buffers,
92                                uint32_t num_buffers, uint32_t min_buffers);
93 
94   // Return a ZSL buffer that was previously obtained by
95   // GetMostRecentZslBuffers().
96   void ReturnZslBuffer(ZslBuffer zsl_buffer);
97 
98   // Return a vector of ZSL buffers that were previously obtained by
99   // GetMostRecentZslBuffers().
100   void ReturnZslBuffers(std::vector<ZslBuffer> zsl_buffers);
101 
102   // Check ZslBuffers are allocated or not.
IsBufferAllocated()103   bool IsBufferAllocated() {
104     return allocated_;
105   };
106 
107   // Check pending_zsl_buffers_ is empty or not.
108   bool IsPendingBufferEmpty();
109 
110   // Add buffer map to pending_zsl_buffers_
111   void AddPendingBuffers(const std::vector<ZslBuffer>& buffers);
112 
113   // Clean buffer map from pending_zsl_buffers_
114   status_t CleanPendingBuffers(std::vector<ZslBuffer>* buffers);
115 
116  private:
117   static const uint32_t kMaxPartialZslBuffers = 100;
118 
119   // Max timestamp difference of the ZSL buffer and current time. Used
120   // to discard old ZSL buffers.
121   static const int64_t kMaxBufferTimestampDiff = 1000000000;  // 1 second
122 
123   // Maximum number of unused buffers. When the number of unused buffers >
124   // kMaxUnusedBuffers, it will try to free excessive buffers.
125   static const uint32_t kMaxUnusedBuffers = 2;
126 
127   // Maximum number of frames with enough unused buffers. When the number of
128   // counter > kMaxIdelBufferFrameCounter, it will try to free excessive
129   // buffers.
130   static const uint32_t kMaxIdelBufferFrameCounter = 300;
131 
132   const bool kMemoryProfilingEnabled;
133 
134   // Remove the oldest metadata.
135   status_t RemoveOldestMetadataLocked();
136 
137   // Get current BOOT_TIME timestamp in nanoseconds
138   status_t GetCurrentTimestampNs(int64_t* current_timestamp);
139 
140   // Allocate a number of buffers. Must be protected by zsl_buffers_lock_.
141   status_t AllocateBuffersLocked(uint32_t buffer_number);
142 
143   // Get an empty buffer. Must be protected by zsl_buffers_lock_.
144   buffer_handle_t GetEmptyBufferLocked();
145 
146   // Try to free unused buffers. Must be protected by zsl_buffers_lock_.
147   void FreeUnusedBuffersLocked();
148 
149   bool allocated_ = false;
150   std::mutex zsl_buffers_lock_;
151 
152   // Buffer manager for allocating the buffers. Protected by mZslBuffersLock.
153   std::unique_ptr<IHalBufferAllocator> internal_buffer_allocator_;
154 
155   // external buffer allocator
156   IHalBufferAllocator* buffer_allocator_ = nullptr;
157 
158   // Empty ZSL buffer queue. Protected by mZslBuffersLock.
159   std::deque<buffer_handle_t> empty_zsl_buffers_;
160 
161   // Filled ZSL buffers. Map from frameNumber to ZslBuffer.
162   // Ordered from the oldest to the newest buffers.
163   // Protected by mZslBuffersLock.
164   std::map<uint32_t, ZslBuffer> filled_zsl_buffers_;
165 
166   // Partially filled ZSL buffers. Either the metadata or
167   // the buffer is returned. Once the metadata and the buffer are both ready,
168   // the ZslBuffer will be moved to the filled_zsl_buffers_.
169   // Map from frameNumber to ZslBuffer. Ordered from the oldest to the newest
170   // buffers. filled_zsl_buffers_ protected by zsl_buffers_lock_.
171   std::map<uint32_t, ZslBuffer> partially_filled_zsl_buffers_;
172 
173   // Store all allocated buffers return from GrallocBufferAllocator
174   std::vector<buffer_handle_t> buffers_;
175 
176   std::mutex pending_zsl_buffers_mutex;
177 
178   // Map from buffer handle to ZSL buffer. Protected by pending_zsl_buffers_mutex.
179   std::unordered_map<buffer_handle_t, ZslBuffer> pending_zsl_buffers_;
180 
181   // Store the buffer descriptor when call AllocateBuffers()
182   // Use it for AllocateExtraBuffers()
183   HalBufferDescriptor buffer_descriptor_;
184 
185   // Count the number when there are enough unused buffers.
186   uint32_t idle_buffer_frame_counter_ = 0;
187 
188   // Partial result count reported by camera HAL
189   int partial_result_count_ = 1;
190 };
191 
192 }  // namespace google_camera_hal
193 }  // namespace android
194 
195 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_BUFFER_MANAGER_H
196