Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / i915 / i915_buffer.c
1 /**************************************************************************
2 *
3 * Copyright © 2009 Jakob Bornecrantz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26 #include "util/u_memory.h"
27 #include "i915_screen.h"
28 #include "i915_buffer.h"
29
30 struct intel_buffer;
31
32 struct i915_buffer
33 {
34 struct pipe_buffer base;
35
36 struct intel_buffer *ibuf; /** hw buffer */
37
38 void *data; /**< user and malloc data */
39 boolean own; /**< we own the data incase of malloc */
40 };
41
42 static INLINE struct i915_buffer *
43 i915_buffer(struct pipe_buffer *buffer)
44 {
45 return (struct i915_buffer *)buffer;
46 }
47
48 static struct pipe_buffer *
49 i915_buffer_create(struct pipe_screen *screen,
50 unsigned alignment,
51 unsigned usage,
52 unsigned size)
53 {
54 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
55
56 if (!buf)
57 return NULL;
58
59 pipe_reference_init(&buf->base.reference, 1);
60 buf->base.alignment = alignment;
61 buf->base.screen = screen;
62 buf->base.usage = usage;
63 buf->base.size = size;
64 buf->data = MALLOC(size);
65 buf->own = TRUE;
66
67 if (!buf->data)
68 goto err;
69
70 return &buf->base;
71
72 err:
73 FREE(buf);
74 return NULL;
75 }
76
77 static struct pipe_buffer *
78 i915_user_buffer_create(struct pipe_screen *screen,
79 void *ptr,
80 unsigned bytes)
81 {
82 struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
83
84 if (!buf)
85 return NULL;
86
87 pipe_reference_init(&buf->base.reference, 1);
88 buf->base.alignment = 0;
89 buf->base.screen = screen;
90 buf->base.usage = 0;
91 buf->base.size = bytes;
92 buf->data = ptr;
93 buf->own = FALSE;
94
95 return &buf->base;
96 }
97
98 static void *
99 i915_buffer_map(struct pipe_screen *screen,
100 struct pipe_buffer *buffer,
101 unsigned usage)
102 {
103 struct i915_buffer *buf = i915_buffer(buffer);
104 assert(!buf->ibuf);
105 return buf->data;
106 }
107
108 static void
109 i915_buffer_unmap(struct pipe_screen *screen,
110 struct pipe_buffer *buffer)
111 {
112 struct i915_buffer *buf = i915_buffer(buffer);
113 assert(!buf->ibuf);
114 (void) buf;
115 }
116
117 static void
118 i915_buffer_destroy(struct pipe_buffer *buffer)
119 {
120 struct i915_buffer *buf = i915_buffer(buffer);
121 assert(!buf->ibuf);
122
123 if (buf->own)
124 FREE(buf->data);
125 FREE(buf);
126 }
127
128 void i915_init_screen_buffer_functions(struct i915_screen *screen)
129 {
130 screen->base.buffer_create = i915_buffer_create;
131 screen->base.user_buffer_create = i915_user_buffer_create;
132 screen->base.buffer_map = i915_buffer_map;
133 screen->base.buffer_map_range = NULL;
134 screen->base.buffer_flush_mapped_range = NULL;
135 screen->base.buffer_unmap = i915_buffer_unmap;
136 screen->base.buffer_destroy = i915_buffer_destroy;
137 }