id_objects.c revision d60b2c68552a2729dfdb33c2bac4822453cf8ae2
1/**************************************************************************
2 *
3 * Copyright 2009 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#include "util/u_memory.h"
29
30#include "id_public.h"
31#include "id_screen.h"
32#include "id_objects.h"
33
34struct pipe_buffer *
35identity_buffer_create(struct identity_screen *id_screen,
36                       struct pipe_buffer *buffer)
37{
38   struct identity_buffer *id_buffer;
39
40   if(!buffer)
41      goto error;
42
43   assert(buffer->screen == id_screen->screen);
44
45   id_buffer = CALLOC_STRUCT(identity_buffer);
46   if(!id_buffer)
47      goto error;
48
49   memcpy(&id_buffer->base, buffer, sizeof(struct pipe_buffer));
50
51   pipe_reference_init(&id_buffer->base.reference, 1);
52   id_buffer->base.screen = &id_screen->base;
53   id_buffer->buffer = buffer;
54
55   return &id_buffer->base;
56
57error:
58   pipe_buffer_reference(&buffer, NULL);
59   return NULL;
60}
61
62void
63identity_buffer_destroy(struct identity_buffer *id_buffer)
64{
65   pipe_buffer_reference(&id_buffer->buffer, NULL);
66   FREE(id_buffer);
67}
68
69
70struct pipe_texture *
71identity_texture_create(struct identity_screen *id_screen,
72                        struct pipe_texture *texture)
73{
74   struct identity_texture *id_texture;
75
76   if(!texture)
77      goto error;
78
79   assert(texture->screen == id_screen->screen);
80
81   id_texture = CALLOC_STRUCT(identity_texture);
82   if(!id_texture)
83      goto error;
84
85   memcpy(&id_texture->base, texture, sizeof(struct pipe_texture));
86
87   pipe_reference_init(&id_texture->base.reference, 1);
88   id_texture->base.screen = &id_screen->base;
89   id_texture->texture = texture;
90
91   return &id_texture->base;
92
93error:
94   pipe_texture_reference(&texture, NULL);
95   return NULL;
96}
97
98void
99identity_texture_destroy(struct identity_texture *id_texture)
100{
101   pipe_texture_reference(&id_texture->texture, NULL);
102   FREE(id_texture);
103}
104
105
106struct pipe_surface *
107identity_surface_create(struct identity_texture *id_texture,
108                        struct pipe_surface *surface)
109{
110   struct identity_surface *id_surface;
111
112   if(!surface)
113      goto error;
114
115   assert(surface->texture == id_texture->texture);
116
117   id_surface = CALLOC_STRUCT(identity_surface);
118   if(!id_surface)
119      goto error;
120
121   memcpy(&id_surface->base, surface, sizeof(struct pipe_surface));
122
123   pipe_reference_init(&id_surface->base.reference, 1);
124   id_surface->base.texture = NULL;
125   pipe_texture_reference(&id_surface->base.texture, &id_texture->base);
126   id_surface->surface = surface;
127
128   return &id_surface->base;
129
130error:
131   pipe_surface_reference(&surface, NULL);
132   return NULL;
133}
134
135void
136identity_surface_destroy(struct identity_surface *id_surface)
137{
138   pipe_texture_reference(&id_surface->base.texture, NULL);
139   pipe_surface_reference(&id_surface->surface, NULL);
140   FREE(id_surface);
141}
142
143
144struct pipe_transfer *
145identity_transfer_create(struct identity_texture *id_texture,
146                         struct pipe_transfer *transfer)
147{
148   struct identity_transfer *id_transfer;
149
150   if(!transfer)
151      goto error;
152
153   assert(transfer->texture == id_texture->texture);
154
155   id_transfer = CALLOC_STRUCT(identity_transfer);
156   if(!id_transfer)
157      goto error;
158
159   memcpy(&id_transfer->base, transfer, sizeof(struct pipe_transfer));
160
161   id_transfer->base.texture = NULL;
162   pipe_texture_reference(&id_transfer->base.texture, &id_texture->base);
163   id_transfer->transfer = transfer;
164   assert(id_transfer->base.texture == &id_texture->base);
165
166   return &id_transfer->base;
167
168error:
169   transfer->texture->screen->tex_transfer_destroy(transfer);
170   return NULL;
171}
172
173void
174identity_transfer_destroy(struct identity_transfer *id_transfer)
175{
176   struct identity_screen *id_screen = identity_screen(id_transfer->base.texture->screen);
177   struct pipe_screen *screen = id_screen->screen;
178
179   pipe_texture_reference(&id_transfer->base.texture, NULL);
180   screen->tex_transfer_destroy(id_transfer->transfer);
181   FREE(id_transfer);
182}
183