1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2014, 2015 Red Hat.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include "util/u_inlines.h"
24bf215546Sopenharmony_ci#include "util/u_memory.h"
25bf215546Sopenharmony_ci#include "virgl_context.h"
26bf215546Sopenharmony_ci#include "virgl_encode.h"
27bf215546Sopenharmony_ci#include "virtio-gpu/virgl_protocol.h"
28bf215546Sopenharmony_ci#include "virgl_resource.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistatic struct pipe_stream_output_target *virgl_create_so_target(
31bf215546Sopenharmony_ci   struct pipe_context *ctx,
32bf215546Sopenharmony_ci   struct pipe_resource *buffer,
33bf215546Sopenharmony_ci   unsigned buffer_offset,
34bf215546Sopenharmony_ci   unsigned buffer_size)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
37bf215546Sopenharmony_ci   struct virgl_resource *res = virgl_resource(buffer);
38bf215546Sopenharmony_ci   struct virgl_so_target *t = CALLOC_STRUCT(virgl_so_target);
39bf215546Sopenharmony_ci   uint32_t handle;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   if (!t)
42bf215546Sopenharmony_ci      return NULL;
43bf215546Sopenharmony_ci   handle = virgl_object_assign_handle();
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   t->base.reference.count = 1;
46bf215546Sopenharmony_ci   t->base.context = ctx;
47bf215546Sopenharmony_ci   pipe_resource_reference(&t->base.buffer, buffer);
48bf215546Sopenharmony_ci   t->base.buffer_offset = buffer_offset;
49bf215546Sopenharmony_ci   t->base.buffer_size = buffer_size;
50bf215546Sopenharmony_ci   t->handle = handle;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   res->bind_history |= PIPE_BIND_STREAM_OUTPUT;
53bf215546Sopenharmony_ci   util_range_add(&res->b, &res->valid_buffer_range, buffer_offset,
54bf215546Sopenharmony_ci                  buffer_offset + buffer_size);
55bf215546Sopenharmony_ci   virgl_resource_dirty(res, 0);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   virgl_encoder_create_so_target(vctx, handle, res, buffer_offset, buffer_size);
58bf215546Sopenharmony_ci   return &t->base;
59bf215546Sopenharmony_ci}
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_cistatic void virgl_destroy_so_target(struct pipe_context *ctx,
62bf215546Sopenharmony_ci                                   struct pipe_stream_output_target *target)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
65bf215546Sopenharmony_ci   struct virgl_so_target *t = virgl_so_target(target);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   pipe_resource_reference(&t->base.buffer, NULL);
68bf215546Sopenharmony_ci   virgl_encode_delete_object(vctx, t->handle, VIRGL_OBJECT_STREAMOUT_TARGET);
69bf215546Sopenharmony_ci   FREE(t);
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistatic void virgl_set_so_targets(struct pipe_context *ctx,
73bf215546Sopenharmony_ci                                unsigned num_targets,
74bf215546Sopenharmony_ci                                struct pipe_stream_output_target **targets,
75bf215546Sopenharmony_ci                                const unsigned *offset)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
78bf215546Sopenharmony_ci   int i;
79bf215546Sopenharmony_ci   for (i = 0; i < num_targets; i++) {
80bf215546Sopenharmony_ci      if (targets[i]) {
81bf215546Sopenharmony_ci         struct virgl_winsys *vws = virgl_screen(vctx->base.screen)->vws;
82bf215546Sopenharmony_ci         struct virgl_resource *res = virgl_resource(targets[i]->buffer);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci         pipe_resource_reference(&vctx->so_targets[i].base.buffer, targets[i]->buffer);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci         vws->emit_res(vws, vctx->cbuf, res->hw_res, FALSE);
87bf215546Sopenharmony_ci      } else {
88bf215546Sopenharmony_ci         pipe_resource_reference(&vctx->so_targets[i].base.buffer, NULL);
89bf215546Sopenharmony_ci      }
90bf215546Sopenharmony_ci   }
91bf215546Sopenharmony_ci   for (i = num_targets; i < vctx->num_so_targets; i++)
92bf215546Sopenharmony_ci      pipe_resource_reference(&vctx->so_targets[i].base.buffer, NULL);
93bf215546Sopenharmony_ci   vctx->num_so_targets = num_targets;
94bf215546Sopenharmony_ci   virgl_encoder_set_so_targets(vctx, num_targets, targets, 0);//append_bitmask);
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid virgl_init_so_functions(struct virgl_context *vctx)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   vctx->base.create_stream_output_target = virgl_create_so_target;
100bf215546Sopenharmony_ci   vctx->base.stream_output_target_destroy = virgl_destroy_so_target;
101bf215546Sopenharmony_ci   vctx->base.set_stream_output_targets = virgl_set_so_targets;
102bf215546Sopenharmony_ci}
103