1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2014, 2015 Red Hat.
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9b8e80941Smrg * the Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#ifndef VIRGL_RESOURCE_H
25b8e80941Smrg#define VIRGL_RESOURCE_H
26b8e80941Smrg
27b8e80941Smrg#include "util/u_resource.h"
28b8e80941Smrg#include "util/u_range.h"
29b8e80941Smrg#include "util/list.h"
30b8e80941Smrg#include "util/u_transfer.h"
31b8e80941Smrg
32b8e80941Smrg#include "virgl_hw.h"
33b8e80941Smrg#include "virgl_screen.h"
34b8e80941Smrg#define VR_MAX_TEXTURE_2D_LEVELS 15
35b8e80941Smrg
36b8e80941Smrgstruct winsys_handle;
37b8e80941Smrgstruct virgl_screen;
38b8e80941Smrgstruct virgl_context;
39b8e80941Smrg
40b8e80941Smrgstruct virgl_resource_metadata
41b8e80941Smrg{
42b8e80941Smrg   unsigned long level_offset[VR_MAX_TEXTURE_2D_LEVELS];
43b8e80941Smrg   unsigned stride[VR_MAX_TEXTURE_2D_LEVELS];
44b8e80941Smrg   unsigned layer_stride[VR_MAX_TEXTURE_2D_LEVELS];
45b8e80941Smrg   uint32_t total_size;
46b8e80941Smrg};
47b8e80941Smrg
48b8e80941Smrgstruct virgl_resource {
49b8e80941Smrg   struct u_resource u;
50b8e80941Smrg   uint16_t clean_mask;
51b8e80941Smrg   struct virgl_hw_res *hw_res;
52b8e80941Smrg   struct virgl_resource_metadata metadata;
53b8e80941Smrg};
54b8e80941Smrg
55b8e80941Smrgstruct virgl_transfer {
56b8e80941Smrg   struct pipe_transfer base;
57b8e80941Smrg   uint32_t offset, l_stride;
58b8e80941Smrg   struct util_range range;
59b8e80941Smrg   struct list_head queue_link;
60b8e80941Smrg   struct pipe_transfer *resolve_transfer;
61b8e80941Smrg   void *hw_res_map;
62b8e80941Smrg};
63b8e80941Smrg
64b8e80941Smrgvoid virgl_resource_destroy(struct pipe_screen *screen,
65b8e80941Smrg                            struct pipe_resource *resource);
66b8e80941Smrg
67b8e80941Smrgvoid virgl_init_screen_resource_functions(struct pipe_screen *screen);
68b8e80941Smrg
69b8e80941Smrgvoid virgl_init_context_resource_functions(struct pipe_context *ctx);
70b8e80941Smrg
71b8e80941Smrgvoid virgl_texture_init(struct virgl_resource *res);
72b8e80941Smrg
73b8e80941Smrgstatic inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
74b8e80941Smrg{
75b8e80941Smrg   return (struct virgl_resource *)r;
76b8e80941Smrg}
77b8e80941Smrg
78b8e80941Smrgstatic inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
79b8e80941Smrg{
80b8e80941Smrg   return (struct virgl_transfer *)trans;
81b8e80941Smrg}
82b8e80941Smrg
83b8e80941Smrgvoid virgl_buffer_init(struct virgl_resource *res);
84b8e80941Smrg
85b8e80941Smrgstatic inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs, unsigned pbind)
86b8e80941Smrg{
87b8e80941Smrg   unsigned outbind = 0;
88b8e80941Smrg   if (pbind & PIPE_BIND_DEPTH_STENCIL)
89b8e80941Smrg      outbind |= VIRGL_BIND_DEPTH_STENCIL;
90b8e80941Smrg   if (pbind & PIPE_BIND_RENDER_TARGET)
91b8e80941Smrg      outbind |= VIRGL_BIND_RENDER_TARGET;
92b8e80941Smrg   if (pbind & PIPE_BIND_SAMPLER_VIEW)
93b8e80941Smrg      outbind |= VIRGL_BIND_SAMPLER_VIEW;
94b8e80941Smrg   if (pbind & PIPE_BIND_VERTEX_BUFFER)
95b8e80941Smrg      outbind |= VIRGL_BIND_VERTEX_BUFFER;
96b8e80941Smrg   if (pbind & PIPE_BIND_INDEX_BUFFER)
97b8e80941Smrg      outbind |= VIRGL_BIND_INDEX_BUFFER;
98b8e80941Smrg   if (pbind & PIPE_BIND_CONSTANT_BUFFER)
99b8e80941Smrg      outbind |= VIRGL_BIND_CONSTANT_BUFFER;
100b8e80941Smrg   if (pbind & PIPE_BIND_DISPLAY_TARGET)
101b8e80941Smrg      outbind |= VIRGL_BIND_DISPLAY_TARGET;
102b8e80941Smrg   if (pbind & PIPE_BIND_STREAM_OUTPUT)
103b8e80941Smrg      outbind |= VIRGL_BIND_STREAM_OUTPUT;
104b8e80941Smrg   if (pbind & PIPE_BIND_CURSOR)
105b8e80941Smrg      outbind |= VIRGL_BIND_CURSOR;
106b8e80941Smrg   if (pbind & PIPE_BIND_CUSTOM)
107b8e80941Smrg      outbind |= VIRGL_BIND_CUSTOM;
108b8e80941Smrg   if (pbind & PIPE_BIND_SCANOUT)
109b8e80941Smrg      outbind |= VIRGL_BIND_SCANOUT;
110b8e80941Smrg   if (pbind & PIPE_BIND_SHADER_BUFFER)
111b8e80941Smrg      outbind |= VIRGL_BIND_SHADER_BUFFER;
112b8e80941Smrg   if (pbind & PIPE_BIND_QUERY_BUFFER)
113b8e80941Smrg      outbind |= VIRGL_BIND_QUERY_BUFFER;
114b8e80941Smrg   if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
115b8e80941Smrg      if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
116b8e80941Smrg         outbind |= VIRGL_BIND_COMMAND_ARGS;
117b8e80941Smrg   return outbind;
118b8e80941Smrg}
119b8e80941Smrg
120b8e80941Smrgbool virgl_res_needs_flush(struct virgl_context *vctx,
121b8e80941Smrg                           struct virgl_transfer *transfer);
122b8e80941Smrgbool virgl_res_needs_readback(struct virgl_context *vctx,
123b8e80941Smrg                              struct virgl_resource *res,
124b8e80941Smrg                              unsigned usage, unsigned level);
125b8e80941Smrg
126b8e80941Smrgvoid virgl_resource_layout(struct pipe_resource *pt,
127b8e80941Smrg                           struct virgl_resource_metadata *metadata);
128b8e80941Smrg
129b8e80941Smrgstruct virgl_transfer *
130b8e80941Smrgvirgl_resource_create_transfer(struct slab_child_pool *pool,
131b8e80941Smrg                               struct pipe_resource *pres,
132b8e80941Smrg                               const struct virgl_resource_metadata *metadata,
133b8e80941Smrg                               unsigned level, unsigned usage,
134b8e80941Smrg                               const struct pipe_box *box);
135b8e80941Smrg
136b8e80941Smrgvoid virgl_resource_destroy_transfer(struct slab_child_pool *pool,
137b8e80941Smrg                                     struct virgl_transfer *trans);
138b8e80941Smrg
139b8e80941Smrgvoid virgl_resource_destroy(struct pipe_screen *screen,
140b8e80941Smrg                            struct pipe_resource *resource);
141b8e80941Smrg
142b8e80941Smrgboolean virgl_resource_get_handle(struct pipe_screen *screen,
143b8e80941Smrg                                  struct pipe_resource *resource,
144b8e80941Smrg                                  struct winsys_handle *whandle);
145b8e80941Smrg
146b8e80941Smrgvoid virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
147b8e80941Smrg
148b8e80941Smrg#endif
149