1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef I915_RESOURCE_H
29 #define I915_RESOURCE_H
30 
31 struct i915_screen;
32 
33 #include "util/u_debug.h"
34 #include "util/u_transfer.h"
35 #include "i915_winsys.h"
36 
37 struct i915_context;
38 struct i915_screen;
39 
40 struct i915_buffer {
41    struct pipe_resource b;
42    uint8_t *data;
43    bool free_on_destroy;
44 };
45 
46 /* Texture transfer. */
47 struct i915_transfer {
48    /* Base class. */
49    struct pipe_transfer b;
50    struct pipe_resource *staging_texture;
51 };
52 
53 #define I915_MAX_TEXTURE_2D_LEVELS 12 /* max 2048x2048 */
54 #define I915_MAX_TEXTURE_3D_LEVELS 9  /* max 256x256x256 */
55 
56 struct offset_pair {
57    unsigned short nblocksx;
58    unsigned short nblocksy;
59 };
60 
61 struct i915_texture {
62    struct pipe_resource b;
63 
64    /* tiling flags */
65    enum i915_winsys_buffer_tile tiling;
66    unsigned stride;
67    unsigned depth_stride; /* per-image on i945? */
68    unsigned total_nblocksy;
69 
70    unsigned nr_images[I915_MAX_TEXTURE_2D_LEVELS];
71 
72    /* Explicitly store the offset of each image for each cube face or
73     * depth value.
74     *
75     * Array [depth] off offsets.
76     */
77    struct offset_pair *image_offset[I915_MAX_TEXTURE_2D_LEVELS];
78 
79    /* The data is held here:
80     */
81    struct i915_winsys_buffer *buffer;
82 };
83 
84 unsigned i915_texture_offset(const struct i915_texture *tex, unsigned level,
85                              unsigned layer);
86 void i915_init_screen_resource_functions(struct i915_screen *is);
87 void i915_init_resource_functions(struct i915_context *i915);
88 
89 static inline struct i915_texture *
i915_texture(struct pipe_resource * resource)90 i915_texture(struct pipe_resource *resource)
91 {
92    struct i915_texture *tex = (struct i915_texture *)resource;
93    assert(tex->b.target != PIPE_BUFFER);
94    return tex;
95 }
96 
97 static inline struct i915_buffer *
i915_buffer(struct pipe_resource * resource)98 i915_buffer(struct pipe_resource *resource)
99 {
100    struct i915_buffer *tex = (struct i915_buffer *)resource;
101    assert(tex->b.target == PIPE_BUFFER);
102    return tex;
103 }
104 
105 struct pipe_resource *i915_texture_create(struct pipe_screen *screen,
106                                           const struct pipe_resource *template,
107                                           bool force_untiled);
108 
109 bool i915_resource_get_handle(struct pipe_screen *screen,
110                               struct pipe_context *context,
111                               struct pipe_resource *texture,
112                               struct winsys_handle *whandle, unsigned usage);
113 
114 struct pipe_resource *
115 i915_texture_from_handle(struct pipe_screen *screen,
116                          const struct pipe_resource *template,
117                          struct winsys_handle *whandle);
118 
119 struct pipe_resource *i915_user_buffer_create(struct pipe_screen *screen,
120                                               void *ptr, unsigned bytes,
121                                               unsigned usage);
122 
123 struct pipe_resource *i915_buffer_create(struct pipe_screen *screen,
124                                          const struct pipe_resource *template);
125 
126 void i915_resource_destroy(struct pipe_screen *screen,
127                            struct pipe_resource *resource);
128 
129 void i915_buffer_subdata(struct pipe_context *rm_ctx,
130                          struct pipe_resource *resource, unsigned usage,
131                          unsigned offset, unsigned size, const void *data);
132 
133 void *i915_buffer_transfer_map(struct pipe_context *pipe,
134                                struct pipe_resource *resource, unsigned level,
135                                unsigned usage, const struct pipe_box *box,
136                                struct pipe_transfer **ptransfer);
137 
138 void i915_buffer_transfer_unmap(struct pipe_context *pipe,
139                                 struct pipe_transfer *transfer);
140 
141 void *i915_texture_transfer_map(struct pipe_context *pipe,
142                                 struct pipe_resource *resource, unsigned level,
143                                 unsigned usage, const struct pipe_box *box,
144                                 struct pipe_transfer **ptransfer);
145 
146 void i915_texture_transfer_unmap(struct pipe_context *pipe,
147                                  struct pipe_transfer *transfer);
148 
149 void i915_texture_subdata(struct pipe_context *pipe,
150                           struct pipe_resource *resource, unsigned level,
151                           unsigned usage, const struct pipe_box *box,
152                           const void *data, unsigned stride,
153                           unsigned layer_stride);
154 
155 #endif /* I915_RESOURCE_H */
156