xref: /aosp_15_r20/external/minigbm/drv_priv.h (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
1 /*
2  * Copyright 2016 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 DRV_PRIV_H
8 #define DRV_PRIV_H
9 
10 #include <pthread.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 
16 #include "drv.h"
17 
18 struct bo_metadata {
19 	uint32_t width;
20 	uint32_t height;
21 	uint32_t format;
22 	uint32_t tiling;
23 	size_t num_planes;
24 	uint32_t offsets[DRV_MAX_PLANES];
25 	uint32_t sizes[DRV_MAX_PLANES];
26 	uint32_t strides[DRV_MAX_PLANES];
27 	uint64_t format_modifier;
28 	uint64_t use_flags;
29 	size_t total_size;
30 	bool cached;
31 
32 	/*
33 	 * Most of the following metadata is virtgpu cross_domain specific.  However, that backend
34 	 * needs to know traditional metadata (strides, offsets) in addition to this backend
35 	 * specific metadata.  It's easiest just to stuff all the metadata here rather than
36 	 * having two metadata structs.
37 	 */
38 	uint32_t blob_id;
39 	uint32_t map_info;
40 	int32_t memory_idx;
41 	int32_t physical_device_idx;
42 };
43 
44 struct bo {
45 	struct driver *drv;
46 	struct bo_metadata meta;
47 	bool is_test_buffer;
48 	union bo_handle handle;
49 	void *priv;
50 };
51 
52 struct format_metadata {
53 	uint32_t priority;
54 	uint32_t tiling;
55 	uint64_t modifier;
56 };
57 
58 struct combination {
59 	uint32_t format;
60 	struct format_metadata metadata;
61 	uint64_t use_flags;
62 };
63 
64 struct driver {
65 	int fd;
66 	const struct backend *backend;
67 	void *priv;
68 	pthread_mutex_t buffer_table_lock;
69 	void *buffer_table;
70 	pthread_mutex_t mappings_lock;
71 	struct drv_array *mappings;
72 	struct drv_array *combos;
73 	bool compression;
74 	bool log_bos;
75 };
76 
77 struct backend {
78 	char *name;
79 	void (*preload)(bool load);
80 	int (*init)(struct driver *drv);
81 	void (*close)(struct driver *drv);
82 	int (*bo_create)(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
83 			 uint64_t use_flags);
84 	int (*bo_create_with_modifiers)(struct bo *bo, uint32_t width, uint32_t height,
85 					uint32_t format, const uint64_t *modifiers, uint32_t count);
86 	// Either both or neither _metadata functions must be implemented.
87 	// If the functions are implemented, bo_create and bo_create_with_modifiers must not be.
88 	int (*bo_compute_metadata)(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
89 				   uint64_t use_flags, const uint64_t *modifiers, uint32_t count);
90 	int (*bo_create_from_metadata)(struct bo *bo);
91 	/* Called for every non-test-buffer BO on free */
92 	int (*bo_release)(struct bo *bo);
93 	/* Called on free if this bo is the last object referencing the contained GEM BOs */
94 	int (*bo_destroy)(struct bo *bo);
95 	int (*bo_import)(struct bo *bo, struct drv_import_fd_data *data);
96 	void *(*bo_map)(struct bo *bo, struct vma *vma, uint32_t map_flags);
97 	int (*bo_unmap)(struct bo *bo, struct vma *vma);
98 	int (*bo_invalidate)(struct bo *bo, struct mapping *mapping);
99 	int (*bo_flush)(struct bo *bo, struct mapping *mapping);
100 	void (*resolve_format_and_use_flags)(struct driver *drv, uint32_t format,
101 					     uint64_t use_flags, uint32_t *out_format,
102 					     uint64_t *out_use_flags);
103 	size_t (*num_planes_from_modifier)(struct driver *drv, uint32_t format, uint64_t modifier);
104 	int (*resource_info)(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
105 			     uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier);
106 	uint32_t (*get_max_texture_2d_size)(struct driver *drv);
107 };
108 
109 // clang-format off
110 #define BO_USE_RENDER_MASK (BO_USE_LINEAR | BO_USE_RENDERING | BO_USE_RENDERSCRIPT | \
111 			    BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_RARELY | \
112 			    BO_USE_SW_WRITE_RARELY | BO_USE_TEXTURE | BO_USE_FRONT_RENDERING)
113 
114 #define BO_USE_TEXTURE_MASK (BO_USE_LINEAR | BO_USE_RENDERSCRIPT | BO_USE_SW_READ_OFTEN | \
115 			     BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_RARELY | \
116 			     BO_USE_SW_WRITE_RARELY | BO_USE_TEXTURE | BO_USE_FRONT_RENDERING)
117 
118 #define BO_USE_SW_MASK (BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN | \
119 			BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY | BO_USE_FRONT_RENDERING)
120 
121 #define BO_USE_GPU_HW (BO_USE_RENDERING | BO_USE_TEXTURE | BO_USE_GPU_DATA_BUFFER)
122 
123 #define BO_USE_NON_GPU_HW (BO_USE_SCANOUT | BO_USE_CAMERA_WRITE | BO_USE_CAMERA_READ | \
124 			   BO_USE_HW_VIDEO_ENCODER | BO_USE_HW_VIDEO_DECODER | BO_USE_SENSOR_DIRECT_DATA)
125 
126 #define BO_USE_HW_MASK	(BO_USE_GPU_HW | BO_USE_NON_GPU_HW)
127 
128 #ifndef DRM_FORMAT_MOD_LINEAR
129 #define DRM_FORMAT_MOD_LINEAR DRM_FORMAT_MOD_NONE
130 #endif
131 
132 #define LINEAR_METADATA (struct format_metadata) { 1, 0, DRM_FORMAT_MOD_LINEAR }
133 
134 #define MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS 15
135 #define MESA_LLVMPIPE_MAX_TEXTURE_2D_SIZE (1 << (MESA_LLVMPIPE_MAX_TEXTURE_2D_LEVELS - 1))
136 #define MESA_LLVMPIPE_TILE_ORDER 6
137 #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
138 
139 // clang-format on
140 
141 #endif
142