virgl: introduce and use virgl_transfer/texture/resource inline wrappers
[mesa.git] / src / gallium / drivers / virgl / virgl_resource.h
1 /*
2 * Copyright 2014, 2015 Red Hat.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef VIRGL_RESOURCE_H
25 #define VIRGL_RESOURCE_H
26
27 #include "util/u_inlines.h"
28 #include "util/u_range.h"
29 #include "util/list.h"
30 #include "util/u_transfer.h"
31
32 #include "virgl_hw.h"
33 #define VR_MAX_TEXTURE_2D_LEVELS 15
34
35 struct virgl_screen;
36 struct virgl_context;
37 struct virgl_resource {
38 struct u_resource u;
39 struct virgl_hw_res *hw_res;
40 boolean clean;
41 };
42
43 struct virgl_buffer {
44 struct virgl_resource base;
45
46 struct list_head flush_list;
47 boolean on_list;
48
49 /* The buffer range which is initialized (with a write transfer,
50 * streamout, DMA, or as a random access target). The rest of
51 * the buffer is considered invalid and can be mapped unsynchronized.
52 *
53 * This allows unsychronized mapping of a buffer range which hasn't
54 * been used yet. It's for applications which forget to use
55 * the unsynchronized map flag and expect the driver to figure it out.
56 */
57 struct util_range valid_buffer_range;
58 };
59
60 struct virgl_texture {
61 struct virgl_resource base;
62
63 unsigned long level_offset[VR_MAX_TEXTURE_2D_LEVELS];
64 unsigned stride[VR_MAX_TEXTURE_2D_LEVELS];
65 };
66
67 struct virgl_transfer {
68 struct pipe_transfer base;
69 uint32_t offset;
70 struct virgl_resource *resolve_tmp;
71 };
72
73 void virgl_resource_destroy(struct pipe_screen *screen,
74 struct pipe_resource *resource);
75
76 void virgl_init_screen_resource_functions(struct pipe_screen *screen);
77
78 void virgl_init_context_resource_functions(struct pipe_context *ctx);
79
80 struct pipe_resource *virgl_texture_create(struct virgl_screen *vs,
81 const struct pipe_resource *templ);
82
83 struct pipe_resource *virgl_texture_from_handle(struct virgl_screen *vs,
84 const struct pipe_resource *templ,
85 struct winsys_handle *whandle);
86
87 static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
88 {
89 return (struct virgl_resource *)r;
90 }
91
92 static inline struct virgl_buffer *virgl_buffer(struct pipe_resource *r)
93 {
94 return (struct virgl_buffer *)r;
95 }
96
97 static inline struct virgl_texture *virgl_texture(struct pipe_resource *r)
98 {
99 return (struct virgl_texture *)r;
100 }
101
102 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
103 {
104 return (struct virgl_transfer *)trans;
105 }
106
107 struct pipe_resource *virgl_buffer_create(struct virgl_screen *vs,
108 const struct pipe_resource *templ);
109
110 static inline unsigned pipe_to_virgl_bind(unsigned pbind)
111 {
112 unsigned outbind = 0;
113 if (pbind & PIPE_BIND_DEPTH_STENCIL)
114 outbind |= VIRGL_BIND_DEPTH_STENCIL;
115 if (pbind & PIPE_BIND_RENDER_TARGET)
116 outbind |= VIRGL_BIND_RENDER_TARGET;
117 if (pbind & PIPE_BIND_SAMPLER_VIEW)
118 outbind |= VIRGL_BIND_SAMPLER_VIEW;
119 if (pbind & PIPE_BIND_VERTEX_BUFFER)
120 outbind |= VIRGL_BIND_VERTEX_BUFFER;
121 if (pbind & PIPE_BIND_INDEX_BUFFER)
122 outbind |= VIRGL_BIND_INDEX_BUFFER;
123 if (pbind & PIPE_BIND_CONSTANT_BUFFER)
124 outbind |= VIRGL_BIND_CONSTANT_BUFFER;
125 if (pbind & PIPE_BIND_DISPLAY_TARGET)
126 outbind |= VIRGL_BIND_DISPLAY_TARGET;
127 if (pbind & PIPE_BIND_STREAM_OUTPUT)
128 outbind |= VIRGL_BIND_STREAM_OUTPUT;
129 if (pbind & PIPE_BIND_CURSOR)
130 outbind |= VIRGL_BIND_CURSOR;
131 if (pbind & PIPE_BIND_CUSTOM)
132 outbind |= VIRGL_BIND_CUSTOM;
133 if (pbind & PIPE_BIND_SCANOUT)
134 outbind |= VIRGL_BIND_SCANOUT;
135 return outbind;
136 }
137
138 bool virgl_res_needs_flush_wait(struct virgl_context *vctx,
139 struct virgl_resource *res,
140 unsigned usage);
141 bool virgl_res_needs_readback(struct virgl_context *vctx,
142 struct virgl_resource *res,
143 unsigned usage);
144 #endif