1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
28 #include <pipe/p_video_context.h>
30 #include <util/u_memory.h>
31 #include <util/u_rect.h>
32 #include <util/u_video.h>
34 #include "vl_context.h"
35 #include "vl_compositor.h"
36 #include "vl_mpeg12_decoder.h"
39 vl_context_destroy(struct pipe_video_context
*context
)
41 struct vl_context
*ctx
= (struct vl_context
*)context
;
48 static struct pipe_surface
*
49 vl_context_create_surface(struct pipe_video_context
*context
,
50 struct pipe_resource
*resource
,
51 const struct pipe_surface
*templ
)
53 struct vl_context
*ctx
= (struct vl_context
*)context
;
57 return ctx
->pipe
->create_surface(ctx
->pipe
, resource
, templ
);
60 static struct pipe_sampler_view
*
61 vl_context_create_sampler_view(struct pipe_video_context
*context
,
62 struct pipe_resource
*resource
,
63 const struct pipe_sampler_view
*templ
)
65 struct vl_context
*ctx
= (struct vl_context
*)context
;
69 return ctx
->pipe
->create_sampler_view(ctx
->pipe
, resource
, templ
);
73 vl_context_upload_sampler(struct pipe_video_context
*context
,
74 struct pipe_sampler_view
*dst
,
75 const struct pipe_box
*dst_box
,
76 const void *src
, unsigned src_stride
,
77 unsigned src_x
, unsigned src_y
)
79 struct vl_context
*ctx
= (struct vl_context
*)context
;
80 struct pipe_transfer
*transfer
;
88 transfer
= ctx
->pipe
->get_transfer(ctx
->pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, dst_box
);
92 map
= ctx
->pipe
->transfer_map(ctx
->pipe
, transfer
);
96 util_copy_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
97 dst_box
->width
, dst_box
->height
,
98 src
, src_stride
, src_x
, src_y
);
100 ctx
->pipe
->transfer_unmap(ctx
->pipe
, transfer
);
103 ctx
->pipe
->transfer_destroy(ctx
->pipe
, transfer
);
107 vl_context_clear_sampler(struct pipe_video_context
*context
,
108 struct pipe_sampler_view
*dst
,
109 const struct pipe_box
*dst_box
,
112 struct vl_context
*ctx
= (struct vl_context
*)context
;
113 struct pipe_transfer
*transfer
;
123 transfer
= ctx
->pipe
->get_transfer(ctx
->pipe
, dst
->texture
, 0, PIPE_TRANSFER_WRITE
, dst_box
);
127 map
= ctx
->pipe
->transfer_map(ctx
->pipe
, transfer
);
131 for ( i
= 0; i
< 4; ++i
)
134 util_fill_rect(map
, dst
->texture
->format
, transfer
->stride
, 0, 0,
135 dst_box
->width
, dst_box
->height
, &uc
);
137 ctx
->pipe
->transfer_unmap(ctx
->pipe
, transfer
);
140 ctx
->pipe
->transfer_destroy(ctx
->pipe
, transfer
);
143 static struct pipe_video_decoder
*
144 vl_context_create_decoder(struct pipe_video_context
*context
,
145 enum pipe_video_profile profile
,
146 enum pipe_video_entrypoint entrypoint
,
147 enum pipe_video_chroma_format chroma_format
,
148 unsigned width
, unsigned height
)
150 struct vl_context
*ctx
= (struct vl_context
*)context
;
151 unsigned buffer_width
, buffer_height
;
155 assert(width
> 0 && height
> 0);
157 pot_buffers
= !ctx
->base
.screen
->get_video_param(ctx
->base
.screen
, profile
, PIPE_VIDEO_CAP_NPOT_TEXTURES
);
159 buffer_width
= pot_buffers
? util_next_power_of_two(width
) : align(width
, MACROBLOCK_WIDTH
);
160 buffer_height
= pot_buffers
? util_next_power_of_two(height
) : align(height
, MACROBLOCK_HEIGHT
);
162 switch (u_reduce_video_profile(profile
)) {
163 case PIPE_VIDEO_CODEC_MPEG12
:
164 return vl_create_mpeg12_decoder(context
, ctx
->pipe
, profile
, entrypoint
,
165 chroma_format
, buffer_width
, buffer_height
);
172 static struct pipe_video_buffer
*
173 vl_context_create_buffer(struct pipe_video_context
*context
,
174 enum pipe_format buffer_format
,
175 enum pipe_video_chroma_format chroma_format
,
176 unsigned width
, unsigned height
)
178 struct vl_context
*ctx
= (struct vl_context
*)context
;
179 const enum pipe_format
*resource_formats
;
180 struct pipe_video_buffer
*result
;
181 unsigned buffer_width
, buffer_height
;
185 assert(width
> 0 && height
> 0);
187 pot_buffers
= !ctx
->base
.screen
->get_video_param
190 PIPE_VIDEO_PROFILE_UNKNOWN
,
191 PIPE_VIDEO_CAP_NPOT_TEXTURES
194 resource_formats
= vl_video_buffer_formats(ctx
->pipe
->screen
, buffer_format
);
195 if (!resource_formats
)
198 buffer_width
= pot_buffers
? util_next_power_of_two(width
) : align(width
, MACROBLOCK_WIDTH
);
199 buffer_height
= pot_buffers
? util_next_power_of_two(height
) : align(height
, MACROBLOCK_HEIGHT
);
201 result
= vl_video_buffer_init(context
, ctx
->pipe
,
202 buffer_width
, buffer_height
, 1,
206 if (result
) // TODO move format handling into vl_video_buffer
207 result
->buffer_format
= buffer_format
;
212 static struct pipe_video_compositor
*
213 vl_context_create_compositor(struct pipe_video_context
*context
)
215 struct vl_context
*ctx
= (struct vl_context
*)context
;
219 return vl_compositor_init(context
, ctx
->pipe
);
222 struct pipe_video_context
*
223 vl_create_context(struct pipe_context
*pipe
)
225 struct vl_context
*ctx
;
227 ctx
= CALLOC_STRUCT(vl_context
);
232 ctx
->base
.screen
= pipe
->screen
;
234 ctx
->base
.destroy
= vl_context_destroy
;
235 ctx
->base
.create_surface
= vl_context_create_surface
;
236 ctx
->base
.create_sampler_view
= vl_context_create_sampler_view
;
237 ctx
->base
.clear_sampler
= vl_context_clear_sampler
;
238 ctx
->base
.upload_sampler
= vl_context_upload_sampler
;
239 ctx
->base
.create_decoder
= vl_context_create_decoder
;
240 ctx
->base
.create_buffer
= vl_context_create_buffer
;
241 ctx
->base
.create_compositor
= vl_context_create_compositor
;