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"
35 #include "shaders_cache.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
40 #include "util/u_inlines.h"
41 #include "util/u_blit.h"
42 #include "util/u_format.h"
43 #include "util/u_tile.h"
44 #include "util/u_memory.h"
45 #include "util/u_math.h"
46 #include "util/u_sampler.h"
48 static enum pipe_format
vg_format_to_pipe(VGImageFormat format
)
52 return PIPE_FORMAT_B5G6R5_UNORM
;
54 return PIPE_FORMAT_B5G5R5A1_UNORM
;
56 return PIPE_FORMAT_B4G4R4A4_UNORM
;
59 return PIPE_FORMAT_L8_UNORM
;
61 return PIPE_FORMAT_B8G8R8A8_UNORM
;
63 return PIPE_FORMAT_A8_UNORM
;
64 #ifdef OPENVG_VERSION_1_1
67 return PIPE_FORMAT_A8_UNORM
;
70 return PIPE_FORMAT_B8G8R8A8_UNORM
;
74 static INLINE
void vg_sync_size(VGfloat
*src_loc
, VGfloat
*dst_loc
)
76 src_loc
[2] = MIN2(src_loc
[2], dst_loc
[2]);
77 src_loc
[3] = MIN2(src_loc
[3], dst_loc
[3]);
78 dst_loc
[2] = src_loc
[2];
79 dst_loc
[3] = src_loc
[3];
83 static void vg_copy_texture(struct vg_context
*ctx
,
84 struct pipe_resource
*dst
, VGint dx
, VGint dy
,
85 struct pipe_sampler_view
*src
, VGint sx
, VGint sy
,
86 VGint width
, VGint height
)
88 VGfloat dst_loc
[4], src_loc
[4];
89 VGfloat dst_bounds
[4], src_bounds
[4];
90 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
98 dst_bounds
[2] = dst
->width0
;
99 dst_bounds
[3] = dst
->height0
;
107 src_bounds
[2] = src
->texture
->width0
;
108 src_bounds
[3] = src
->texture
->height0
;
110 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
111 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
112 shift
[0] = src_shift
[0] - dst_shift
[0];
113 shift
[1] = src_shift
[1] - dst_shift
[1];
116 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
118 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
121 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
123 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
125 vg_sync_size(src_loc
, dst_loc
);
127 if (src_loc
[2] >= 0 && src_loc
[3] >= 0 &&
128 dst_loc
[2] >= 0 && dst_loc
[3] >= 0) {
129 renderer_copy_texture(ctx
->renderer
,
132 src_loc
[1] + src_loc
[3],
133 src_loc
[0] + src_loc
[2],
137 dst_loc
[1] + dst_loc
[3],
138 dst_loc
[0] + dst_loc
[2],
144 void vg_copy_surface(struct vg_context
*ctx
,
145 struct pipe_surface
*dst
, VGint dx
, VGint dy
,
146 struct pipe_surface
*src
, VGint sx
, VGint sy
,
147 VGint width
, VGint height
)
149 VGfloat dst_loc
[4], src_loc
[4];
150 VGfloat dst_bounds
[4], src_bounds
[4];
151 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
159 dst_bounds
[2] = dst
->width
;
160 dst_bounds
[3] = dst
->height
;
168 src_bounds
[2] = src
->width
;
169 src_bounds
[3] = src
->height
;
171 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
172 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
173 shift
[0] = src_shift
[0] - dst_shift
[0];
174 shift
[1] = src_shift
[1] - dst_shift
[1];
177 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
179 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
182 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
184 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
186 vg_sync_size(src_loc
, dst_loc
);
188 if (src_loc
[2] > 0 && src_loc
[3] > 0 &&
189 dst_loc
[2] > 0 && dst_loc
[3] > 0) {
191 renderer_copy_surface(ctx
->renderer
,
194 src
->height
- (src_loc
[1] + src_loc
[3]),
195 src_loc
[0] + src_loc
[2],
196 src
->height
- src_loc
[1],
199 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
200 dst_loc
[0] + dst_loc
[2],
201 dst
->height
- dst_loc
[1],
204 renderer_copy_surface(ctx
->renderer
,
207 src
->height
- src_loc
[1],
208 src_loc
[0] + src_loc
[2],
209 src
->height
- (src_loc
[1] + src_loc
[3]),
212 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
213 dst_loc
[0] + dst_loc
[2],
214 dst
->height
- dst_loc
[1],
220 static struct pipe_resource
*image_texture(struct vg_image
*img
)
222 struct pipe_resource
*tex
= img
->sampler_view
->texture
;
227 static void image_cleari(struct vg_image
*img
, VGint clear_colori
,
228 VGint x
, VGint y
, VGint width
, VGint height
)
232 VGfloat dwidth
, dheight
;
234 clearbuf
= malloc(sizeof(VGint
)*width
*height
);
235 for (i
= 0; i
< width
*height
; ++i
)
236 clearbuf
[i
] = clear_colori
;
238 dwidth
= MIN2(width
, img
->width
);
239 dheight
= MIN2(height
, img
->height
);
241 image_sub_data(img
, clearbuf
, width
* sizeof(VGint
),
243 x
, y
, dwidth
, dheight
);
247 struct vg_image
* image_create(VGImageFormat format
,
248 VGint width
, VGint height
)
250 struct vg_context
*ctx
= vg_current_context();
251 struct pipe_context
*pipe
= ctx
->pipe
;
252 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
253 enum pipe_format pformat
= vg_format_to_pipe(format
);
254 struct pipe_resource pt
, *newtex
;
255 struct pipe_sampler_view view_templ
;
256 struct pipe_sampler_view
*view
;
257 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
259 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
261 image
->format
= format
;
262 image
->width
= width
;
263 image
->height
= height
;
265 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
266 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
267 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
268 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
269 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
270 image
->sampler
.normalized_coords
= 1;
272 assert(screen
->is_format_supported(screen
, pformat
, PIPE_TEXTURE_2D
,
273 0, PIPE_BIND_SAMPLER_VIEW
, 0));
275 memset(&pt
, 0, sizeof(pt
));
276 pt
.target
= PIPE_TEXTURE_2D
;
282 pt
.bind
= PIPE_BIND_SAMPLER_VIEW
;
284 newtex
= screen
->resource_create(screen
, &pt
);
286 debug_assert(newtex
);
288 u_sampler_view_default_template(&view_templ
, newtex
, newtex
->format
);
289 view
= pipe
->create_sampler_view(pipe
, newtex
, &view_templ
);
290 /* want the texture to go away if the view is freed */
291 pipe_resource_reference(&newtex
, NULL
);
293 image
->sampler_view
= view
;
295 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
297 image_cleari(image
, 0, 0, 0, image
->width
, image
->height
);
301 void image_destroy(struct vg_image
*img
)
303 struct vg_context
*ctx
= vg_current_context();
304 vg_context_remove_object(ctx
, VG_OBJECT_IMAGE
, img
);
308 /* remove img from the parent child array */
310 struct vg_image
**array
=
311 (struct vg_image
**)img
->parent
->children_array
->data
;
313 for (idx
= 0; idx
< img
->parent
->children_array
->num_elements
; ++idx
) {
314 struct vg_image
*child
= array
[idx
];
319 debug_assert(idx
< img
->parent
->children_array
->num_elements
);
320 array_remove_element(img
->parent
->children_array
, idx
);
323 if (img
->children_array
&& img
->children_array
->num_elements
) {
324 /* reparent the children */
326 struct vg_image
*parent
= img
->parent
;
327 struct vg_image
**children
=
328 (struct vg_image
**)img
->children_array
->data
;
330 VGint min_x
= children
[0]->x
;
331 parent
= children
[0];
333 for (i
= 1; i
< img
->children_array
->num_elements
; ++i
) {
334 struct vg_image
*child
= children
[i
];
335 if (child
->x
< min_x
) {
341 for (i
= 0; i
< img
->children_array
->num_elements
; ++i
) {
342 struct vg_image
*child
= children
[i
];
343 if (child
!= parent
) {
344 child
->parent
= parent
;
345 if (!parent
->children_array
) {
346 parent
->children_array
= array_create(
347 sizeof(struct vg_image
*));
349 array_append_data(parent
->children_array
,
352 child
->parent
= NULL
;
354 array_destroy(img
->children_array
);
357 pipe_sampler_view_reference(&img
->sampler_view
, NULL
);
361 void image_clear(struct vg_image
*img
,
362 VGint x
, VGint y
, VGint width
, VGint height
)
364 struct vg_context
*ctx
= vg_current_context();
365 VGfloat
*clear_colorf
= ctx
->state
.vg
.clear_color
;
368 /* FIXME: this is very nasty */
369 r
= float_to_ubyte(clear_colorf
[0]);
370 g
= float_to_ubyte(clear_colorf
[1]);
371 b
= float_to_ubyte(clear_colorf
[2]);
372 a
= float_to_ubyte(clear_colorf
[3]);
373 clear_colori
= r
<< 24 | g
<< 16 | b
<< 8 | a
;
374 image_cleari(img
, clear_colori
, x
, y
, width
, height
);
377 void image_sub_data(struct vg_image
*image
,
380 VGImageFormat dataFormat
,
382 VGint width
, VGint height
)
384 const VGint yStep
= 1;
385 VGubyte
*src
= (VGubyte
*)data
;
386 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
387 VGfloat
*df
= (VGfloat
*)temp
;
389 struct vg_context
*ctx
= vg_current_context();
390 struct pipe_context
*pipe
= ctx
->pipe
;
391 struct pipe_resource
*texture
= image_texture(image
);
392 VGint xoffset
= 0, yoffset
= 0;
405 if (width
<= 0 || height
<= 0) {
406 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
410 if (x
> image
->width
|| y
> image
->width
) {
411 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
415 if (x
+ width
> image
->width
) {
416 width
= image
->width
- x
;
419 if (y
+ height
> image
->height
) {
420 height
= image
->height
- y
;
423 { /* upload color_data */
424 struct pipe_transfer
*transfer
= pipe_get_transfer(
425 pipe
, texture
, 0, 0, 0,
426 PIPE_TRANSFER_WRITE
, 0, 0, texture
->width0
, texture
->height0
);
427 src
+= (dataStride
* yoffset
);
428 for (i
= 0; i
< height
; i
++) {
429 _vega_unpack_float_span_rgba(ctx
, width
, xoffset
, src
, dataFormat
, temp
);
430 pipe_put_tile_rgba(pipe
, transfer
, x
+image
->x
, y
+image
->y
, width
, 1, df
);
434 pipe
->transfer_destroy(pipe
, transfer
);
438 void image_get_sub_data(struct vg_image
* image
,
441 VGImageFormat dataFormat
,
443 VGint width
, VGint height
)
445 struct vg_context
*ctx
= vg_current_context();
446 struct pipe_context
*pipe
= ctx
->pipe
;
447 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
448 VGfloat
*df
= (VGfloat
*)temp
;
449 VGint y
= 0, yStep
= 1;
451 VGubyte
*dst
= (VGubyte
*)data
;
454 struct pipe_transfer
*transfer
=
455 pipe_get_transfer(pipe
,
456 image
->sampler_view
->texture
, 0, 0, 0,
459 image
->x
+ image
->width
,
460 image
->y
+ image
->height
);
461 /* Do a row at a time to flip image data vertically */
462 for (i
= 0; i
< height
; i
++) {
464 debug_printf("%d-%d == %d\n", sy
, height
, y
);
466 pipe_get_tile_rgba(pipe
, transfer
, sx
+image
->x
, y
, width
, 1, df
);
468 _vega_pack_rgba_span_float(ctx
, width
, temp
, dataFormat
, dst
);
472 pipe
->transfer_destroy(pipe
, transfer
);
476 struct vg_image
* image_child_image(struct vg_image
*parent
,
478 VGint width
, VGint height
)
480 struct vg_context
*ctx
= vg_current_context();
481 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
483 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
485 image
->x
= parent
->x
+ x
;
486 image
->y
= parent
->y
+ y
;
487 image
->width
= width
;
488 image
->height
= height
;
489 image
->parent
= parent
;
490 image
->sampler_view
= NULL
;
491 pipe_sampler_view_reference(&image
->sampler_view
,
492 parent
->sampler_view
);
494 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
495 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
496 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
497 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
498 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
499 image
->sampler
.normalized_coords
= 1;
501 if (!parent
->children_array
)
502 parent
->children_array
= array_create(
503 sizeof(struct vg_image
*));
505 array_append_data(parent
->children_array
,
508 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
513 void image_copy(struct vg_image
*dst
, VGint dx
, VGint dy
,
514 struct vg_image
*src
, VGint sx
, VGint sy
,
515 VGint width
, VGint height
,
518 struct vg_context
*ctx
= vg_current_context();
520 if (width
<= 0 || height
<= 0) {
521 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
524 /* make sure rendering has completed */
525 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
526 vg_copy_texture(ctx
, dst
->sampler_view
->texture
, dst
->x
+ dx
, dst
->y
+ dy
,
527 src
->sampler_view
, src
->x
+ sx
, src
->y
+ sy
, width
, height
);
530 void image_draw(struct vg_image
*img
)
532 struct vg_context
*ctx
= vg_current_context();
537 struct matrix
*matrix
;
548 matrix
= &ctx
->state
.vg
.image_user_to_surface_matrix
;
550 matrix_map_point(matrix
, x1
, y1
, &x1
, &y1
);
551 matrix_map_point(matrix
, x2
, y2
, &x2
, &y2
);
552 matrix_map_point(matrix
, x3
, y3
, &x3
, &y3
);
553 matrix_map_point(matrix
, x4
, y4
, &x4
, &y4
);
555 shader_set_drawing_image(ctx
->shader
, VG_TRUE
);
556 shader_set_paint(ctx
->shader
, ctx
->state
.vg
.fill_paint
);
557 shader_set_image(ctx
->shader
, img
);
558 shader_bind(ctx
->shader
);
560 renderer_texture_quad(ctx
->renderer
, image_texture(img
),
561 img
->x
, img
->y
, img
->x
+ img
->width
, img
->y
+ img
->height
,
562 x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
);
565 void image_set_pixels(VGint dx
, VGint dy
,
566 struct vg_image
*src
, VGint sx
, VGint sy
,
567 VGint width
, VGint height
)
569 struct vg_context
*ctx
= vg_current_context();
570 struct pipe_context
*pipe
= ctx
->pipe
;
571 struct pipe_screen
*screen
= pipe
->screen
;
572 struct pipe_surface
*surf
;
573 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
575 /* make sure rendering has completed */
576 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
578 surf
= screen
->get_tex_surface(screen
, image_texture(src
), 0, 0, 0,
579 0 /* no bind flags as surf isn't actually used??? */);
581 vg_copy_surface(ctx
, strb
->surface
, dx
, dy
,
582 surf
, sx
+src
->x
, sy
+src
->y
, width
, height
);
584 screen
->tex_surface_destroy(surf
);
587 void image_get_pixels(struct vg_image
*dst
, VGint dx
, VGint dy
,
589 VGint width
, VGint height
)
591 struct vg_context
*ctx
= vg_current_context();
592 struct pipe_context
*pipe
= ctx
->pipe
;
593 struct pipe_screen
*screen
= pipe
->screen
;
594 struct pipe_surface
*surf
;
595 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
597 /* flip the y coordinates */
598 /*dy = dst->height - dy - height;*/
600 /* make sure rendering has completed */
601 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
603 surf
= screen
->get_tex_surface(screen
, image_texture(dst
), 0, 0, 0,
604 0 /* no bind flags as surf isn't actually used??? */);
606 vg_copy_surface(ctx
, surf
, dst
->x
+ dx
, dst
->y
+ dy
,
607 strb
->surface
, sx
, sy
, width
, height
);
609 pipe_surface_reference(&surf
, NULL
);
613 VGboolean
vg_image_overlaps(struct vg_image
*dst
,
614 struct vg_image
*src
)
616 if (dst
== src
|| dst
->parent
== src
||
619 if (dst
->parent
&& dst
->parent
== src
->parent
) {
620 VGfloat left1
= dst
->x
;
621 VGfloat left2
= src
->x
;
622 VGfloat right1
= dst
->x
+ dst
->width
;
623 VGfloat right2
= src
->x
+ src
->width
;
624 VGfloat bottom1
= dst
->y
;
625 VGfloat bottom2
= src
->y
;
626 VGfloat top1
= dst
->y
+ dst
->height
;
627 VGfloat top2
= src
->y
+ src
->height
;
629 return !(left2
> right1
|| right2
< left1
||
630 top2
> bottom1
|| bottom2
< top1
);
635 VGint
image_bind_samplers(struct vg_image
*img
, struct pipe_sampler_state
**samplers
,
636 struct pipe_sampler_view
**sampler_views
)
638 img
->sampler
.min_img_filter
= image_sampler_filter(img
->base
.ctx
);
639 img
->sampler
.mag_img_filter
= image_sampler_filter(img
->base
.ctx
);
640 samplers
[3] = &img
->sampler
;
641 sampler_views
[3] = img
->sampler_view
;
645 VGint
image_sampler_filter(struct vg_context
*ctx
)
647 switch(ctx
->state
.vg
.image_quality
) {
648 case VG_IMAGE_QUALITY_NONANTIALIASED
:
649 return PIPE_TEX_FILTER_NEAREST
;
651 case VG_IMAGE_QUALITY_FASTER
:
652 return PIPE_TEX_FILTER_NEAREST
;
654 case VG_IMAGE_QUALITY_BETTER
:
655 /* possibly use anisotropic filtering */
656 return PIPE_TEX_FILTER_LINEAR
;
659 debug_printf("Unknown image quality");
661 return PIPE_TEX_FILTER_NEAREST
;