1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 #include "vg_translate.h"
30 #include "vg_context.h"
33 #include "util_array.h"
34 #include "api_consts.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_tile.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_sampler.h"
46 static enum pipe_format
vg_format_to_pipe(VGImageFormat format
)
50 return PIPE_FORMAT_B5G6R5_UNORM
;
52 return PIPE_FORMAT_B5G5R5A1_UNORM
;
54 return PIPE_FORMAT_B4G4R4A4_UNORM
;
57 return PIPE_FORMAT_L8_UNORM
;
59 return PIPE_FORMAT_B8G8R8A8_UNORM
;
61 return PIPE_FORMAT_A8_UNORM
;
62 #ifdef OPENVG_VERSION_1_1
65 return PIPE_FORMAT_A8_UNORM
;
68 return PIPE_FORMAT_B8G8R8A8_UNORM
;
72 static INLINE
void vg_sync_size(VGfloat
*src_loc
, VGfloat
*dst_loc
)
74 src_loc
[2] = MIN2(src_loc
[2], dst_loc
[2]);
75 src_loc
[3] = MIN2(src_loc
[3], dst_loc
[3]);
76 dst_loc
[2] = src_loc
[2];
77 dst_loc
[3] = src_loc
[3];
81 static void vg_copy_texture(struct vg_context
*ctx
,
82 struct pipe_resource
*dst
, VGint dx
, VGint dy
,
83 struct pipe_sampler_view
*src
, VGint sx
, VGint sy
,
84 VGint width
, VGint height
)
86 VGfloat dst_loc
[4], src_loc
[4];
87 VGfloat dst_bounds
[4], src_bounds
[4];
88 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
96 dst_bounds
[2] = dst
->width0
;
97 dst_bounds
[3] = dst
->height0
;
105 src_bounds
[2] = src
->texture
->width0
;
106 src_bounds
[3] = src
->texture
->height0
;
108 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
109 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
110 shift
[0] = src_shift
[0] - dst_shift
[0];
111 shift
[1] = src_shift
[1] - dst_shift
[1];
114 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
116 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
119 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
121 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
123 vg_sync_size(src_loc
, dst_loc
);
125 if (src_loc
[2] >= 0 && src_loc
[3] >= 0 &&
126 dst_loc
[2] >= 0 && dst_loc
[3] >= 0) {
127 renderer_copy_texture(ctx
->renderer
,
130 src_loc
[1] + src_loc
[3],
131 src_loc
[0] + src_loc
[2],
135 dst_loc
[1] + dst_loc
[3],
136 dst_loc
[0] + dst_loc
[2],
142 void vg_copy_surface(struct vg_context
*ctx
,
143 struct pipe_surface
*dst
, VGint dx
, VGint dy
,
144 struct pipe_surface
*src
, VGint sx
, VGint sy
,
145 VGint width
, VGint height
)
147 VGfloat dst_loc
[4], src_loc
[4];
148 VGfloat dst_bounds
[4], src_bounds
[4];
149 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
157 dst_bounds
[2] = dst
->width
;
158 dst_bounds
[3] = dst
->height
;
166 src_bounds
[2] = src
->width
;
167 src_bounds
[3] = src
->height
;
169 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
170 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
171 shift
[0] = src_shift
[0] - dst_shift
[0];
172 shift
[1] = src_shift
[1] - dst_shift
[1];
175 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
177 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
180 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
182 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
184 vg_sync_size(src_loc
, dst_loc
);
186 if (src_loc
[2] > 0 && src_loc
[3] > 0 &&
187 dst_loc
[2] > 0 && dst_loc
[3] > 0) {
189 renderer_copy_surface(ctx
->renderer
,
192 src
->height
- (src_loc
[1] + src_loc
[3]),
193 src_loc
[0] + src_loc
[2],
194 src
->height
- src_loc
[1],
197 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
198 dst_loc
[0] + dst_loc
[2],
199 dst
->height
- dst_loc
[1],
202 renderer_copy_surface(ctx
->renderer
,
205 src
->height
- src_loc
[1],
206 src_loc
[0] + src_loc
[2],
207 src
->height
- (src_loc
[1] + src_loc
[3]),
210 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
211 dst_loc
[0] + dst_loc
[2],
212 dst
->height
- dst_loc
[1],
218 static struct pipe_resource
*image_texture(struct vg_image
*img
)
220 struct pipe_resource
*tex
= img
->sampler_view
->texture
;
225 static void image_cleari(struct vg_image
*img
, VGint clear_colori
,
226 VGint x
, VGint y
, VGint width
, VGint height
)
230 VGfloat dwidth
, dheight
;
232 clearbuf
= malloc(sizeof(VGint
)*width
*height
);
233 for (i
= 0; i
< width
*height
; ++i
)
234 clearbuf
[i
] = clear_colori
;
236 dwidth
= MIN2(width
, img
->width
);
237 dheight
= MIN2(height
, img
->height
);
239 image_sub_data(img
, clearbuf
, width
* sizeof(VGint
),
241 x
, y
, dwidth
, dheight
);
245 struct vg_image
* image_create(VGImageFormat format
,
246 VGint width
, VGint height
)
248 struct vg_context
*ctx
= vg_current_context();
249 struct pipe_context
*pipe
= ctx
->pipe
;
250 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
251 enum pipe_format pformat
= vg_format_to_pipe(format
);
252 struct pipe_resource pt
, *newtex
;
253 struct pipe_sampler_view view_templ
;
254 struct pipe_sampler_view
*view
;
255 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
257 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
259 image
->format
= format
;
260 image
->width
= width
;
261 image
->height
= height
;
263 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
264 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
265 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
266 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
267 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
268 image
->sampler
.normalized_coords
= 1;
270 assert(screen
->is_format_supported(screen
, pformat
, PIPE_TEXTURE_2D
,
271 0, PIPE_BIND_SAMPLER_VIEW
, 0));
273 memset(&pt
, 0, sizeof(pt
));
274 pt
.target
= PIPE_TEXTURE_2D
;
280 pt
.bind
= PIPE_BIND_SAMPLER_VIEW
;
282 newtex
= screen
->resource_create(screen
, &pt
);
284 debug_assert(newtex
);
286 u_sampler_view_default_template(&view_templ
, newtex
, newtex
->format
);
287 view
= pipe
->create_sampler_view(pipe
, newtex
, &view_templ
);
288 /* want the texture to go away if the view is freed */
289 pipe_resource_reference(&newtex
, NULL
);
291 image
->sampler_view
= view
;
293 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
295 image_cleari(image
, 0, 0, 0, image
->width
, image
->height
);
299 void image_destroy(struct vg_image
*img
)
301 struct vg_context
*ctx
= vg_current_context();
302 vg_context_remove_object(ctx
, VG_OBJECT_IMAGE
, img
);
306 /* remove img from the parent child array */
308 struct vg_image
**array
=
309 (struct vg_image
**)img
->parent
->children_array
->data
;
311 for (idx
= 0; idx
< img
->parent
->children_array
->num_elements
; ++idx
) {
312 struct vg_image
*child
= array
[idx
];
317 debug_assert(idx
< img
->parent
->children_array
->num_elements
);
318 array_remove_element(img
->parent
->children_array
, idx
);
321 if (img
->children_array
&& img
->children_array
->num_elements
) {
322 /* reparent the children */
324 struct vg_image
*parent
= img
->parent
;
325 struct vg_image
**children
=
326 (struct vg_image
**)img
->children_array
->data
;
328 VGint min_x
= children
[0]->x
;
329 parent
= children
[0];
331 for (i
= 1; i
< img
->children_array
->num_elements
; ++i
) {
332 struct vg_image
*child
= children
[i
];
333 if (child
->x
< min_x
) {
339 for (i
= 0; i
< img
->children_array
->num_elements
; ++i
) {
340 struct vg_image
*child
= children
[i
];
341 if (child
!= parent
) {
342 child
->parent
= parent
;
343 if (!parent
->children_array
) {
344 parent
->children_array
= array_create(
345 sizeof(struct vg_image
*));
347 array_append_data(parent
->children_array
,
350 child
->parent
= NULL
;
352 array_destroy(img
->children_array
);
355 pipe_sampler_view_reference(&img
->sampler_view
, NULL
);
359 void image_clear(struct vg_image
*img
,
360 VGint x
, VGint y
, VGint width
, VGint height
)
362 struct vg_context
*ctx
= vg_current_context();
363 VGfloat
*clear_colorf
= ctx
->state
.vg
.clear_color
;
366 /* FIXME: this is very nasty */
367 r
= float_to_ubyte(clear_colorf
[0]);
368 g
= float_to_ubyte(clear_colorf
[1]);
369 b
= float_to_ubyte(clear_colorf
[2]);
370 a
= float_to_ubyte(clear_colorf
[3]);
371 clear_colori
= r
<< 24 | g
<< 16 | b
<< 8 | a
;
372 image_cleari(img
, clear_colori
, x
, y
, width
, height
);
375 void image_sub_data(struct vg_image
*image
,
378 VGImageFormat dataFormat
,
380 VGint width
, VGint height
)
382 const VGint yStep
= 1;
383 VGubyte
*src
= (VGubyte
*)data
;
384 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
385 VGfloat
*df
= (VGfloat
*)temp
;
387 struct vg_context
*ctx
= vg_current_context();
388 struct pipe_context
*pipe
= ctx
->pipe
;
389 struct pipe_resource
*texture
= image_texture(image
);
390 VGint xoffset
= 0, yoffset
= 0;
403 if (width
<= 0 || height
<= 0) {
404 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
408 if (x
> image
->width
|| y
> image
->width
) {
409 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
413 if (x
+ width
> image
->width
) {
414 width
= image
->width
- x
;
417 if (y
+ height
> image
->height
) {
418 height
= image
->height
- y
;
421 { /* upload color_data */
422 struct pipe_transfer
*transfer
= pipe_get_transfer(
423 pipe
, texture
, 0, 0, 0,
424 PIPE_TRANSFER_WRITE
, 0, 0, texture
->width0
, texture
->height0
);
425 src
+= (dataStride
* yoffset
);
426 for (i
= 0; i
< height
; i
++) {
427 _vega_unpack_float_span_rgba(ctx
, width
, xoffset
, src
, dataFormat
, temp
);
428 pipe_put_tile_rgba(pipe
, transfer
, x
+image
->x
, y
+image
->y
, width
, 1, df
);
432 pipe
->transfer_destroy(pipe
, transfer
);
436 void image_get_sub_data(struct vg_image
* image
,
439 VGImageFormat dataFormat
,
441 VGint width
, VGint height
)
443 struct vg_context
*ctx
= vg_current_context();
444 struct pipe_context
*pipe
= ctx
->pipe
;
445 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
446 VGfloat
*df
= (VGfloat
*)temp
;
447 VGint y
= 0, yStep
= 1;
449 VGubyte
*dst
= (VGubyte
*)data
;
452 struct pipe_transfer
*transfer
=
453 pipe_get_transfer(pipe
,
454 image
->sampler_view
->texture
, 0, 0, 0,
457 image
->x
+ image
->width
,
458 image
->y
+ image
->height
);
459 /* Do a row at a time to flip image data vertically */
460 for (i
= 0; i
< height
; i
++) {
462 debug_printf("%d-%d == %d\n", sy
, height
, y
);
464 pipe_get_tile_rgba(pipe
, transfer
, sx
+image
->x
, y
, width
, 1, df
);
466 _vega_pack_rgba_span_float(ctx
, width
, temp
, dataFormat
, dst
);
470 pipe
->transfer_destroy(pipe
, transfer
);
474 struct vg_image
* image_child_image(struct vg_image
*parent
,
476 VGint width
, VGint height
)
478 struct vg_context
*ctx
= vg_current_context();
479 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
481 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
483 image
->x
= parent
->x
+ x
;
484 image
->y
= parent
->y
+ y
;
485 image
->width
= width
;
486 image
->height
= height
;
487 image
->parent
= parent
;
488 image
->sampler_view
= NULL
;
489 pipe_sampler_view_reference(&image
->sampler_view
,
490 parent
->sampler_view
);
492 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
493 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
494 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
495 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
496 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
497 image
->sampler
.normalized_coords
= 1;
499 if (!parent
->children_array
)
500 parent
->children_array
= array_create(
501 sizeof(struct vg_image
*));
503 array_append_data(parent
->children_array
,
506 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
511 void image_copy(struct vg_image
*dst
, VGint dx
, VGint dy
,
512 struct vg_image
*src
, VGint sx
, VGint sy
,
513 VGint width
, VGint height
,
516 struct vg_context
*ctx
= vg_current_context();
518 if (width
<= 0 || height
<= 0) {
519 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
522 /* make sure rendering has completed */
523 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
524 vg_copy_texture(ctx
, dst
->sampler_view
->texture
, dst
->x
+ dx
, dst
->y
+ dy
,
525 src
->sampler_view
, src
->x
+ sx
, src
->y
+ sy
, width
, height
);
528 void image_draw(struct vg_image
*img
)
530 struct vg_context
*ctx
= vg_current_context();
535 struct matrix
*matrix
;
546 matrix
= &ctx
->state
.vg
.image_user_to_surface_matrix
;
548 matrix_map_point(matrix
, x1
, y1
, &x1
, &y1
);
549 matrix_map_point(matrix
, x2
, y2
, &x2
, &y2
);
550 matrix_map_point(matrix
, x3
, y3
, &x3
, &y3
);
551 matrix_map_point(matrix
, x4
, y4
, &x4
, &y4
);
553 shader_set_drawing_image(ctx
->shader
, VG_TRUE
);
554 shader_set_paint(ctx
->shader
, ctx
->state
.vg
.fill_paint
);
555 shader_set_image(ctx
->shader
, img
);
556 shader_bind(ctx
->shader
);
558 renderer_texture_quad(ctx
->renderer
, image_texture(img
),
559 img
->x
, img
->y
, img
->x
+ img
->width
, img
->y
+ img
->height
,
560 x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
);
563 void image_set_pixels(VGint dx
, VGint dy
,
564 struct vg_image
*src
, VGint sx
, VGint sy
,
565 VGint width
, VGint height
)
567 struct vg_context
*ctx
= vg_current_context();
568 struct pipe_context
*pipe
= ctx
->pipe
;
569 struct pipe_screen
*screen
= pipe
->screen
;
570 struct pipe_surface
*surf
;
571 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
573 /* make sure rendering has completed */
574 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
576 surf
= screen
->get_tex_surface(screen
, image_texture(src
), 0, 0, 0,
577 0 /* no bind flags as surf isn't actually used??? */);
579 vg_copy_surface(ctx
, strb
->surface
, dx
, dy
,
580 surf
, sx
+src
->x
, sy
+src
->y
, width
, height
);
582 screen
->tex_surface_destroy(surf
);
585 void image_get_pixels(struct vg_image
*dst
, VGint dx
, VGint dy
,
587 VGint width
, VGint height
)
589 struct vg_context
*ctx
= vg_current_context();
590 struct pipe_context
*pipe
= ctx
->pipe
;
591 struct pipe_screen
*screen
= pipe
->screen
;
592 struct pipe_surface
*surf
;
593 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
595 /* flip the y coordinates */
596 /*dy = dst->height - dy - height;*/
598 /* make sure rendering has completed */
599 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
601 surf
= screen
->get_tex_surface(screen
, image_texture(dst
), 0, 0, 0,
602 0 /* no bind flags as surf isn't actually used??? */);
604 vg_copy_surface(ctx
, surf
, dst
->x
+ dx
, dst
->y
+ dy
,
605 strb
->surface
, sx
, sy
, width
, height
);
607 pipe_surface_reference(&surf
, NULL
);
611 VGboolean
vg_image_overlaps(struct vg_image
*dst
,
612 struct vg_image
*src
)
614 if (dst
== src
|| dst
->parent
== src
||
617 if (dst
->parent
&& dst
->parent
== src
->parent
) {
618 VGfloat left1
= dst
->x
;
619 VGfloat left2
= src
->x
;
620 VGfloat right1
= dst
->x
+ dst
->width
;
621 VGfloat right2
= src
->x
+ src
->width
;
622 VGfloat bottom1
= dst
->y
;
623 VGfloat bottom2
= src
->y
;
624 VGfloat top1
= dst
->y
+ dst
->height
;
625 VGfloat top2
= src
->y
+ src
->height
;
627 return !(left2
> right1
|| right2
< left1
||
628 top2
> bottom1
|| bottom2
< top1
);
633 VGint
image_bind_samplers(struct vg_image
*img
, struct pipe_sampler_state
**samplers
,
634 struct pipe_sampler_view
**sampler_views
)
636 img
->sampler
.min_img_filter
= image_sampler_filter(img
->base
.ctx
);
637 img
->sampler
.mag_img_filter
= image_sampler_filter(img
->base
.ctx
);
638 samplers
[3] = &img
->sampler
;
639 sampler_views
[3] = img
->sampler_view
;
643 VGint
image_sampler_filter(struct vg_context
*ctx
)
645 switch(ctx
->state
.vg
.image_quality
) {
646 case VG_IMAGE_QUALITY_NONANTIALIASED
:
647 return PIPE_TEX_FILTER_NEAREST
;
649 case VG_IMAGE_QUALITY_FASTER
:
650 return PIPE_TEX_FILTER_NEAREST
;
652 case VG_IMAGE_QUALITY_BETTER
:
653 /* possibly use anisotropic filtering */
654 return PIPE_TEX_FILTER_LINEAR
;
657 debug_printf("Unknown image quality");
659 return PIPE_TEX_FILTER_NEAREST
;