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_GRALLOC_BUFFER_ALLOCATOR_H
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_GRALLOC_BUFFER_ALLOCATOR_H
19 
20 #include "hal_buffer_allocator.h"
21 
22 #include <hardware/gralloc1.h>
23 #include <utils/Errors.h>
24 #include <vector>
25 
26 namespace android {
27 namespace google_camera_hal {
28 
29 // Gralloc buffer data structure
30 struct BufferDescriptor {
31   uint32_t width = 0;
32   uint32_t height = 0;
33   int32_t format = -1;
34   uint64_t producer_flags = 0;
35   uint64_t consumer_flags = 0;
36   uint32_t num_buffers = 0;
37 };
38 
39 class GrallocBufferAllocator : IHalBufferAllocator {
40  public:
41   // Creates GrallocBuffer and allocate buffers
42   static std::unique_ptr<IHalBufferAllocator> Create();
43 
44   virtual ~GrallocBufferAllocator() = default;
45 
46   // Allocate buffers and return buffer via buffers.
47   // The buffers is owned by caller
48   status_t AllocateBuffers(const HalBufferDescriptor& buffer_descriptor,
49                            std::vector<buffer_handle_t>* buffers);
50 
51   void FreeBuffers(std::vector<buffer_handle_t>* buffers);
52 
53  protected:
54   GrallocBufferAllocator() = default;
55 
56  private:
57 
58   // Do not support the copy constructor or assignment operator
59   GrallocBufferAllocator(const GrallocBufferAllocator&) = delete;
60   GrallocBufferAllocator& operator=(const GrallocBufferAllocator&) = delete;
61 
62   // Convert HAL buffer descriptor to gralloc descriptor
63   void ConvertHalBufferDescriptor(
64       const HalBufferDescriptor& hal_buffer_descriptor,
65       BufferDescriptor* gralloc_buffer_descriptor);
66 };
67 
68 }  // namespace google_camera_hal
69 }  // namespace android
70 
71 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_GRALLOC_BUFFER_ALLOCATOR_H
72