xref: /aosp_15_r20/external/minigbm/cros_gralloc/cros_gralloc_driver.h (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
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 	~cros_gralloc_driver();
26 
27 	static std::shared_ptr<cros_gralloc_driver> get_instance();
28 	bool is_supported(const struct cros_gralloc_buffer_descriptor *descriptor);
29 	int32_t allocate(const struct cros_gralloc_buffer_descriptor *descriptor,
30 			 native_handle_t **out_handle);
31 
32 	int32_t retain(buffer_handle_t handle);
33 	int32_t release(buffer_handle_t handle);
34 
35 	int32_t lock(buffer_handle_t handle, int32_t acquire_fence, bool close_acquire_fence,
36 		     const struct rectangle *rect, uint32_t map_flags,
37 		     uint8_t *addr[DRV_MAX_PLANES]);
38 	int32_t unlock(buffer_handle_t handle, int32_t *release_fence);
39 
40 	int32_t invalidate(buffer_handle_t handle);
41 	int32_t flush(buffer_handle_t handle);
42 
43 	int32_t get_backing_store(buffer_handle_t handle, uint64_t *out_store);
44 	int32_t resource_info(buffer_handle_t handle, uint32_t strides[DRV_MAX_PLANES],
45 			      uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier);
46 
47 	uint32_t get_resolved_drm_format(uint32_t drm_format, uint64_t use_flags);
48 
49 	void with_buffer(cros_gralloc_handle_t hnd,
50 			 const std::function<void(cros_gralloc_buffer *)> &function);
51 	void with_each_buffer(const std::function<void(cros_gralloc_buffer *)> &function);
52 
53       private:
54 	cros_gralloc_driver();
55 	bool is_initialized();
56 	cros_gralloc_buffer *get_buffer(cros_gralloc_handle_t hnd);
57 	bool
58 	get_resolved_format_and_use_flags(const struct cros_gralloc_buffer_descriptor *descriptor,
59 					  uint32_t *out_format, uint64_t *out_use_flags);
60 
61 	int create_reserved_region(const std::string &buffer_name, uint64_t reserved_region_size);
62 
63 #if ANDROID_API_LEVEL >= 31 && defined(HAS_DMABUF_SYSTEM_HEAP)
64 	/* For allocating cros_gralloc_buffer reserved regions for metadata. */
65 	BufferAllocator allocator_;
66 #endif
67 
68 	std::unique_ptr<struct driver, void (*)(struct driver *)> drv_;
69 
70 	struct cros_gralloc_imported_handle_info {
71 		/*
72 		 * The underlying buffer for referred to by this handle (as multiple handles can
73 		 * refer to the same buffer).
74 		 */
75 		cros_gralloc_buffer *buffer = nullptr;
76 
77 		/* The handle's refcount as a handle can be imported multiple times.*/
78 		int32_t refcount = 1;
79 	};
80 
81 	std::mutex mutex_;
82 	std::unordered_map<uint32_t, std::unique_ptr<cros_gralloc_buffer>> buffers_;
83 	std::unordered_map<cros_gralloc_handle_t, cros_gralloc_imported_handle_info> handles_;
84 
85 	/* TODO(b/242184599): remove after SwiftShader is moved to the host. */
86 	const bool is_running_with_software_rendering_ = false;
87 };
88 
89 #endif
90