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 "pipe/p_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"
47 static enum pipe_format
vg_format_to_pipe(VGImageFormat format
)
51 return PIPE_FORMAT_R5G6B5_UNORM
;
53 return PIPE_FORMAT_A1R5G5B5_UNORM
;
55 return PIPE_FORMAT_A4R4G4B4_UNORM
;
58 return PIPE_FORMAT_L8_UNORM
;
60 return PIPE_FORMAT_A8R8G8B8_UNORM
;
62 return PIPE_FORMAT_A8_UNORM
;
63 #ifdef OPENVG_VERSION_1_1
66 return PIPE_FORMAT_A8_UNORM
;
69 return PIPE_FORMAT_A8R8G8B8_UNORM
;
73 static INLINE
void vg_sync_size(VGfloat
*src_loc
, VGfloat
*dst_loc
)
75 src_loc
[2] = MIN2(src_loc
[2], dst_loc
[2]);
76 src_loc
[3] = MIN2(src_loc
[3], dst_loc
[3]);
77 dst_loc
[2] = src_loc
[2];
78 dst_loc
[3] = src_loc
[3];
82 static void vg_copy_texture(struct vg_context
*ctx
,
83 struct pipe_texture
*dst
, VGint dx
, VGint dy
,
84 struct pipe_texture
*src
, VGint sx
, VGint sy
,
85 VGint width
, VGint height
)
87 VGfloat dst_loc
[4], src_loc
[4];
88 VGfloat dst_bounds
[4], src_bounds
[4];
89 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
97 dst_bounds
[2] = dst
->width0
;
98 dst_bounds
[3] = dst
->height0
;
106 src_bounds
[2] = src
->width0
;
107 src_bounds
[3] = src
->height0
;
109 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
110 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
111 shift
[0] = src_shift
[0] - dst_shift
[0];
112 shift
[1] = src_shift
[1] - dst_shift
[1];
115 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
117 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
120 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
122 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
124 vg_sync_size(src_loc
, dst_loc
);
126 if (src_loc
[2] >= 0 && src_loc
[3] >= 0 &&
127 dst_loc
[2] >= 0 && dst_loc
[3] >= 0) {
128 renderer_copy_texture(ctx
->renderer
,
131 src_loc
[1] + src_loc
[3],
132 src_loc
[0] + src_loc
[2],
136 dst_loc
[1] + dst_loc
[3],
137 dst_loc
[0] + dst_loc
[2],
143 void vg_copy_surface(struct vg_context
*ctx
,
144 struct pipe_surface
*dst
, VGint dx
, VGint dy
,
145 struct pipe_surface
*src
, VGint sx
, VGint sy
,
146 VGint width
, VGint height
)
148 VGfloat dst_loc
[4], src_loc
[4];
149 VGfloat dst_bounds
[4], src_bounds
[4];
150 VGfloat src_shift
[4], dst_shift
[4], shift
[4];
158 dst_bounds
[2] = dst
->width
;
159 dst_bounds
[3] = dst
->height
;
167 src_bounds
[2] = src
->width
;
168 src_bounds
[3] = src
->height
;
170 vg_bound_rect(src_loc
, src_bounds
, src_shift
);
171 vg_bound_rect(dst_loc
, dst_bounds
, dst_shift
);
172 shift
[0] = src_shift
[0] - dst_shift
[0];
173 shift
[1] = src_shift
[1] - dst_shift
[1];
176 vg_shift_rectx(src_loc
, src_bounds
, -shift
[0]);
178 vg_shift_rectx(dst_loc
, dst_bounds
, shift
[0]);
181 vg_shift_recty(src_loc
, src_bounds
, -shift
[1]);
183 vg_shift_recty(dst_loc
, dst_bounds
, shift
[1]);
185 vg_sync_size(src_loc
, dst_loc
);
187 if (src_loc
[2] > 0 && src_loc
[3] > 0 &&
188 dst_loc
[2] > 0 && dst_loc
[3] > 0) {
190 renderer_copy_surface(ctx
->renderer
,
193 src
->height
- (src_loc
[1] + src_loc
[3]),
194 src_loc
[0] + src_loc
[2],
195 src
->height
- src_loc
[1],
198 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
199 dst_loc
[0] + dst_loc
[2],
200 dst
->height
- dst_loc
[1],
203 renderer_copy_surface(ctx
->renderer
,
206 src
->height
- src_loc
[1],
207 src_loc
[0] + src_loc
[2],
208 src
->height
- (src_loc
[1] + src_loc
[3]),
211 dst
->height
- (dst_loc
[1] + dst_loc
[3]),
212 dst_loc
[0] + dst_loc
[2],
213 dst
->height
- dst_loc
[1],
219 static struct pipe_texture
*image_texture(struct vg_image
*img
)
221 struct pipe_texture
*tex
= img
->texture
;
226 static void image_cleari(struct vg_image
*img
, VGint clear_colori
,
227 VGint x
, VGint y
, VGint width
, VGint height
)
231 VGfloat dwidth
, dheight
;
233 clearbuf
= malloc(sizeof(VGint
)*width
*height
);
234 for (i
= 0; i
< width
*height
; ++i
)
235 clearbuf
[i
] = clear_colori
;
237 dwidth
= MIN2(width
, img
->width
);
238 dheight
= MIN2(height
, img
->height
);
240 image_sub_data(img
, clearbuf
, width
* sizeof(VGint
),
242 x
, y
, dwidth
, dheight
);
246 struct vg_image
* image_create(VGImageFormat format
,
247 VGint width
, VGint height
)
249 struct vg_context
*ctx
= vg_current_context();
250 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
251 enum pipe_format pformat
= vg_format_to_pipe(format
);
252 struct pipe_texture pt
, *newtex
;
253 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
255 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
257 image
->format
= format
;
258 image
->width
= width
;
259 image
->height
= height
;
261 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
262 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
263 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
264 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
265 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
266 image
->sampler
.normalized_coords
= 1;
268 assert(screen
->is_format_supported(screen
, pformat
, PIPE_TEXTURE_2D
,
269 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
271 memset(&pt
, 0, sizeof(pt
));
272 pt
.target
= PIPE_TEXTURE_2D
;
278 pt
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
280 newtex
= screen
->texture_create(screen
, &pt
);
282 debug_assert(newtex
);
284 image
->texture
= newtex
;
286 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
288 image_cleari(image
, 0, 0, 0, image
->width
, image
->height
);
292 void image_destroy(struct vg_image
*img
)
294 struct vg_context
*ctx
= vg_current_context();
295 vg_context_remove_object(ctx
, VG_OBJECT_IMAGE
, img
);
299 /* remove img from the parent child array */
301 struct vg_image
**array
=
302 (struct vg_image
**)img
->parent
->children_array
->data
;
304 for (idx
= 0; idx
< img
->parent
->children_array
->num_elements
; ++idx
) {
305 struct vg_image
*child
= array
[idx
];
310 debug_assert(idx
< img
->parent
->children_array
->num_elements
);
311 array_remove_element(img
->parent
->children_array
, idx
);
314 if (img
->children_array
&& img
->children_array
->num_elements
) {
315 /* reparent the children */
317 struct vg_image
*parent
= img
->parent
;
318 struct vg_image
**children
=
319 (struct vg_image
**)img
->children_array
->data
;
321 VGint min_x
= children
[0]->x
;
322 parent
= children
[0];
324 for (i
= 1; i
< img
->children_array
->num_elements
; ++i
) {
325 struct vg_image
*child
= children
[i
];
326 if (child
->x
< min_x
) {
332 for (i
= 0; i
< img
->children_array
->num_elements
; ++i
) {
333 struct vg_image
*child
= children
[i
];
334 if (child
!= parent
) {
335 child
->parent
= parent
;
336 if (!parent
->children_array
) {
337 parent
->children_array
= array_create(
338 sizeof(struct vg_image
*));
340 array_append_data(parent
->children_array
,
343 child
->parent
= NULL
;
345 array_destroy(img
->children_array
);
348 pipe_texture_reference(&img
->texture
, NULL
);
352 void image_clear(struct vg_image
*img
,
353 VGint x
, VGint y
, VGint width
, VGint height
)
355 struct vg_context
*ctx
= vg_current_context();
356 VGfloat
*clear_colorf
= ctx
->state
.vg
.clear_color
;
359 /* FIXME: this is very nasty */
360 r
= float_to_ubyte(clear_colorf
[0]);
361 g
= float_to_ubyte(clear_colorf
[1]);
362 b
= float_to_ubyte(clear_colorf
[2]);
363 a
= float_to_ubyte(clear_colorf
[3]);
364 clear_colori
= r
<< 24 | g
<< 16 | b
<< 8 | a
;
365 image_cleari(img
, clear_colori
, x
, y
, width
, height
);
368 void image_sub_data(struct vg_image
*image
,
371 VGImageFormat dataFormat
,
373 VGint width
, VGint height
)
375 const VGint yStep
= 1;
376 VGubyte
*src
= (VGubyte
*)data
;
377 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
378 VGfloat
*df
= (VGfloat
*)temp
;
380 struct vg_context
*ctx
= vg_current_context();
381 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
382 struct pipe_texture
*texture
= image_texture(image
);
383 VGint xoffset
= 0, yoffset
= 0;
396 if (width
<= 0 || height
<= 0) {
397 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
401 if (x
> image
->width
|| y
> image
->width
) {
402 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
406 if (x
+ width
> image
->width
) {
407 width
= image
->width
- x
;
410 if (y
+ height
> image
->height
) {
411 height
= image
->height
- y
;
414 { /* upload color_data */
415 struct pipe_transfer
*transfer
= screen
->get_tex_transfer(
416 screen
, texture
, 0, 0, 0,
417 PIPE_TRANSFER_WRITE
, 0, 0, texture
->width0
, texture
->height0
);
418 src
+= (dataStride
* yoffset
);
419 for (i
= 0; i
< height
; i
++) {
420 _vega_unpack_float_span_rgba(ctx
, width
, xoffset
, src
, dataFormat
, temp
);
421 pipe_put_tile_rgba(transfer
, x
+image
->x
, y
+image
->y
, width
, 1, df
);
425 screen
->tex_transfer_destroy(transfer
);
429 void image_get_sub_data(struct vg_image
* image
,
432 VGImageFormat dataFormat
,
434 VGint width
, VGint height
)
436 struct vg_context
*ctx
= vg_current_context();
437 struct pipe_context
*pipe
= ctx
->pipe
;
438 struct pipe_screen
*screen
= pipe
->screen
;
439 VGfloat temp
[VEGA_MAX_IMAGE_WIDTH
][4];
440 VGfloat
*df
= (VGfloat
*)temp
;
441 VGint y
= 0, yStep
= 1;
443 VGubyte
*dst
= (VGubyte
*)data
;
446 struct pipe_transfer
*transfer
=
447 screen
->get_tex_transfer(screen
,
448 image
->texture
, 0, 0, 0,
451 image
->x
+ image
->width
,
452 image
->y
+ image
->height
);
453 /* Do a row at a time to flip image data vertically */
454 for (i
= 0; i
< height
; i
++) {
456 debug_printf("%d-%d == %d\n", sy
, height
, y
);
458 pipe_get_tile_rgba(transfer
, sx
+image
->x
, y
, width
, 1, df
);
460 _vega_pack_rgba_span_float(ctx
, width
, temp
, dataFormat
, dst
);
464 screen
->tex_transfer_destroy(transfer
);
468 struct vg_image
* image_child_image(struct vg_image
*parent
,
470 VGint width
, VGint height
)
472 struct vg_context
*ctx
= vg_current_context();
473 struct vg_image
*image
= CALLOC_STRUCT(vg_image
);
475 vg_init_object(&image
->base
, ctx
, VG_OBJECT_IMAGE
);
477 image
->x
= parent
->x
+ x
;
478 image
->y
= parent
->y
+ y
;
479 image
->width
= width
;
480 image
->height
= height
;
481 image
->parent
= parent
;
483 pipe_texture_reference(&image
->texture
,
486 image
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
487 image
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
488 image
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
489 image
->sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
490 image
->sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
491 image
->sampler
.normalized_coords
= 1;
493 if (!parent
->children_array
)
494 parent
->children_array
= array_create(
495 sizeof(struct vg_image
*));
497 array_append_data(parent
->children_array
,
500 vg_context_add_object(ctx
, VG_OBJECT_IMAGE
, image
);
505 void image_copy(struct vg_image
*dst
, VGint dx
, VGint dy
,
506 struct vg_image
*src
, VGint sx
, VGint sy
,
507 VGint width
, VGint height
,
510 struct vg_context
*ctx
= vg_current_context();
512 if (width
<= 0 || height
<= 0) {
513 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
516 /* make sure rendering has completed */
517 ctx
->pipe
->flush(ctx
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
518 vg_copy_texture(ctx
, dst
->texture
, dst
->x
+ dx
, dst
->y
+ dy
,
519 src
->texture
, src
->x
+ sx
, src
->y
+ sy
, width
, height
);
522 void image_draw(struct vg_image
*img
)
524 struct vg_context
*ctx
= vg_current_context();
529 struct matrix
*matrix
;
540 matrix
= &ctx
->state
.vg
.image_user_to_surface_matrix
;
542 matrix_map_point(matrix
, x1
, y1
, &x1
, &y1
);
543 matrix_map_point(matrix
, x2
, y2
, &x2
, &y2
);
544 matrix_map_point(matrix
, x3
, y3
, &x3
, &y3
);
545 matrix_map_point(matrix
, x4
, y4
, &x4
, &y4
);
547 shader_set_drawing_image(ctx
->shader
, VG_TRUE
);
548 shader_set_paint(ctx
->shader
, ctx
->state
.vg
.fill_paint
);
549 shader_set_image(ctx
->shader
, img
);
550 shader_bind(ctx
->shader
);
552 renderer_texture_quad(ctx
->renderer
, image_texture(img
),
553 img
->x
, img
->y
, img
->x
+ img
->width
, img
->y
+ img
->height
,
554 x1
, y1
, x2
, y2
, x3
, y3
, x4
, y4
);
557 void image_set_pixels(VGint dx
, VGint dy
,
558 struct vg_image
*src
, VGint sx
, VGint sy
,
559 VGint width
, VGint height
)
561 struct vg_context
*ctx
= vg_current_context();
562 struct pipe_context
*pipe
= ctx
->pipe
;
563 struct pipe_screen
*screen
= pipe
->screen
;
564 struct pipe_surface
*surf
;
565 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
567 /* make sure rendering has completed */
568 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
570 surf
= screen
->get_tex_surface(screen
, image_texture(src
), 0, 0, 0,
571 PIPE_BUFFER_USAGE_GPU_READ
);
573 vg_copy_surface(ctx
, strb
->surface
, dx
, dy
,
574 surf
, sx
+src
->x
, sy
+src
->y
, width
, height
);
576 screen
->tex_surface_destroy(surf
);
579 void image_get_pixels(struct vg_image
*dst
, VGint dx
, VGint dy
,
581 VGint width
, VGint height
)
583 struct vg_context
*ctx
= vg_current_context();
584 struct pipe_context
*pipe
= ctx
->pipe
;
585 struct pipe_screen
*screen
= pipe
->screen
;
586 struct pipe_surface
*surf
;
587 struct st_renderbuffer
*strb
= ctx
->draw_buffer
->strb
;
589 /* flip the y coordinates */
590 /*dy = dst->height - dy - height;*/
592 /* make sure rendering has completed */
593 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
595 surf
= screen
->get_tex_surface(screen
, image_texture(dst
), 0, 0, 0,
596 PIPE_BUFFER_USAGE_GPU_WRITE
|
597 PIPE_BUFFER_USAGE_GPU_READ
);
598 vg_copy_surface(ctx
, surf
, dst
->x
+ dx
, dst
->y
+ dy
,
599 strb
->surface
, sx
, sy
, width
, height
);
601 pipe_surface_reference(&surf
, NULL
);
605 VGboolean
vg_image_overlaps(struct vg_image
*dst
,
606 struct vg_image
*src
)
608 if (dst
== src
|| dst
->parent
== src
||
611 if (dst
->parent
&& dst
->parent
== src
->parent
) {
612 VGfloat left1
= dst
->x
;
613 VGfloat left2
= src
->x
;
614 VGfloat right1
= dst
->x
+ dst
->width
;
615 VGfloat right2
= src
->x
+ src
->width
;
616 VGfloat bottom1
= dst
->y
;
617 VGfloat bottom2
= src
->y
;
618 VGfloat top1
= dst
->y
+ dst
->height
;
619 VGfloat top2
= src
->y
+ src
->height
;
621 return !(left2
> right1
|| right2
< left1
||
622 top2
> bottom1
|| bottom2
< top1
);
627 VGint
image_bind_samplers(struct vg_image
*img
, struct pipe_sampler_state
**samplers
,
628 struct pipe_texture
**textures
)
630 img
->sampler
.min_img_filter
= image_sampler_filter(img
->base
.ctx
);
631 img
->sampler
.mag_img_filter
= image_sampler_filter(img
->base
.ctx
);
632 samplers
[3] = &img
->sampler
;
633 textures
[3] = img
->texture
;
637 VGint
image_sampler_filter(struct vg_context
*ctx
)
639 switch(ctx
->state
.vg
.image_quality
) {
640 case VG_IMAGE_QUALITY_NONANTIALIASED
:
641 return PIPE_TEX_FILTER_NEAREST
;
643 case VG_IMAGE_QUALITY_FASTER
:
644 return PIPE_TEX_FILTER_NEAREST
;
646 case VG_IMAGE_QUALITY_BETTER
:
647 /* possibly use anisotropic filtering */
648 return PIPE_TEX_FILTER_LINEAR
;
651 debug_printf("Unknown image quality");
653 return PIPE_TEX_FILTER_NEAREST
;