1 /**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 *********************************************************
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 #include "xa_context.h"
31 #include "cso_cache/cso_context.h"
32 #include "util/u_inlines.h"
33 #include "util/u_rect.h"
34 #include "pipe/p_context.h"
38 xa_context_default(struct xa_tracker
*xa
)
40 return xa
->default_ctx
;
44 xa_context_create(struct xa_tracker
*xa
)
46 struct xa_context
*ctx
= calloc(1, sizeof(*ctx
));
49 ctx
->pipe
= xa
->screen
->context_create(xa
->screen
, NULL
);
50 ctx
->cso
= cso_create_context(ctx
->pipe
);
51 ctx
->shaders
= xa_shaders_create(ctx
);
52 renderer_init_state(ctx
);
58 xa_context_destroy(struct xa_context
*r
)
60 struct pipe_resource
**vsbuf
= &r
->vs_const_buffer
;
61 struct pipe_resource
**fsbuf
= &r
->fs_const_buffer
;
64 pipe_resource_reference(vsbuf
, NULL
);
67 pipe_resource_reference(fsbuf
, NULL
);
70 xa_shaders_destroy(r
->shaders
);
75 cso_release_all(r
->cso
);
76 cso_destroy_context(r
->cso
);
82 xa_surface_dma(struct xa_context
*ctx
,
83 struct xa_surface
*srf
,
86 int to_surface
, struct xa_box
*boxes
, unsigned int num_boxes
)
88 struct pipe_transfer
*transfer
;
91 enum pipe_transfer_usage transfer_direction
;
92 struct pipe_context
*pipe
= ctx
->pipe
;
94 transfer_direction
= (to_surface
? PIPE_TRANSFER_WRITE
:
97 for (i
= 0; i
< num_boxes
; ++i
, ++boxes
) {
98 w
= boxes
->x2
- boxes
->x1
;
99 h
= boxes
->y2
- boxes
->y1
;
101 transfer
= pipe_get_transfer(pipe
, srf
->tex
, 0, 0,
102 transfer_direction
, boxes
->x1
, boxes
->y1
,
105 return -XA_ERR_NORES
;
107 map
= pipe_transfer_map(ctx
->pipe
, transfer
);
112 util_copy_rect(map
, srf
->tex
->format
, transfer
->stride
,
113 0, 0, w
, h
, data
, pitch
, boxes
->x1
, boxes
->y1
);
115 util_copy_rect(data
, srf
->tex
->format
, pitch
,
116 boxes
->x1
, boxes
->y1
, w
, h
, map
, transfer
->stride
, 0,
119 pipe
->transfer_unmap(pipe
, transfer
);
120 pipe
->transfer_destroy(pipe
, transfer
);
122 pipe
->flush(pipe
, &ctx
->last_fence
);
126 pipe
->transfer_destroy(pipe
, transfer
);
127 return -XA_ERR_NORES
;
131 xa_surface_map(struct xa_context
*ctx
,
132 struct xa_surface
*srf
, unsigned int usage
)
135 unsigned int transfer_direction
= 0;
136 struct pipe_context
*pipe
= ctx
->pipe
;
141 if (usage
& XA_MAP_READ
)
142 transfer_direction
= PIPE_TRANSFER_READ
;
143 if (usage
& XA_MAP_WRITE
)
144 transfer_direction
= PIPE_TRANSFER_WRITE
;
146 if (!transfer_direction
)
149 srf
->transfer
= pipe_get_transfer(pipe
, srf
->tex
, 0, 0,
150 transfer_direction
, 0, 0,
151 srf
->tex
->width0
, srf
->tex
->height0
);
155 map
= pipe_transfer_map(pipe
, srf
->transfer
);
157 pipe
->transfer_destroy(pipe
, srf
->transfer
);
159 srf
->mapping_pipe
= pipe
;
164 xa_surface_unmap(struct xa_surface
*srf
)
167 struct pipe_context
*pipe
= srf
->mapping_pipe
;
169 pipe
->transfer_unmap(pipe
, srf
->transfer
);
170 pipe
->transfer_destroy(pipe
, srf
->transfer
);
171 srf
->transfer
= NULL
;
176 xa_copy_prepare(struct xa_context
*ctx
,
177 struct xa_surface
*dst
, struct xa_surface
*src
)
179 if (src
== dst
|| src
->tex
->format
!= dst
->tex
->format
)
180 return -XA_ERR_INVAL
;
189 xa_copy(struct xa_context
*ctx
,
190 int dx
, int dy
, int sx
, int sy
, int width
, int height
)
192 struct pipe_box src_box
;
194 u_box_2d(sx
, sy
, width
, height
, &src_box
);
195 ctx
->pipe
->resource_copy_region(ctx
->pipe
,
196 ctx
->dst
->tex
, 0, dx
, dy
, 0, ctx
->src
->tex
,
202 xa_copy_done(struct xa_context
*ctx
)
204 ctx
->pipe
->flush(ctx
->pipe
, &ctx
->last_fence
);
208 xa_fence_get(struct xa_context
*ctx
)
210 struct xa_fence
*fence
= malloc(sizeof(*fence
));
211 struct pipe_screen
*screen
= ctx
->xa
->screen
;
218 if (ctx
->last_fence
== NULL
)
219 fence
->pipe_fence
= NULL
;
221 screen
->fence_reference(screen
, &fence
->pipe_fence
, ctx
->last_fence
);
227 xa_fence_wait(struct xa_fence
*fence
, uint64_t timeout
)
232 if (fence
->pipe_fence
) {
233 struct pipe_screen
*screen
= fence
->xa
->screen
;
236 timed_out
= !screen
->fence_finish(screen
, fence
->pipe_fence
, timeout
);
240 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);
246 xa_fence_destroy(struct xa_fence
*fence
)
251 if (fence
->pipe_fence
) {
252 struct pipe_screen
*screen
= fence
->xa
->screen
;
254 screen
->fence_reference(screen
, &fence
->pipe_fence
, NULL
);