1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <[email protected]>
25 */
26
27 #ifndef H_ETNAVIV_RESOURCE
28 #define H_ETNAVIV_RESOURCE
29
30 #include "etnaviv_internal.h"
31 #include "etnaviv_tiling.h"
32 #include "pipe/p_state.h"
33 #include "util/format/u_format.h"
34 #include "util/list.h"
35 #include "util/hash_table.h"
36 #include "util/u_helpers.h"
37 #include "util/u_range.h"
38
39 #include "drm-uapi/drm_fourcc.h"
40
41 struct etna_context;
42 struct pipe_screen;
43 struct util_dynarray;
44
45 struct etna_ts_sw_meta {
46 uint16_t version;
47 struct {
48 uint16_t data_offset;
49 uint32_t data_size;
50 uint32_t layer_stride;
51 uint32_t comp_format;
52 uint64_t clear_value;
53 uint32_t seqno;
54 uint32_t flush_seqno;
55 uint8_t valid;
56 uint8_t pad[3];
57 } v0;
58 };
59
60 struct etna_resource_level {
61 unsigned width, height; /* in pixels */
62 unsigned padded_width, padded_height; /* in samples */
63 unsigned depth;
64 unsigned offset; /* offset into memory area */
65 uint32_t stride; /* row stride */
66 uint32_t layer_stride; /* layer stride */
67 unsigned size; /* total size of memory area */
68
69 uint32_t ts_offset;
70 uint32_t ts_layer_stride;
71 uint32_t ts_size;
72 uint64_t clear_value; /* clear value of resource level (mainly for TS) */
73 bool ts_valid;
74 uint8_t ts_mode;
75 int8_t ts_compress_fmt; /* COLOR_COMPRESSION_FORMAT_* (-1 = disable) */
76
77 struct etna_ts_sw_meta *ts_meta; /* metadata for shared TS */
78
79 /* keep track if we have done some per block patching */
80 bool patched;
81 struct util_dynarray *patch_offsets;
82
83 uint32_t seqno;
84 uint32_t flush_seqno;
85 };
86
87 /* returns TRUE if a is newer than b */
88 static inline bool
etna_resource_level_newer(struct etna_resource_level * a,struct etna_resource_level * b)89 etna_resource_level_newer(struct etna_resource_level *a,
90 struct etna_resource_level *b)
91 {
92 uint32_t a_seqno = a->ts_meta ? a->ts_meta->v0.seqno : a->seqno;
93 uint32_t b_seqno = b->ts_meta ? b->ts_meta->v0.seqno : b->seqno;
94
95 return (int)(a_seqno - b_seqno) > 0;
96 }
97
98 /* returns TRUE if a is older than b */
99 static inline bool
etna_resource_level_older(struct etna_resource_level * a,struct etna_resource_level * b)100 etna_resource_level_older(struct etna_resource_level *a,
101 struct etna_resource_level *b)
102 {
103 uint32_t a_seqno = a->ts_meta ? a->ts_meta->v0.seqno : a->seqno;
104 uint32_t b_seqno = b->ts_meta ? b->ts_meta->v0.seqno : b->seqno;
105
106 return (int)(a_seqno - b_seqno) < 0;
107 }
108
109 static inline bool
etna_resource_level_ts_valid(struct etna_resource_level * lvl)110 etna_resource_level_ts_valid(struct etna_resource_level *lvl)
111 {
112 if (unlikely(lvl->ts_meta))
113 return lvl->ts_meta->v0.valid;
114 else
115 return lvl->ts_valid;
116 }
117
118 static inline void
etna_resource_level_ts_mark_valid(struct etna_resource_level * lvl)119 etna_resource_level_ts_mark_valid(struct etna_resource_level *lvl)
120 {
121 if (unlikely(lvl->ts_meta))
122 lvl->ts_meta->v0.valid = 1;
123 else
124 lvl->ts_valid = true;
125 }
126
127 static inline void
etna_resource_level_ts_mark_invalid(struct etna_resource_level * lvl)128 etna_resource_level_ts_mark_invalid(struct etna_resource_level *lvl)
129 {
130 if (unlikely(lvl->ts_meta))
131 lvl->ts_meta->v0.valid = 0;
132 else
133 lvl->ts_valid = false;
134 }
135
136 /* returns TRUE if a is older than b */
137 static inline bool
etna_resource_level_needs_flush(struct etna_resource_level * lvl)138 etna_resource_level_needs_flush(struct etna_resource_level *lvl)
139 {
140 if (!etna_resource_level_ts_valid(lvl))
141 return false;
142
143 if (unlikely(lvl->ts_meta))
144 return ((int)(lvl->ts_meta->v0.seqno - lvl->ts_meta->v0.flush_seqno) > 0);
145 else
146 return ((int)(lvl->seqno - lvl->flush_seqno) > 0);
147 }
148
149 static inline void
etna_resource_level_mark_flushed(struct etna_resource_level * lvl)150 etna_resource_level_mark_flushed(struct etna_resource_level *lvl)
151 {
152 if (unlikely(lvl->ts_meta))
153 lvl->ts_meta->v0.flush_seqno = lvl->ts_meta->v0.seqno;
154 else
155 lvl->flush_seqno = lvl->seqno;
156 }
157
158 static inline void
etna_resource_level_mark_changed(struct etna_resource_level * lvl)159 etna_resource_level_mark_changed(struct etna_resource_level *lvl)
160 {
161 if (unlikely(lvl->ts_meta))
162 lvl->ts_meta->v0.seqno++;
163 else
164 lvl->seqno++;
165 }
166
167 static inline void
etna_resource_level_copy_seqno(struct etna_resource_level * dst,struct etna_resource_level * src)168 etna_resource_level_copy_seqno(struct etna_resource_level *dst,
169 struct etna_resource_level *src)
170 {
171 uint32_t src_seqno = src->ts_meta ? src->ts_meta->v0.seqno : src->seqno;
172
173 if (unlikely(dst->ts_meta))
174 dst->ts_meta->v0.seqno = src_seqno;
175 else
176 dst->seqno = src_seqno;
177 }
178
179 /* status of queued up but not flushed reads and write operations.
180 * In _transfer_map() we need to know if queued up rendering needs
181 * to be flushed to preserve the order of cpu and gpu access. */
182 enum etna_resource_status {
183 ETNA_PENDING_WRITE = 0x01,
184 ETNA_PENDING_READ = 0x02,
185 };
186
187 struct etna_resource {
188 struct pipe_resource base;
189 struct renderonly_scanout *scanout;
190
191 /* only lod 0 used for non-texture buffers */
192 /* Layout for surface (tiled, multitiled, split tiled, ...) */
193 enum etna_surface_layout layout;
194 uint64_t modifier;
195 /* Horizontal alignment for texture unit (TEXTURE_HALIGN_*) */
196 unsigned halign;
197 struct etna_bo *bo; /* Surface video memory */
198 struct etna_bo *ts_bo; /* Tile status video memory */
199 struct renderonly_scanout *ts_scanout; /* display compatible TS */
200
201 struct etna_resource_level levels[ETNA_NUM_LOD];
202
203 /* buffer range that has been initialized */
204 struct util_range valid_buffer_range;
205
206 /* for when TE doesn't support the base layout */
207 struct pipe_resource *texture;
208 /* for when PE doesn't support the base layout */
209 struct pipe_resource *render;
210 /* frontend flushes resource via an explicit call to flush_resource */
211 bool explicit_flush;
212 /* resource is shared outside of the screen */
213 bool shared;
214 };
215
216 /* returns TRUE if a is newer than b */
217 static inline bool
etna_resource_newer(struct etna_resource * a,struct etna_resource * b)218 etna_resource_newer(struct etna_resource *a, struct etna_resource *b)
219 {
220 assert(a->base.last_level == b->base.last_level);
221
222 for (int level = 0; level <= a->base.last_level; level++)
223 if (etna_resource_level_newer(&a->levels[level], &b->levels[level]))
224 return true;
225
226 return false;
227 }
228
229 /* returns TRUE if a is older than b */
230 static inline bool
etna_resource_older(struct etna_resource * a,struct etna_resource * b)231 etna_resource_older(struct etna_resource *a, struct etna_resource *b)
232 {
233 assert(a->base.last_level == b->base.last_level);
234
235 for (int level = 0; level <= a->base.last_level; level++)
236 if (etna_resource_level_older(&a->levels[level], &b->levels[level]))
237 return true;
238
239 return false;
240 }
241
242 /* returns TRUE if the resource needs a resolve to itself */
243 bool etna_resource_needs_flush(struct etna_resource *res);
244
245 /* is the resource only used on the sampler? */
246 static inline bool
etna_resource_sampler_only(const struct pipe_resource * pres)247 etna_resource_sampler_only(const struct pipe_resource *pres)
248 {
249 return (pres->bind & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET |
250 PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_BLENDABLE)) ==
251 PIPE_BIND_SAMPLER_VIEW;
252 }
253
254 static inline bool
etna_resource_hw_tileable(bool use_blt,const struct pipe_resource * pres)255 etna_resource_hw_tileable(bool use_blt, const struct pipe_resource *pres)
256 {
257 if (use_blt)
258 return true;
259
260 /* RS can only tile 16bpp or 32bpp formats */
261 return util_format_get_blocksize(pres->format) == 2 ||
262 util_format_get_blocksize(pres->format) == 4;
263 }
264
265 /* returns TRUE if resource TS buffer is exposed externally */
266 static inline bool
etna_resource_ext_ts(const struct etna_resource * res)267 etna_resource_ext_ts(const struct etna_resource *res)
268 {
269 return res->modifier & VIVANTE_MOD_TS_MASK;
270 }
271
272 static inline struct etna_resource *
etna_resource(struct pipe_resource * p)273 etna_resource(struct pipe_resource *p)
274 {
275 return (struct etna_resource *)p;
276 }
277
278 void
279 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
280 enum etna_resource_status status);
281
282 static inline void
resource_read(struct etna_context * ctx,struct pipe_resource * prsc)283 resource_read(struct etna_context *ctx, struct pipe_resource *prsc)
284 {
285 etna_resource_used(ctx, prsc, ETNA_PENDING_READ);
286 }
287
288 static inline void
resource_written(struct etna_context * ctx,struct pipe_resource * prsc)289 resource_written(struct etna_context *ctx, struct pipe_resource *prsc)
290 {
291 etna_resource_used(ctx, prsc, ETNA_PENDING_WRITE);
292 }
293
294 enum etna_resource_status
295 etna_resource_status(struct etna_context *ctx, struct etna_resource *res);
296
297 /* Allocate Tile Status for an etna resource.
298 * Tile status is a cache of the clear status per tile. This means a smaller
299 * surface has to be cleared which is faster.
300 * This is also called "fast clear". */
301 bool
302 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
303 struct etna_resource *prsc,
304 uint64_t modifier);
305
306 struct pipe_resource *
307 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
308 uint64_t modifier, const struct pipe_resource *templat);
309
310 void
311 etna_resource_screen_init(struct pipe_screen *pscreen);
312
313 #endif
314