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_resource.h"
28 #include "util/u_range.h"
29 #include "util/list.h"
30 #include "util/u_transfer.h"
31 
32 #include "virtio-gpu/virgl_hw.h"
33 #include "virgl_screen.h"
34 #define VR_MAX_TEXTURE_2D_LEVELS 15
35 
36 #define VIRGL_BLOB_MEM_GUEST 1
37 #define VIRGL_BLOB_MEM_HOST3D 2
38 #define VIRGL_BLOB_MEM_HOST3D_GUEST 3
39 
40 struct winsys_handle;
41 struct virgl_screen;
42 struct virgl_context;
43 
44 struct virgl_resource_metadata
45 {
46    unsigned long level_offset[VR_MAX_TEXTURE_2D_LEVELS];
47    unsigned stride[VR_MAX_TEXTURE_2D_LEVELS];
48    unsigned layer_stride[VR_MAX_TEXTURE_2D_LEVELS];
49    uint32_t plane, plane_offset, total_size;
50    uint64_t modifier;
51 };
52 
53 struct virgl_resource {
54    struct u_resource u;
55    uint16_t clean_mask;
56    struct virgl_hw_res *hw_res;
57    struct virgl_resource_metadata metadata;
58 
59    /* For PIPE_BUFFER only.  Data outside of this range are uninitialized. */
60    struct util_range valid_buffer_range;
61 
62    /* This mask indicates where the resource has been bound to, excluding
63     * pipe_surface binds.
64     *
65     * This is more accurate than pipe_resource::bind.  Besides,
66     * pipe_resource::bind can be 0 with direct state access, and is not
67     * usable.
68     */
69    unsigned bind_history;
70    uint32_t blob_mem;
71 };
72 
73 struct virgl_transfer {
74    struct pipe_transfer base;
75    uint32_t offset, l_stride;
76    struct util_range range;
77    struct list_head queue_link;
78    struct pipe_transfer *resolve_transfer;
79 
80    struct virgl_hw_res *hw_res;
81    void *hw_res_map;
82    /* If not NULL, denotes that this is a copy transfer, i.e.,
83     * that the transfer source data should be taken from this
84     * resource instead of the original transfer resource.
85     */
86    struct virgl_hw_res *copy_src_hw_res;
87    /* The offset in the copy source resource to copy data from. */
88    uint32_t copy_src_offset;
89 };
90 
91 void virgl_resource_destroy(struct pipe_screen *screen,
92                             struct pipe_resource *resource);
93 
94 void virgl_init_screen_resource_functions(struct pipe_screen *screen);
95 
96 void virgl_init_context_resource_functions(struct pipe_context *ctx);
97 
98 void virgl_texture_init(struct virgl_resource *res);
99 
virgl_resource(struct pipe_resource * r)100 static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
101 {
102    return (struct virgl_resource *)r;
103 }
104 
virgl_transfer(struct pipe_transfer * trans)105 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
106 {
107    return (struct virgl_transfer *)trans;
108 }
109 
110 void virgl_buffer_init(struct virgl_resource *res);
111 
pipe_to_virgl_bind(const struct virgl_screen * vs,unsigned pbind)112 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs,
113                                           unsigned pbind)
114 {
115    unsigned outbind = 0;
116    if (pbind & PIPE_BIND_DEPTH_STENCIL)
117       outbind |= VIRGL_BIND_DEPTH_STENCIL;
118    if (pbind & PIPE_BIND_RENDER_TARGET)
119       outbind |= VIRGL_BIND_RENDER_TARGET;
120    if (pbind & PIPE_BIND_SAMPLER_VIEW)
121       outbind |= VIRGL_BIND_SAMPLER_VIEW;
122    if (pbind & PIPE_BIND_VERTEX_BUFFER)
123       outbind |= VIRGL_BIND_VERTEX_BUFFER;
124    if (pbind & PIPE_BIND_INDEX_BUFFER)
125       outbind |= VIRGL_BIND_INDEX_BUFFER;
126    if (pbind & PIPE_BIND_CONSTANT_BUFFER)
127       outbind |= VIRGL_BIND_CONSTANT_BUFFER;
128    if (pbind & PIPE_BIND_DISPLAY_TARGET)
129       outbind |= VIRGL_BIND_DISPLAY_TARGET;
130    if (pbind & PIPE_BIND_STREAM_OUTPUT)
131       outbind |= VIRGL_BIND_STREAM_OUTPUT;
132    if (pbind & PIPE_BIND_CURSOR)
133       outbind |= VIRGL_BIND_CURSOR;
134    if (pbind & PIPE_BIND_CUSTOM)
135       outbind |= VIRGL_BIND_CUSTOM;
136    if (pbind & PIPE_BIND_SCANOUT)
137       outbind |= VIRGL_BIND_SCANOUT;
138    if (pbind & PIPE_BIND_SHARED)
139       outbind |= VIRGL_BIND_SHARED;
140    if (pbind & PIPE_BIND_SHADER_BUFFER)
141       outbind |= VIRGL_BIND_SHADER_BUFFER;
142    if (pbind & PIPE_BIND_QUERY_BUFFER)
143       outbind |= VIRGL_BIND_QUERY_BUFFER;
144    if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
145       if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
146          outbind |= VIRGL_BIND_COMMAND_ARGS;
147 
148    /* Staging resources should only be created directly through the winsys,
149     * not using pipe_resources.
150     */
151    assert(!(outbind & VIRGL_BIND_STAGING));
152 
153    return outbind;
154 }
155 
pipe_to_virgl_flags(const struct virgl_screen * vs,unsigned pflags)156 static inline unsigned pipe_to_virgl_flags(const struct virgl_screen *vs,
157                                            unsigned pflags)
158 {
159    unsigned out_flags = 0;
160    if (pflags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
161       out_flags |= VIRGL_RESOURCE_FLAG_MAP_PERSISTENT;
162 
163    if (pflags & PIPE_RESOURCE_FLAG_MAP_COHERENT)
164       out_flags |= VIRGL_RESOURCE_FLAG_MAP_COHERENT;
165 
166    return out_flags;
167 }
168 
169 void *
170 virgl_resource_transfer_map(struct pipe_context *ctx,
171                             struct pipe_resource *resource,
172                             unsigned level,
173                             unsigned usage,
174                             const struct pipe_box *box,
175                             struct pipe_transfer **transfer);
176 
177 struct virgl_transfer *
178 virgl_resource_create_transfer(struct virgl_context *vctx,
179                                struct pipe_resource *pres,
180                                const struct virgl_resource_metadata *metadata,
181                                unsigned level, unsigned usage,
182                                const struct pipe_box *box);
183 
184 void virgl_resource_destroy_transfer(struct virgl_context *vctx,
185                                      struct virgl_transfer *trans);
186 
187 void virgl_resource_destroy(struct pipe_screen *screen,
188                             struct pipe_resource *resource);
189 
190 bool virgl_resource_get_handle(struct pipe_screen *screen,
191                                struct pipe_resource *resource,
192                                struct winsys_handle *whandle);
193 
194 void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
195 
196 #endif
197