virgl: consolidate transfer code
[mesa.git] / src / gallium / drivers / virgl / virgl_texture.c
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 #include "util/u_format.h"
24 #include "util/u_inlines.h"
25 #include "util/u_memory.h"
26
27 #include "virgl_context.h"
28 #include "virgl_resource.h"
29 #include "virgl_screen.h"
30
31 static void virgl_copy_region_with_blit(struct pipe_context *pipe,
32 struct pipe_resource *dst,
33 unsigned dst_level,
34 unsigned dstx, unsigned dsty, unsigned dstz,
35 struct pipe_resource *src,
36 unsigned src_level,
37 const struct pipe_box *src_box)
38 {
39 struct pipe_blit_info blit;
40
41 memset(&blit, 0, sizeof(blit));
42 blit.src.resource = src;
43 blit.src.format = src->format;
44 blit.src.level = src_level;
45 blit.src.box = *src_box;
46 blit.dst.resource = dst;
47 blit.dst.format = dst->format;
48 blit.dst.level = dst_level;
49 blit.dst.box.x = dstx;
50 blit.dst.box.y = dsty;
51 blit.dst.box.z = dstz;
52 blit.dst.box.width = src_box->width;
53 blit.dst.box.height = src_box->height;
54 blit.dst.box.depth = src_box->depth;
55 blit.mask = util_format_get_mask(src->format) &
56 util_format_get_mask(dst->format);
57 blit.filter = PIPE_TEX_FILTER_NEAREST;
58
59 if (blit.mask) {
60 pipe->blit(pipe, &blit);
61 }
62 }
63 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
64 struct pipe_resource *orig,
65 const struct pipe_box *box,
66 unsigned level, unsigned flags)
67 {
68 memset(res, 0, sizeof(*res));
69 res->format = orig->format;
70 res->width0 = box->width;
71 res->height0 = box->height;
72 res->depth0 = 1;
73 res->array_size = 1;
74 res->usage = PIPE_USAGE_STAGING;
75 res->flags = flags;
76
77 /* We must set the correct texture target and dimensions for a 3D box. */
78 if (box->depth > 1 && util_max_layer(orig, level) > 0)
79 res->target = orig->target;
80 else
81 res->target = PIPE_TEXTURE_2D;
82
83 switch (res->target) {
84 case PIPE_TEXTURE_1D_ARRAY:
85 case PIPE_TEXTURE_2D_ARRAY:
86 case PIPE_TEXTURE_CUBE_ARRAY:
87 res->array_size = box->depth;
88 break;
89 case PIPE_TEXTURE_3D:
90 res->depth0 = box->depth;
91 break;
92 default:
93 break;
94 }
95 }
96
97 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
98 struct pipe_resource *resource,
99 unsigned level,
100 unsigned usage,
101 const struct pipe_box *box,
102 struct pipe_transfer **transfer)
103 {
104 struct virgl_context *vctx = virgl_context(ctx);
105 struct virgl_screen *vs = virgl_screen(ctx->screen);
106 struct virgl_texture *vtex = virgl_texture(resource);
107 struct virgl_transfer *trans;
108 void *ptr;
109 boolean readback = TRUE;
110 struct virgl_hw_res *hw_res;
111 bool doflushwait;
112
113 doflushwait = virgl_res_needs_flush_wait(vctx, &vtex->base, usage);
114 if (doflushwait)
115 ctx->flush(ctx, NULL, 0);
116
117 trans = virgl_resource_create_transfer(ctx, resource, &vtex->metadata, level,
118 usage, box);
119
120 if (resource->nr_samples > 1) {
121 struct pipe_resource tmp_resource;
122 virgl_init_temp_resource_from_box(&tmp_resource, resource, box,
123 level, 0);
124
125 trans->resolve_tmp = (struct virgl_resource *)ctx->screen->resource_create(ctx->screen, &tmp_resource);
126
127 virgl_copy_region_with_blit(ctx, &trans->resolve_tmp->u.b, 0, 0, 0, 0, resource, level, box);
128 ctx->flush(ctx, NULL, 0);
129 /* we want to do a resolve blit into the temporary */
130 hw_res = trans->resolve_tmp->hw_res;
131 struct virgl_resource_metadata *data = &((struct virgl_texture*)trans->resolve_tmp)->metadata;
132 trans->base.stride = data->stride[level];
133 trans->base.layer_stride = data->layer_stride[level];
134 trans->offset = 0;
135 } else {
136 hw_res = vtex->base.hw_res;
137 trans->resolve_tmp = NULL;
138 }
139
140 readback = virgl_res_needs_readback(vctx, &vtex->base, usage);
141 if (readback)
142 vs->vws->transfer_get(vs->vws, hw_res, box, trans->base.stride,
143 trans->l_stride, trans->offset, level);
144
145 if (doflushwait || readback)
146 vs->vws->resource_wait(vs->vws, vtex->base.hw_res);
147
148 ptr = vs->vws->resource_map(vs->vws, hw_res);
149 if (!ptr) {
150 slab_free(&vctx->transfer_pool, trans);
151 return NULL;
152 }
153
154 *transfer = &trans->base;
155 return ptr + trans->offset;
156 }
157
158 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
159 struct pipe_transfer *transfer)
160 {
161 struct virgl_context *vctx = virgl_context(ctx);
162 struct virgl_transfer *trans = virgl_transfer(transfer);
163 struct virgl_texture *vtex = virgl_texture(transfer->resource);
164
165 if (trans->base.usage & PIPE_TRANSFER_WRITE) {
166 if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
167 struct virgl_screen *vs = virgl_screen(ctx->screen);
168 vtex->base.clean = FALSE;
169 vctx->num_transfers++;
170 vs->vws->transfer_put(vs->vws, vtex->base.hw_res,
171 &transfer->box, trans->base.stride,
172 trans->l_stride, trans->offset,
173 transfer->level);
174
175 }
176 }
177
178 if (trans->resolve_tmp)
179 pipe_resource_reference((struct pipe_resource **)&trans->resolve_tmp, NULL);
180
181 virgl_resource_destroy_transfer(vctx, trans);
182 }
183
184 static boolean virgl_texture_get_handle(struct pipe_screen *screen,
185 struct pipe_resource *ptex,
186 struct winsys_handle *whandle)
187 {
188 struct virgl_screen *vs = virgl_screen(screen);
189 struct virgl_texture *vtex = virgl_texture(ptex);
190
191 return vs->vws->resource_get_handle(vs->vws, vtex->base.hw_res,
192 vtex->metadata.stride[0], whandle);
193 }
194
195 static void virgl_texture_destroy(struct pipe_screen *screen,
196 struct pipe_resource *res)
197 {
198 struct virgl_screen *vs = virgl_screen(screen);
199 struct virgl_texture *vtex = virgl_texture(res);
200 vs->vws->resource_unref(vs->vws, vtex->base.hw_res);
201 FREE(vtex);
202 }
203
204 static const struct u_resource_vtbl virgl_texture_vtbl =
205 {
206 virgl_texture_get_handle, /* get_handle */
207 virgl_texture_destroy, /* resource_destroy */
208 virgl_texture_transfer_map, /* transfer_map */
209 NULL, /* transfer_flush_region */
210 virgl_texture_transfer_unmap, /* transfer_unmap */
211 };
212
213 struct pipe_resource *
214 virgl_texture_from_handle(struct virgl_screen *vs,
215 const struct pipe_resource *template,
216 struct winsys_handle *whandle)
217 {
218 struct virgl_texture *tex = CALLOC_STRUCT(virgl_texture);
219 tex->base.u.b = *template;
220 tex->base.u.b.screen = &vs->base;
221 pipe_reference_init(&tex->base.u.b.reference, 1);
222 tex->base.u.vtbl = &virgl_texture_vtbl;
223
224 tex->base.hw_res = vs->vws->resource_create_from_handle(vs->vws, whandle);
225 return &tex->base.u.b;
226 }
227
228 struct pipe_resource *virgl_texture_create(struct virgl_screen *vs,
229 const struct pipe_resource *template)
230 {
231 struct virgl_texture *tex;
232 unsigned vbind;
233
234 tex = CALLOC_STRUCT(virgl_texture);
235 tex->base.clean = TRUE;
236 tex->base.u.b = *template;
237 tex->base.u.b.screen = &vs->base;
238 pipe_reference_init(&tex->base.u.b.reference, 1);
239 tex->base.u.vtbl = &virgl_texture_vtbl;
240 virgl_resource_layout(&tex->base.u.b, &tex->metadata);
241
242 vbind = pipe_to_virgl_bind(template->bind);
243 tex->base.hw_res = vs->vws->resource_create(vs->vws, template->target,
244 template->format, vbind,
245 template->width0,
246 template->height0,
247 template->depth0,
248 template->array_size,
249 template->last_level,
250 template->nr_samples,
251 tex->metadata.total_size);
252 if (!tex->base.hw_res) {
253 FREE(tex);
254 return NULL;
255 }
256 return &tex->base.u.b;
257 }