virgl: add ability to do finer grain dirty tracking
[mesa.git] / src / gallium / drivers / virgl / virgl_buffer.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
24 #include "util/u_inlines.h"
25 #include "util/u_memory.h"
26 #include "virgl_context.h"
27 #include "virgl_resource.h"
28 #include "virgl_screen.h"
29
30 static void *virgl_buffer_transfer_map(struct pipe_context *ctx,
31 struct pipe_resource *resource,
32 unsigned level,
33 unsigned usage,
34 const struct pipe_box *box,
35 struct pipe_transfer **transfer)
36 {
37 struct virgl_context *vctx = virgl_context(ctx);
38 struct virgl_screen *vs = virgl_screen(ctx->screen);
39 struct virgl_resource *vbuf = virgl_resource(resource);
40 struct virgl_transfer *trans;
41 void *ptr;
42 bool readback;
43 bool doflushwait = false;
44
45 if (usage & PIPE_TRANSFER_READ)
46 doflushwait = true;
47 else
48 doflushwait = virgl_res_needs_flush_wait(vctx, vbuf, usage);
49
50 if (doflushwait)
51 ctx->flush(ctx, NULL, 0);
52
53 trans = virgl_resource_create_transfer(ctx, resource, &vbuf->metadata, level,
54 usage, box);
55
56 readback = virgl_res_needs_readback(vctx, vbuf, usage);
57 if (readback)
58 vs->vws->transfer_get(vs->vws, vbuf->hw_res, box, trans->base.stride,
59 trans->l_stride, trans->offset, level);
60
61 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED))
62 doflushwait = true;
63
64 if (doflushwait || readback)
65 vs->vws->resource_wait(vs->vws, vbuf->hw_res);
66
67 ptr = vs->vws->resource_map(vs->vws, vbuf->hw_res);
68 if (!ptr) {
69 return NULL;
70 }
71
72 *transfer = &trans->base;
73 return ptr + trans->offset;
74 }
75
76 static void virgl_buffer_transfer_unmap(struct pipe_context *ctx,
77 struct pipe_transfer *transfer)
78 {
79 struct virgl_context *vctx = virgl_context(ctx);
80 struct virgl_transfer *trans = virgl_transfer(transfer);
81 struct virgl_resource *vbuf = virgl_resource(transfer->resource);
82
83 if (trans->base.usage & PIPE_TRANSFER_WRITE) {
84 struct virgl_screen *vs = virgl_screen(ctx->screen);
85 if (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) {
86 if (trans->range.end <= trans->range.start)
87 goto out;
88
89 transfer->box.x += trans->range.start;
90 transfer->box.width = trans->range.end - trans->range.start;
91 trans->offset = transfer->box.x;
92 }
93
94 vctx->num_transfers++;
95 vs->vws->transfer_put(vs->vws, vbuf->hw_res,
96 &transfer->box, trans->base.stride,
97 trans->l_stride, trans->offset,
98 transfer->level);
99
100 }
101
102 out:
103 virgl_resource_destroy_transfer(vctx, trans);
104 }
105
106 static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx,
107 struct pipe_transfer *transfer,
108 const struct pipe_box *box)
109 {
110 struct virgl_resource *vbuf = virgl_resource(transfer->resource);
111 struct virgl_transfer *trans = virgl_transfer(transfer);
112
113 /*
114 * FIXME: This is not optimal. For example,
115 *
116 * glMapBufferRange(.., 0, 100, GL_MAP_FLUSH_EXPLICIT_BIT)
117 * glFlushMappedBufferRange(.., 25, 30)
118 * glFlushMappedBufferRange(.., 65, 70)
119 *
120 * We'll end up flushing 25 --> 70.
121 */
122 util_range_add(&trans->range, box->x, box->x + box->width);
123 vbuf->clean[0] = FALSE;
124 }
125
126 static const struct u_resource_vtbl virgl_buffer_vtbl =
127 {
128 u_default_resource_get_handle, /* get_handle */
129 virgl_resource_destroy, /* resource_destroy */
130 virgl_buffer_transfer_map, /* transfer_map */
131 virgl_buffer_transfer_flush_region, /* transfer_flush_region */
132 virgl_buffer_transfer_unmap, /* transfer_unmap */
133 };
134
135 void virgl_buffer_init(struct virgl_resource *res)
136 {
137 res->u.vtbl = &virgl_buffer_vtbl;
138 }