1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/**************************************************************************
*
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* \file
* Drop-in implementation of the winsys driver functions for buffer handles.
*
* \author José Fonseca <jrfonseca@tungstengraphics.com>
*/
#include <assert.h>
#include <stdlib.h>
#include "pipe/p_winsys.h"
#include "pipe/p_defines.h"
#include "pb_buffer.h"
#include "pb_buffer_handle.h"
static struct pipe_buffer_handle *
buffer_handle_create(struct pipe_winsys *winsys,
unsigned alignment,
unsigned flags,
unsigned hint)
{
struct pipe_buffer_handle *handle;
handle = (struct pipe_buffer_handle *)malloc(sizeof(struct pipe_buffer_handle));
if(!handle)
return NULL;
handle->refcount = 1;
handle->alignment = alignment;
handle->flags = flags;
handle->hint = hint;
handle->buf = &null_buffer;
return handle;
}
static struct pipe_buffer_handle *
buffer_handle_create_user(struct pipe_winsys *winsys,
void *data, unsigned size)
{
struct pipe_buffer_handle *handle;
struct pipe_buffer *buf;
handle = buffer_handle_create(winsys, 1, 0, 0);
if(!handle)
return NULL;
buf = client_buffer_create(data);
if(!buf) {
free(handle);
return NULL;
}
buffer_handle_data(handle, buf);
return handle;
}
static void *
buffer_handle_map(struct pipe_winsys *winsys,
struct pipe_buffer_handle *handle,
unsigned flags)
{
return buffer_map(handle->buf, flags);
}
static void
buffer_handle_unmap(struct pipe_winsys *winsys,
struct pipe_buffer_handle *handle)
{
buffer_unmap(handle->buf);
}
static void
buffer_handle_reference(struct pipe_winsys *winsys,
struct pipe_buffer_handle **dst,
struct pipe_buffer_handle *src)
{
/* XXX: should this be thread safe? */
if (src) {
src->refcount++;
}
if (*dst) {
(*dst)->refcount--;
if ((*dst)->refcount == 0) {
buffer_release((*dst)->buf);
free(*dst);
}
}
*dst = src;
}
static int
buffer_handle_subdata(struct pipe_winsys *winsys,
struct pipe_buffer_handle *handle,
unsigned long offset,
unsigned long size,
const void *data)
{
void *map;
assert(handle);
assert(data);
map = buffer_handle_map(winsys, handle, PIPE_BUFFER_FLAG_WRITE);
if(map) {
memcpy((char *)map + offset, data, size);
buffer_handle_unmap(winsys, handle);
return 0;
}
return -1;
}
static int
buffer_handle_get_subdata(struct pipe_winsys *winsys,
struct pipe_buffer_handle *handle,
unsigned long offset,
unsigned long size,
void *data)
{
void *map;
assert(handle);
assert(data);
map = buffer_handle_map(winsys, handle, PIPE_BUFFER_FLAG_READ);
if(map) {
memcpy(data, (char *)map + offset, size);
buffer_handle_unmap(winsys, handle);
return 0;
}
return -1;
}
void
buffer_handle_init_winsys(struct pipe_winsys *winsys)
{
winsys->buffer_create = buffer_handle_create;
winsys->user_buffer_create = buffer_handle_create_user;
winsys->buffer_map = buffer_handle_map;
winsys->buffer_unmap = buffer_handle_unmap;
winsys->buffer_reference = buffer_handle_reference;
winsys->buffer_subdata = buffer_handle_subdata;
winsys->buffer_get_subdata = buffer_handle_get_subdata;
}
|