1 /**********************************************************
2  * Copyright 2009-2011 VMware, Inc. All rights reserved.
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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
22  * SOFTWARE.
23  *
24  *********************************************************
25  * Authors:
26  * Zack Rusin <zackr-at-vmware-dot-com>
27  * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
28  */
29 
30 #ifndef _XA_PRIV_H_
31 #define _XA_PRIV_H_
32 
33 #include "xa_tracker.h"
34 #include "xa_context.h"
35 #include "xa_composite.h"
36 
37 #include "pipe/p_screen.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_state.h"
40 
41 #include "util/u_math.h"
42 
43 #if defined(__GNUC__)
44 #define XA_EXPORT __attribute__ ((visibility("default")))
45 #else
46 #define XA_EXPORT
47 #endif
48 
49 #define XA_VB_SIZE (100 * 4 * 3 * 4)
50 #define XA_LAST_SURFACE_TYPE (xa_type_yuv_component + 1)
51 #define XA_MAX_SAMPLERS 3
52 
53 struct xa_fence {
54     struct pipe_fence_handle *pipe_fence;
55     struct xa_tracker *xa;
56 };
57 
58 struct xa_format_descriptor {
59     enum pipe_format format;
60     enum xa_formats xa_format;
61 };
62 
63 struct xa_surface {
64     int refcount;
65     struct pipe_resource template;
66     struct xa_tracker *xa;
67     struct pipe_resource *tex;
68     struct pipe_transfer *transfer;
69     unsigned int flags;
70     struct xa_format_descriptor fdesc;
71     struct pipe_context *mapping_pipe;
72 };
73 
74 struct xa_tracker {
75     enum xa_formats *supported_formats;
76     unsigned int format_map[XA_LAST_SURFACE_TYPE][2];
77     struct pipe_loader_device *dev;
78     struct pipe_screen *screen;
79     struct xa_context *default_ctx;
80 };
81 
82 struct xa_context {
83     struct xa_tracker *xa;
84     struct pipe_context *pipe;
85 
86     struct cso_context *cso;
87     struct xa_shaders *shaders;
88 
89     struct pipe_resource *vs_const_buffer;
90     struct pipe_resource *fs_const_buffer;
91 
92     float buffer[XA_VB_SIZE];
93     unsigned int buffer_size;
94     struct pipe_vertex_element velems[3];
95 
96     /* number of attributes per vertex for the current
97      * draw operation */
98     unsigned int attrs_per_vertex;
99 
100     unsigned int fb_width;
101     unsigned int fb_height;
102 
103     struct pipe_fence_handle *last_fence;
104     struct xa_surface *src;
105     struct xa_surface *dst;
106     struct pipe_surface *srf;
107 
108     /* destination scissor state.. we scissor out untouched parts
109      * of the dst for the benefit of tilers:
110      */
111     struct pipe_scissor_state scissor;
112     int scissor_valid;
113 
114     int simple_copy;
115 
116     int has_solid_src;
117     int has_solid_mask;
118     float solid_color[4];
119 
120     unsigned int num_bound_samplers;
121     struct pipe_sampler_view *bound_sampler_views[XA_MAX_SAMPLERS];
122     const struct xa_composite *comp;
123 };
124 
125 static inline void
xa_scissor_reset(struct xa_context * ctx)126 xa_scissor_reset(struct xa_context *ctx)
127 {
128     ctx->scissor.maxx = 0;
129     ctx->scissor.maxy = 0;
130     ctx->scissor.minx = ~0;
131     ctx->scissor.miny = ~0;
132     ctx->scissor_valid = FALSE;
133 }
134 
135 static inline void
xa_scissor_update(struct xa_context * ctx,unsigned minx,unsigned miny,unsigned maxx,unsigned maxy)136 xa_scissor_update(struct xa_context *ctx, unsigned minx, unsigned miny,
137 		unsigned maxx, unsigned maxy)
138 {
139     ctx->scissor.maxx = MAX2(ctx->scissor.maxx, maxx);
140     ctx->scissor.maxy = MAX2(ctx->scissor.maxy, maxy);
141     ctx->scissor.minx = MIN2(ctx->scissor.minx, minx);
142     ctx->scissor.miny = MIN2(ctx->scissor.miny, miny);
143     ctx->scissor_valid = TRUE;
144 }
145 
146 enum xa_vs_traits {
147     VS_COMPOSITE = 1 << 0,
148     VS_MASK = 1 << 1,
149     VS_SRC_SRC = 1 << 2,
150     VS_MASK_SRC = 1 << 3,
151     VS_YUV = 1 << 4,
152 };
153 
154 enum xa_fs_traits {
155     FS_COMPOSITE = 1 << 0,
156     FS_MASK = 1 << 1,
157     FS_SRC_SRC = 1 << 2,
158     FS_MASK_SRC = 1 << 3,
159     FS_YUV = 1 << 4,
160     FS_SRC_REPEAT_NONE = 1 << 5,
161     FS_MASK_REPEAT_NONE = 1 << 6,
162     FS_SRC_SWIZZLE_RGB = 1 << 7,
163     FS_MASK_SWIZZLE_RGB = 1 << 8,
164     FS_SRC_SET_ALPHA = 1 << 9,
165     FS_MASK_SET_ALPHA = 1 << 10,
166     FS_SRC_LUMINANCE = 1 << 11,
167     FS_MASK_LUMINANCE = 1 << 12,
168     FS_DST_LUMINANCE = 1 << 13,
169     FS_CA = 1 << 14,
170 };
171 
172 struct xa_shader {
173     void *fs;
174     void *vs;
175 };
176 
177 struct xa_shaders;
178 
179 /*
180  * Inline utilities
181  */
182 
183 static inline int
xa_min(int a,int b)184 xa_min(int a, int b)
185 {
186     return ((a <= b) ? a : b);
187 }
188 
189 static inline void
xa_pixel_to_float4(uint32_t pixel,float * color)190 xa_pixel_to_float4(uint32_t pixel, float *color)
191 {
192     uint32_t	    r, g, b, a;
193 
194     a = (pixel >> 24) & 0xff;
195     r = (pixel >> 16) & 0xff;
196     g = (pixel >>  8) & 0xff;
197     b = (pixel >>  0) & 0xff;
198     color[0] = ((float)r) / 255.;
199     color[1] = ((float)g) / 255.;
200     color[2] = ((float)b) / 255.;
201     color[3] = ((float)a) / 255.;
202 }
203 
204 static inline void
xa_pixel_to_float4_a8(uint32_t pixel,float * color)205 xa_pixel_to_float4_a8(uint32_t pixel, float *color)
206 {
207     uint32_t a;
208 
209     a = (pixel >> 24) & 0xff;
210     color[0] = ((float)a) / 255.;
211     color[1] = ((float)a) / 255.;
212     color[2] = ((float)a) / 255.;
213     color[3] = ((float)a) / 255.;
214 }
215 
216 /*
217  * xa_tgsi.c
218  */
219 
220 extern struct xa_shaders *xa_shaders_create(struct xa_context *);
221 
222 void xa_shaders_destroy(struct xa_shaders *shaders);
223 
224 struct xa_shader xa_shaders_get(struct xa_shaders *shaders,
225 				unsigned vs_traits, unsigned fs_traits);
226 
227 /*
228  * xa_context.c
229  */
230 extern void
231 xa_context_flush(struct xa_context *ctx);
232 
233 extern int
234 xa_ctx_srf_create(struct xa_context *ctx, struct xa_surface *dst);
235 
236 extern void
237 xa_ctx_srf_destroy(struct xa_context *ctx);
238 
239 extern void
240 xa_ctx_sampler_views_destroy(struct xa_context *ctx);
241 
242 /*
243  * xa_renderer.c
244  */
245 void renderer_set_constants(struct xa_context *r,
246 			    int shader_type, const float *params,
247 			    int param_bytes);
248 
249 void renderer_draw_yuv(struct xa_context *r,
250 		       float src_x,
251 		       float src_y,
252 		       float src_w,
253 		       float src_h,
254 		       int dst_x,
255 		       int dst_y, int dst_w, int dst_h,
256 		       struct xa_surface *srf[]);
257 
258 void renderer_bind_destination(struct xa_context *r,
259 			       struct pipe_surface *surface);
260 
261 void renderer_init_state(struct xa_context *r);
262 void renderer_copy_prepare(struct xa_context *r,
263 			   struct pipe_surface *dst_surface,
264 			   struct pipe_resource *src_texture,
265 			   const enum xa_formats src_xa_format,
266 			   const enum xa_formats dst_xa_format);
267 
268 void renderer_copy(struct xa_context *r, int dx,
269 		   int dy,
270 		   int sx,
271 		   int sy,
272 		   int width, int height, float src_width, float src_height);
273 
274 void renderer_draw_flush(struct xa_context *r);
275 
276 void renderer_begin_solid(struct xa_context *r);
277 void renderer_solid(struct xa_context *r,
278 		    int x0, int y0, int x1, int y1);
279 void
280 renderer_begin_textures(struct xa_context *r);
281 
282 void
283 renderer_texture(struct xa_context *r,
284 		 int *pos,
285 		 int width, int height,
286 		 const float *src_matrix,
287 		 const float *mask_matrix);
288 
289 #endif
290