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_HWL_BUFFER_ALLOCATOR_H
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_HWL_BUFFER_ALLOCATOR_H
19 
20 #include "camera_buffer_allocator_hwl.h"
21 #include "camera_device_session_hwl.h"
22 #include "hal_buffer_allocator.h"
23 
24 #include <hardware/gralloc1.h>
25 #include <utils/Errors.h>
26 #include <vector>
27 
28 namespace android {
29 namespace google_camera_hal {
30 
31 // Implement IHalBufferAllocator to allocate/free buffers from HWL allocator
32 // This allocator can be created from device session where it knows the instance
33 // of HWL allocator implementation
34 class HwlBufferAllocator : IHalBufferAllocator {
35  public:
36   // Creates HwlBuffer and allocate buffers
37   static std::unique_ptr<IHalBufferAllocator> Create(
38       CameraBufferAllocatorHwl* camera_buffer_allocator_hwl);
39 
40   virtual ~HwlBufferAllocator() = default;
41 
42   // Allocate buffers and return buffer via buffers.
43   // The buffers is owned by caller
44   status_t AllocateBuffers(const HalBufferDescriptor& buffer_descriptor,
45                            std::vector<buffer_handle_t>* buffers) override;
46 
47   void FreeBuffers(std::vector<buffer_handle_t>* buffers) override;
48 
49  protected:
50   HwlBufferAllocator() = default;
51 
52  private:
53   status_t Initialize(CameraBufferAllocatorHwl* camera_buffer_allocator_hwl);
54 
55   // Do not support the copy constructor or assignment operator
56   HwlBufferAllocator(const HwlBufferAllocator&) = delete;
57   HwlBufferAllocator& operator=(const HwlBufferAllocator&) = delete;
58 
59   CameraBufferAllocatorHwl* camera_buffer_allocator_hwl_ = nullptr;
60 
61   uint64_t id_ = 0;
62   static std::atomic<uint64_t> global_instance_count_;
63 };
64 
65 }  // namespace google_camera_hal
66 }  // namespace android
67 
68 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_HWL_BUFFER_ALLOCATOR_H
69