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 #include "i915_screen.h"
42
43 void
i915_resource_destroy(struct pipe_screen * screen,struct pipe_resource * resource)44 i915_resource_destroy(struct pipe_screen *screen,
45 struct pipe_resource *resource)
46 {
47 if (resource->target == PIPE_BUFFER) {
48 struct i915_buffer *buffer = i915_buffer(resource);
49 if (buffer->free_on_destroy)
50 align_free(buffer->data);
51 FREE(buffer);
52 } else {
53 struct i915_texture *tex = i915_texture(resource);
54 struct i915_winsys *iws = i915_screen(screen)->iws;
55 uint32_t i;
56
57 if (tex->buffer)
58 iws->buffer_destroy(iws, tex->buffer);
59
60 for (i = 0; i < ARRAY_SIZE(tex->image_offset); i++)
61 FREE(tex->image_offset[i]);
62
63 FREE(tex);
64 }
65 }
66
67 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)68 i915_buffer_transfer_map(struct pipe_context *pipe,
69 struct pipe_resource *resource, unsigned level,
70 unsigned usage, const struct pipe_box *box,
71 struct pipe_transfer **ptransfer)
72 {
73 struct i915_context *i915 = i915_context(pipe);
74 struct i915_buffer *buffer = i915_buffer(resource);
75 struct pipe_transfer *transfer = slab_alloc_st(&i915->transfer_pool);
76
77 if (!transfer)
78 return NULL;
79
80 transfer->resource = resource;
81 transfer->level = level;
82 transfer->usage = usage;
83 transfer->box = *box;
84 *ptransfer = transfer;
85
86 return buffer->data + transfer->box.x;
87 }
88
89 void
i915_buffer_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)90 i915_buffer_transfer_unmap(struct pipe_context *pipe,
91 struct pipe_transfer *transfer)
92 {
93 struct i915_context *i915 = i915_context(pipe);
94 slab_free_st(&i915->transfer_pool, transfer);
95 }
96
97 void
i915_buffer_subdata(struct pipe_context * rm_ctx,struct pipe_resource * resource,unsigned usage,unsigned offset,unsigned size,const void * data)98 i915_buffer_subdata(struct pipe_context *rm_ctx, struct pipe_resource *resource,
99 unsigned usage, unsigned offset, unsigned size,
100 const void *data)
101 {
102 struct i915_buffer *buffer = i915_buffer(resource);
103
104 memcpy(buffer->data + offset, data, size);
105 }
106
107 struct pipe_resource *
i915_buffer_create(struct pipe_screen * screen,const struct pipe_resource * template)108 i915_buffer_create(struct pipe_screen *screen,
109 const struct pipe_resource *template)
110 {
111 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
112
113 if (!buf)
114 return NULL;
115
116 buf->b = *template;
117 pipe_reference_init(&buf->b.reference, 1);
118 buf->b.screen = screen;
119 buf->data = align_malloc(template->width0, 64);
120 buf->free_on_destroy = true;
121
122 if (!buf->data)
123 goto err;
124
125 return &buf->b;
126
127 err:
128 FREE(buf);
129 return NULL;
130 }
131
132 struct pipe_resource *
i915_user_buffer_create(struct pipe_screen * screen,void * ptr,unsigned bytes,unsigned bind)133 i915_user_buffer_create(struct pipe_screen *screen, void *ptr, unsigned bytes,
134 unsigned bind)
135 {
136 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
137
138 if (!buf)
139 return NULL;
140
141 pipe_reference_init(&buf->b.reference, 1);
142 buf->b.screen = screen;
143 buf->b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
144 buf->b.usage = PIPE_USAGE_IMMUTABLE;
145 buf->b.bind = bind;
146 buf->b.flags = 0;
147 buf->b.width0 = bytes;
148 buf->b.height0 = 1;
149 buf->b.depth0 = 1;
150 buf->b.array_size = 1;
151
152 buf->data = ptr;
153 buf->free_on_destroy = false;
154
155 return &buf->b;
156 }
157