1 /* 2 * Copyright 2017 The Chromium OS Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 7 #ifndef CROS_GRALLOC_DRIVER_H 8 #define CROS_GRALLOC_DRIVER_H 9 10 #include "cros_gralloc_buffer.h" 11 12 #include <functional> 13 #include <memory> 14 #include <mutex> 15 #include <string> 16 #include <unordered_map> 17 18 #if ANDROID_API_LEVEL >= 31 && defined(HAS_DMABUF_SYSTEM_HEAP) 19 #include <BufferAllocator/BufferAllocator.h> 20 #endif 21 22 class cros_gralloc_driver 23 { 24 public: 25 static cros_gralloc_driver *get_instance(); 26 bool is_supported(const struct cros_gralloc_buffer_descriptor *descriptor); 27 int32_t allocate(const struct cros_gralloc_buffer_descriptor *descriptor, 28 native_handle_t **out_handle); 29 30 int32_t retain(buffer_handle_t handle); 31 int32_t release(buffer_handle_t handle); 32 33 int32_t lock(buffer_handle_t handle, int32_t acquire_fence, bool close_acquire_fence, 34 const struct rectangle *rect, uint32_t map_flags, 35 uint8_t *addr[DRV_MAX_PLANES]); 36 int32_t unlock(buffer_handle_t handle, int32_t *release_fence); 37 38 int32_t invalidate(buffer_handle_t handle); 39 int32_t flush(buffer_handle_t handle, int32_t *release_fence); 40 41 int32_t get_backing_store(buffer_handle_t handle, uint64_t *out_store); 42 int32_t resource_info(buffer_handle_t handle, uint32_t strides[DRV_MAX_PLANES], 43 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier); 44 45 int32_t get_reserved_region(buffer_handle_t handle, void **reserved_region_addr, 46 uint64_t *reserved_region_size); 47 48 uint32_t get_resolved_drm_format(uint32_t drm_format, uint64_t use_flags); 49 50 void with_buffer(cros_gralloc_handle_t hnd, 51 const std::function<void(cros_gralloc_buffer *)> &function); 52 void with_each_buffer(const std::function<void(cros_gralloc_buffer *)> &function); 53 54 private: 55 cros_gralloc_driver(); 56 ~cros_gralloc_driver(); 57 bool is_initialized(); 58 cros_gralloc_buffer *get_buffer(cros_gralloc_handle_t hnd); 59 bool 60 get_resolved_format_and_use_flags(const struct cros_gralloc_buffer_descriptor *descriptor, 61 uint32_t *out_format, uint64_t *out_use_flags); 62 63 int create_reserved_region(const std::string &buffer_name, uint64_t reserved_region_size); 64 65 #if ANDROID_API_LEVEL >= 31 && defined(HAS_DMABUF_SYSTEM_HEAP) 66 /* For allocating cros_gralloc_buffer reserved regions for metadata. */ 67 BufferAllocator allocator_; 68 #endif 69 70 std::unique_ptr<struct driver, void (*)(struct driver *)> drv_; 71 72 struct cros_gralloc_imported_handle_info { 73 /* 74 * The underlying buffer for referred to by this handle (as multiple handles can 75 * refer to the same buffer). 76 */ 77 cros_gralloc_buffer *buffer = nullptr; 78 79 /* The handle's refcount as a handle can be imported multiple times.*/ 80 int32_t refcount = 1; 81 }; 82 83 std::mutex mutex_; 84 std::unordered_map<uint32_t, std::unique_ptr<cros_gralloc_buffer>> buffers_; 85 std::unordered_map<cros_gralloc_handle_t, cros_gralloc_imported_handle_info> handles_; 86 }; 87 88 #endif 89