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 // Gralloc1 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(); 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 status_t Initialize(); 58 59 // Do not support the copy constructor or assignment operator 60 GrallocBufferAllocator(const GrallocBufferAllocator&) = delete; 61 GrallocBufferAllocator& operator=(const GrallocBufferAllocator&) = delete; 62 63 // Init gralloc1 interface function pointer 64 template <typename T> 65 void InitGrallocInterface(gralloc1_function_descriptor_t desc, 66 T* output_function); 67 68 // Setup descriptor before allocate buffer via gralloc1 interface 69 status_t SetupDescriptor(const BufferDescriptor& buffer_descriptor, 70 gralloc1_buffer_descriptor_t* output_descriptor); 71 72 // Convert HAL buffer descriptor to gralloc descriptor 73 void ConvertHalBufferDescriptor( 74 const HalBufferDescriptor& hal_buffer_descriptor, 75 BufferDescriptor* gralloc_buffer_descriptor); 76 77 hw_module_t* module_ = nullptr; 78 gralloc1_device_t* device_ = nullptr; 79 80 // Gralloc1 Interface 81 GRALLOC1_PFN_CREATE_DESCRIPTOR create_descriptor_ = nullptr; 82 GRALLOC1_PFN_DESTROY_DESCRIPTOR destroy_descriptor_ = nullptr; 83 GRALLOC1_PFN_SET_DIMENSIONS set_dimensions_ = nullptr; 84 GRALLOC1_PFN_SET_FORMAT set_format_ = nullptr; 85 GRALLOC1_PFN_SET_CONSUMER_USAGE set_consumer_usage_ = nullptr; 86 GRALLOC1_PFN_SET_PRODUCER_USAGE set_producer_usage_ = nullptr; 87 GRALLOC1_PFN_GET_STRIDE get_stride_ = nullptr; 88 GRALLOC1_PFN_ALLOCATE allocate_ = nullptr; 89 GRALLOC1_PFN_RELEASE release_ = nullptr; 90 }; 91 92 } // namespace google_camera_hal 93 } // namespace android 94 95 #endif // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_GRALLOC_BUFFER_ALLOCATOR_H 96