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_inlines.h"
24#include "util/u_memory.h"
25#include "virgl_context.h"
26#include "virgl_encode.h"
27#include "virtio-gpu/virgl_protocol.h"
28#include "virgl_resource.h"
29
30static struct pipe_stream_output_target *virgl_create_so_target(
31   struct pipe_context *ctx,
32   struct pipe_resource *buffer,
33   unsigned buffer_offset,
34   unsigned buffer_size)
35{
36   struct virgl_context *vctx = virgl_context(ctx);
37   struct virgl_resource *res = virgl_resource(buffer);
38   struct virgl_so_target *t = CALLOC_STRUCT(virgl_so_target);
39   uint32_t handle;
40
41   if (!t)
42      return NULL;
43   handle = virgl_object_assign_handle();
44
45   t->base.reference.count = 1;
46   t->base.context = ctx;
47   pipe_resource_reference(&t->base.buffer, buffer);
48   t->base.buffer_offset = buffer_offset;
49   t->base.buffer_size = buffer_size;
50   t->handle = handle;
51
52   res->bind_history |= PIPE_BIND_STREAM_OUTPUT;
53   util_range_add(&res->b, &res->valid_buffer_range, buffer_offset,
54                  buffer_offset + buffer_size);
55   virgl_resource_dirty(res, 0);
56
57   virgl_encoder_create_so_target(vctx, handle, res, buffer_offset, buffer_size);
58   return &t->base;
59}
60
61static void virgl_destroy_so_target(struct pipe_context *ctx,
62                                   struct pipe_stream_output_target *target)
63{
64   struct virgl_context *vctx = virgl_context(ctx);
65   struct virgl_so_target *t = virgl_so_target(target);
66
67   pipe_resource_reference(&t->base.buffer, NULL);
68   virgl_encode_delete_object(vctx, t->handle, VIRGL_OBJECT_STREAMOUT_TARGET);
69   FREE(t);
70}
71
72static void virgl_set_so_targets(struct pipe_context *ctx,
73                                unsigned num_targets,
74                                struct pipe_stream_output_target **targets,
75                                const unsigned *offset)
76{
77   struct virgl_context *vctx = virgl_context(ctx);
78   int i;
79   for (i = 0; i < num_targets; i++) {
80      if (targets[i]) {
81         struct virgl_winsys *vws = virgl_screen(vctx->base.screen)->vws;
82         struct virgl_resource *res = virgl_resource(targets[i]->buffer);
83
84         pipe_resource_reference(&vctx->so_targets[i].base.buffer, targets[i]->buffer);
85
86         vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
87      } else {
88         pipe_resource_reference(&vctx->so_targets[i].base.buffer, NULL);
89      }
90   }
91   for (i = num_targets; i < vctx->num_so_targets; i++)
92      pipe_resource_reference(&vctx->so_targets[i].base.buffer, NULL);
93   vctx->num_so_targets = num_targets;
94   virgl_encoder_set_so_targets(vctx, num_targets, targets, 0);//append_bitmask);
95}
96
97void virgl_init_so_functions(struct virgl_context *vctx)
98{
99   vctx->base.create_stream_output_target = virgl_create_so_target;
100   vctx->base.stream_output_target_destroy = virgl_destroy_so_target;
101   vctx->base.set_stream_output_targets = virgl_set_so_targets;
102}
103