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_inlines.h"
29#include "util/u_memory.h"
30
31#include "glhd_screen.h"
32#include "glhd_objects.h"
33#include "glhd_context.h"
34
35
36
37struct pipe_resource *
38galahad_resource_create(struct galahad_screen *glhd_screen,
39                        struct pipe_resource *resource)
40{
41   struct galahad_resource *glhd_resource;
42
43   if(!resource)
44      goto error;
45
46   assert(resource->screen == glhd_screen->screen);
47
48   glhd_resource = CALLOC_STRUCT(galahad_resource);
49   if(!glhd_resource)
50      goto error;
51
52   memcpy(&glhd_resource->base, resource, sizeof(struct pipe_resource));
53
54   pipe_reference_init(&glhd_resource->base.reference, 1);
55   glhd_resource->base.screen = &glhd_screen->base;
56   glhd_resource->resource = resource;
57
58   return &glhd_resource->base;
59
60error:
61   pipe_resource_reference(&resource, NULL);
62   return NULL;
63}
64
65void
66galahad_resource_destroy(struct galahad_resource *glhd_resource)
67{
68   pipe_resource_reference(&glhd_resource->resource, NULL);
69   FREE(glhd_resource);
70}
71
72
73struct pipe_surface *
74galahad_surface_create(struct galahad_context *glhd_context,
75                        struct galahad_resource *glhd_resource,
76                        struct pipe_surface *surface)
77{
78   struct galahad_surface *glhd_surface;
79
80   if(!surface)
81      goto error;
82
83   assert(surface->texture == glhd_resource->resource);
84
85   glhd_surface = CALLOC_STRUCT(galahad_surface);
86   if(!glhd_surface)
87      goto error;
88
89   memcpy(&glhd_surface->base, surface, sizeof(struct pipe_surface));
90
91   pipe_reference_init(&glhd_surface->base.reference, 1);
92   glhd_surface->base.texture = NULL;
93   pipe_resource_reference(&glhd_surface->base.texture, &glhd_resource->base);
94   glhd_surface->surface = surface;
95
96   return &glhd_surface->base;
97
98error:
99   pipe_surface_reference(&surface, NULL);
100   return NULL;
101}
102
103void
104galahad_surface_destroy(struct galahad_context *glhd_context,
105                         struct galahad_surface *glhd_surface)
106{
107   pipe_resource_reference(&glhd_surface->base.texture, NULL);
108   pipe_surface_reference(&glhd_surface->surface, NULL);
109   FREE(glhd_surface);
110}
111
112
113struct pipe_sampler_view *
114galahad_sampler_view_create(struct galahad_context *glhd_context,
115                             struct galahad_resource *glhd_resource,
116                             struct pipe_sampler_view *view)
117{
118   struct galahad_sampler_view *glhd_view;
119
120   if (!view)
121      goto error;
122
123   assert(view->texture == glhd_resource->resource);
124
125   glhd_view = CALLOC_STRUCT(galahad_sampler_view);
126
127   glhd_view->base = *view;
128   glhd_view->base.reference.count = 1;
129   glhd_view->base.texture = NULL;
130   pipe_resource_reference(&glhd_view->base.texture, &glhd_resource->base);
131   glhd_view->base.context = &glhd_context->base;
132   glhd_view->sampler_view = view;
133
134   return &glhd_view->base;
135error:
136   return NULL;
137}
138
139void
140galahad_sampler_view_destroy(struct galahad_context *glhd_context,
141                              struct galahad_sampler_view *glhd_view)
142{
143   pipe_sampler_view_reference(&glhd_view->sampler_view, NULL);
144   pipe_resource_reference(&glhd_view->base.texture, NULL);
145   FREE(glhd_view);
146}
147
148
149struct pipe_transfer *
150galahad_transfer_create(struct galahad_context *glhd_context,
151                         struct galahad_resource *glhd_resource,
152                         struct pipe_transfer *transfer)
153{
154   struct galahad_transfer *glhd_transfer;
155
156   if(!transfer)
157      goto error;
158
159   assert(transfer->resource == glhd_resource->resource);
160
161   glhd_transfer = CALLOC_STRUCT(galahad_transfer);
162   if(!glhd_transfer)
163      goto error;
164
165   memcpy(&glhd_transfer->base, transfer, sizeof(struct pipe_transfer));
166
167   glhd_transfer->base.resource = NULL;
168   glhd_transfer->transfer = transfer;
169
170   pipe_resource_reference(&glhd_transfer->base.resource, &glhd_resource->base);
171   assert(glhd_transfer->base.resource == &glhd_resource->base);
172
173   return &glhd_transfer->base;
174
175error:
176   glhd_context->pipe->transfer_destroy(glhd_context->pipe, transfer);
177   return NULL;
178}
179
180void
181galahad_transfer_destroy(struct galahad_context *glhd_context,
182                          struct galahad_transfer *glhd_transfer)
183{
184   pipe_resource_reference(&glhd_transfer->base.resource, NULL);
185   glhd_context->pipe->transfer_destroy(glhd_context->pipe,
186                                        glhd_transfer->transfer);
187   FREE(glhd_transfer);
188}
189