1 /**************************************************************************
2 *
3 * Copyright 2006 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 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 * Michel Dänzer <daenzer@vmware.com>
31 */
32
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38
39 #include "i915_context.h"
40 #include "i915_resource.h"
41
42
43
44 static boolean
i915_buffer_get_handle(struct pipe_screen * screen,struct pipe_resource * resource,struct winsys_handle * handle)45 i915_buffer_get_handle(struct pipe_screen *screen,
46 struct pipe_resource *resource,
47 struct winsys_handle *handle)
48 {
49 return FALSE;
50 }
51
52 static void
i915_buffer_destroy(struct pipe_screen * screen,struct pipe_resource * resource)53 i915_buffer_destroy(struct pipe_screen *screen,
54 struct pipe_resource *resource)
55 {
56 struct i915_buffer *buffer = i915_buffer(resource);
57 if (buffer->free_on_destroy)
58 align_free(buffer->data);
59 FREE(buffer);
60 }
61
62
63 static void *
i915_buffer_transfer_map(struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)64 i915_buffer_transfer_map(struct pipe_context *pipe,
65 struct pipe_resource *resource,
66 unsigned level,
67 unsigned usage,
68 const struct pipe_box *box,
69 struct pipe_transfer **ptransfer)
70 {
71 struct i915_context *i915 = i915_context(pipe);
72 struct i915_buffer *buffer = i915_buffer(resource);
73 struct pipe_transfer *transfer = slab_alloc_st(&i915->transfer_pool);
74
75 if (!transfer)
76 return NULL;
77
78 transfer->resource = resource;
79 transfer->level = level;
80 transfer->usage = usage;
81 transfer->box = *box;
82 *ptransfer = transfer;
83
84 return buffer->data + transfer->box.x;
85 }
86
87 static void
i915_buffer_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)88 i915_buffer_transfer_unmap(struct pipe_context *pipe,
89 struct pipe_transfer *transfer)
90 {
91 struct i915_context *i915 = i915_context(pipe);
92 slab_free_st(&i915->transfer_pool, transfer);
93 }
94
95 void
i915_buffer_subdata(struct pipe_context * rm_ctx,struct pipe_resource * resource,unsigned usage,unsigned offset,unsigned size,const void * data)96 i915_buffer_subdata(struct pipe_context *rm_ctx,
97 struct pipe_resource *resource,
98 unsigned usage, unsigned offset,
99 unsigned size, const void *data)
100 {
101 struct i915_buffer *buffer = i915_buffer(resource);
102
103 memcpy(buffer->data + offset, data, size);
104 }
105
106
107 struct u_resource_vtbl i915_buffer_vtbl =
108 {
109 i915_buffer_get_handle, /* get_handle */
110 i915_buffer_destroy, /* resource_destroy */
111 i915_buffer_transfer_map, /* transfer_map */
112 u_default_transfer_flush_region, /* transfer_flush_region */
113 i915_buffer_transfer_unmap, /* transfer_unmap */
114 };
115
116
117
118 struct pipe_resource *
i915_buffer_create(struct pipe_screen * screen,const struct pipe_resource * template)119 i915_buffer_create(struct pipe_screen *screen,
120 const struct pipe_resource *template)
121 {
122 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
123
124 if (!buf)
125 return NULL;
126
127 buf->b.b = *template;
128 buf->b.vtbl = &i915_buffer_vtbl;
129 pipe_reference_init(&buf->b.b.reference, 1);
130 buf->b.b.screen = screen;
131 buf->data = align_malloc(template->width0, 64);
132 buf->free_on_destroy = TRUE;
133
134 if (!buf->data)
135 goto err;
136
137 return &buf->b.b;
138
139 err:
140 FREE(buf);
141 return NULL;
142 }
143
144
145
146 struct pipe_resource *
i915_user_buffer_create(struct pipe_screen * screen,void * ptr,unsigned bytes,unsigned bind)147 i915_user_buffer_create(struct pipe_screen *screen,
148 void *ptr,
149 unsigned bytes,
150 unsigned bind)
151 {
152 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
153
154 if (!buf)
155 return NULL;
156
157 pipe_reference_init(&buf->b.b.reference, 1);
158 buf->b.vtbl = &i915_buffer_vtbl;
159 buf->b.b.screen = screen;
160 buf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
161 buf->b.b.usage = PIPE_USAGE_IMMUTABLE;
162 buf->b.b.bind = bind;
163 buf->b.b.flags = 0;
164 buf->b.b.width0 = bytes;
165 buf->b.b.height0 = 1;
166 buf->b.b.depth0 = 1;
167 buf->b.b.array_size = 1;
168
169 buf->data = ptr;
170 buf->free_on_destroy = FALSE;
171
172 return &buf->b.b;
173 }
174