xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_screen_cache.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #ifndef SVGA_SCREEN_CACHE_H_
9 #define SVGA_SCREEN_CACHE_H_
10 
11 
12 #include "svga_types.h"
13 #include "svga_reg.h"
14 #include "svga3d_reg.h"
15 
16 #include "util/u_thread.h"
17 
18 #include "util/list.h"
19 
20 
21 /* Guess the storage size of cached surfaces and try and keep it under
22  * this amount:
23  */
24 #define SVGA_HOST_SURFACE_CACHE_BYTES (16 * 1024 * 1024)
25 
26 /* Maximum number of discrete surfaces in the cache:
27  */
28 #define SVGA_HOST_SURFACE_CACHE_SIZE 1024
29 
30 /* Number of hash buckets:
31  */
32 #define SVGA_HOST_SURFACE_CACHE_BUCKETS 256
33 
34 
35 struct svga_winsys_surface;
36 struct svga_screen;
37 struct svga_context;
38 
39 /**
40  * Same as svga_winsys_screen::surface_create.
41  */
42 struct svga_host_surface_cache_key
43 {
44    SVGA3dSurfaceAllFlags flags;
45    SVGA3dSurfaceFormat format;
46    SVGA3dSize size;
47    uint32_t numFaces:3;
48    uint32_t arraySize:16;
49    uint32_t numMipLevels:6;
50    uint32_t cachable:1;         /* False if this is a shared surface */
51    uint32_t sampleCount:5;
52    uint32_t scanout:1;
53    uint32_t coherent:1;
54    uint32_t persistent:1;
55 };
56 
57 
58 struct svga_host_surface_cache_entry
59 {
60    /**
61     * Head for the LRU list, svga_host_surface_cache::unused, and
62     * svga_host_surface_cache::empty
63     */
64    struct list_head head;
65 
66    /** Head for the bucket lists. */
67    struct list_head bucket_head;
68 
69    struct svga_host_surface_cache_key key;
70    struct svga_winsys_surface *handle;
71 
72    struct pipe_fence_handle *fence;
73 };
74 
75 
76 /**
77  * Cache of the host surfaces.
78  *
79  * A cache entry can be in the following stages:
80  * 1. empty (entry->handle = NULL)
81  * 2. holding a buffer in a validate list
82  * 3. holding a buffer in an invalidate list
83  * 4. holding a flushed buffer (not in any validate list) with an active fence
84  * 5. holding a flushed buffer with an expired fence
85  *
86  * An entry progresses from 1 -> 2 -> 3 -> 4 -> 5. When we need an entry to put a
87  * buffer into we preferentially take from 1, or from the least recently used
88  * buffer from 4/5.
89  */
90 struct svga_host_surface_cache
91 {
92    mtx_t mutex;
93 
94    /* Unused buffers are put in buckets to speed up lookups */
95    struct list_head bucket[SVGA_HOST_SURFACE_CACHE_BUCKETS];
96 
97    /* Entries with unused buffers, ordered from most to least recently used
98     * (3 and 4) */
99    struct list_head unused;
100 
101    /* Entries with buffers still in validate list (2) */
102    struct list_head validated;
103 
104    /* Entries with buffers still in invalidate list (3) */
105    struct list_head invalidated;
106 
107    /** Empty entries (1) */
108    struct list_head empty;
109 
110    /** The actual storage for the entries */
111    struct svga_host_surface_cache_entry entries[SVGA_HOST_SURFACE_CACHE_SIZE];
112 
113    /** Sum of sizes of all surfaces (in bytes) */
114    unsigned total_size;
115 };
116 
117 
118 void
119 svga_screen_cache_cleanup(struct svga_screen *svgascreen);
120 
121 void
122 svga_screen_cache_flush(struct svga_screen *svgascreen,
123                         struct svga_context *svga,
124                         struct pipe_fence_handle *fence);
125 
126 enum pipe_error
127 svga_screen_cache_init(struct svga_screen *svgascreen);
128 
129 
130 struct svga_winsys_surface *
131 svga_screen_surface_create(struct svga_screen *svgascreen,
132                            unsigned bind_flags, enum pipe_resource_usage usage,
133                            bool *invalidated,
134                            struct svga_host_surface_cache_key *key);
135 
136 void
137 svga_screen_surface_destroy(struct svga_screen *svgascreen,
138                             const struct svga_host_surface_cache_key *key,
139                             bool to_invalidate,
140                             struct svga_winsys_surface **handle);
141 
142 void
143 svga_screen_cache_dump(const struct svga_screen *svgascreen);
144 
145 unsigned
146 svga_surface_size(const struct svga_host_surface_cache_key *key);
147 
148 
149 #endif /* SVGA_SCREEN_CACHE_H_ */
150