1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
/**************************************************************************
*
* Copyright 2009 VMware, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#include "VG/openvg.h"
#include "vg_context.h"
#include "image.h"
#include "api.h"
#include "renderer.h"
#include "shaders_cache.h"
#include "st_inlines.h"
#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
#include "pipe/p_screen.h"
#include "util/u_format.h"
#include "util/u_sampler.h"
#include "util/u_string.h"
#include "asm_filters.h"
struct filter_info {
struct vg_image *dst;
struct vg_image *src;
struct vg_shader * (*setup_shader)(struct vg_context *, void *);
void *user_data;
const void *const_buffer;
VGint const_buffer_len;
VGTilingMode tiling_mode;
struct pipe_sampler_view *extra_texture_view;
};
static INLINE struct pipe_resource *create_texture_1d(struct vg_context *ctx,
const VGuint *color_data,
const VGint color_data_len)
{
struct pipe_context *pipe = ctx->pipe;
struct pipe_screen *screen = pipe->screen;
struct pipe_resource *tex = 0;
struct pipe_resource templ;
memset(&templ, 0, sizeof(templ));
templ.target = PIPE_TEXTURE_1D;
templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
templ.last_level = 0;
templ.width0 = color_data_len;
templ.height0 = 1;
templ.depth0 = 1;
templ.bind = PIPE_BIND_SAMPLER_VIEW;
tex = screen->resource_create(screen, &templ);
{ /* upload color_data */
struct pipe_transfer *transfer =
pipe_get_transfer(pipe, tex,
0, 0, 0,
PIPE_TRANSFER_READ_WRITE ,
0, 0, tex->width0, tex->height0);
void *map = pipe->transfer_map(pipe, transfer);
memcpy(map, color_data, sizeof(VGint)*color_data_len);
pipe->transfer_unmap(pipe, transfer);
pipe->transfer_destroy(pipe, transfer);
}
return tex;
}
static INLINE struct pipe_sampler_view *create_texture_1d_view(struct vg_context *ctx,
const VGuint *color_data,
const VGint color_data_len)
{
struct pipe_context *pipe = ctx->pipe;
struct pipe_resource *texture;
struct pipe_sampler_view view_templ;
struct pipe_sampler_view *view;
texture = create_texture_1d(ctx, color_data, color_data_len);
if (!texture)
return NULL;
u_sampler_view_default_template(&view_templ, texture, texture->format);
view = pipe->create_sampler_view(pipe, texture, &view_templ);
/* want the texture to go away if the view is freed */
pipe_resource_reference(&texture, NULL);
return view;
}
static INLINE struct pipe_surface * setup_framebuffer(struct vg_image *dst)
{
struct vg_context *ctx = vg_current_context();
struct pipe_context *pipe = ctx->pipe;
struct pipe_framebuffer_state fb;
struct pipe_surface *dst_surf = pipe->screen->get_tex_surface(
pipe->screen, dst->sampler_view->texture, 0, 0, 0,
PIPE_BIND_RENDER_TARGET);
/* drawing dest */
memset(&fb, 0, sizeof(fb));
fb.width = dst->x + dst_surf->width;
fb.height = dst->y + dst_surf->height;
fb.nr_cbufs = 1;
fb.cbufs[0] = dst_surf;
{
VGint i;
for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
fb.cbufs[i] = 0;
}
cso_set_framebuffer(ctx->cso_context, &fb);
return dst_surf;
}
static void setup_viewport(struct vg_image *dst)
{
struct vg_context *ctx = vg_current_context();
vg_set_viewport(ctx, VEGA_Y0_TOP);
}
static void setup_blend()
{
struct vg_context *ctx = vg_current_context();
struct pipe_blend_state blend;
memset(&blend, 0, sizeof(blend));
blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
if (ctx->state.vg.filter_channel_mask & VG_RED)
blend.rt[0].colormask |= PIPE_MASK_R;
if (ctx->state.vg.filter_channel_mask & VG_GREEN)
blend.rt[0].colormask |= PIPE_MASK_G;
if (ctx->state.vg.filter_channel_mask & VG_BLUE)
blend.rt[0].colormask |= PIPE_MASK_B;
if (ctx->state.vg.filter_channel_mask & VG_ALPHA)
blend.rt[0].colormask |= PIPE_MASK_A;
blend.rt[0].blend_enable = 0;
cso_set_blend(ctx->cso_context, &blend);
}
static void setup_constant_buffer(struct vg_context *ctx, const void *buffer,
VGint param_bytes)
{
struct pipe_context *pipe = ctx->pipe;
struct pipe_resource **cbuf = &ctx->filter.buffer;
/* We always need to get a new buffer, to keep the drivers simple and
* avoid gratuitous rendering synchronization. */
pipe_resource_reference(cbuf, NULL);
*cbuf = pipe_buffer_create(pipe->screen,
PIPE_BIND_CONSTANT_BUFFER,
param_bytes);
if (*cbuf) {
st_no_flush_pipe_buffer_write(ctx, *cbuf,
0, param_bytes, buffer);
}
ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, *cbuf);
}
static void setup_samplers(struct vg_context *ctx, struct filter_info *info)
{
struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
struct pipe_sampler_state sampler[3];
int num_samplers = 0;
int num_textures = 0;
samplers[0] = NULL;
samplers[1] = NULL;
samplers[2] = NULL;
samplers[3] = NULL;
sampler_views[0] = NULL;
sampler_views[1] = NULL;
sampler_views[2] = NULL;
sampler_views[3] = NULL;
memset(&sampler[0], 0, sizeof(struct pipe_sampler_state));
sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
sampler[0].wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
sampler[0].min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
sampler[0].mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
sampler[0].normalized_coords = 1;
switch(info->tiling_mode) {
case VG_TILE_FILL:
sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
memcpy(sampler[0].border_color,
ctx->state.vg.tile_fill_color,
sizeof(VGfloat) * 4);
break;
case VG_TILE_PAD:
sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
break;
case VG_TILE_REPEAT:
sampler[0].wrap_s = PIPE_TEX_WRAP_REPEAT;
sampler[0].wrap_t = PIPE_TEX_WRAP_REPEAT;
break;
case VG_TILE_REFLECT:
sampler[0].wrap_s = PIPE_TEX_WRAP_MIRROR_REPEAT;
sampler[0].wrap_t = PIPE_TEX_WRAP_MIRROR_REPEAT;
break;
default:
debug_assert(!"Unknown tiling mode");
}
samplers[0] = &sampler[0];
sampler_views[0] = info->src->sampler_view;
++num_samplers;
++num_textures;
if (info->extra_texture_view) {
memcpy(&sampler[1], &sampler[0], sizeof(struct pipe_sampler_state));
samplers[1] = &sampler[1];
sampler_views[1] = info->extra_texture_view;
++num_samplers;
++num_textures;
}
cso_set_samplers(ctx->cso_context, num_samplers, (const struct pipe_sampler_state **)samplers);
cso_set_fragment_sampler_views(ctx->cso_context, num_textures, sampler_views);
}
static struct vg_shader * setup_color_matrix(struct vg_context *ctx, void *user_data)
{
struct vg_shader *shader =
shader_create_from_text(ctx->pipe, color_matrix_asm, 200,
PIPE_SHADER_FRAGMENT);
cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
return shader;
}
static struct vg_shader * setup_convolution(struct vg_context *ctx, void *user_data)
{
char buffer[1024];
VGint num_consts = (VGint)(long)(user_data);
struct vg_shader *shader;
util_snprintf(buffer, 1023, convolution_asm, num_consts, num_consts / 2 + 1);
shader = shader_create_from_text(ctx->pipe, buffer, 200,
PIPE_SHADER_FRAGMENT);
cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
return shader;
}
static struct vg_shader * setup_lookup(struct vg_context *ctx, void *user_data)
{
struct vg_shader *shader =
shader_create_from_text(ctx->pipe, lookup_asm,
200, PIPE_SHADER_FRAGMENT);
cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
return shader;
}
static struct vg_shader * setup_lookup_single(struct vg_context *ctx, void *user_data)
{
char buffer[1024];
VGImageChannel channel = (VGImageChannel)(user_data);
struct vg_shader *shader;
switch(channel) {
case VG_RED:
util_snprintf(buffer, 1023, lookup_single_asm, "xxxx");
break;
case VG_GREEN:
util_snprintf(buffer, 1023, lookup_single_asm, "yyyy");
break;
case VG_BLUE:
util_snprintf(buffer, 1023, lookup_single_asm, "zzzz");
break;
case VG_ALPHA:
util_snprintf(buffer, 1023, lookup_single_asm, "wwww");
break;
default:
debug_assert(!"Unknown color channel");
}
shader = shader_create_from_text(ctx->pipe, buffer, 200,
PIPE_SHADER_FRAGMENT);
cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
return shader;
}
static void execute_filter(struct vg_context *ctx,
struct filter_info *info)
{
struct pipe_surface *dst_surf;
struct vg_shader *shader;
cso_save_framebuffer(ctx->cso_context);
cso_save_fragment_shader(ctx->cso_context);
cso_save_viewport(ctx->cso_context);
cso_save_blend(ctx->cso_context);
cso_save_samplers(ctx->cso_context);
cso_save_fragment_sampler_views(ctx->cso_context);
dst_surf = setup_framebuffer(info->dst);
setup_viewport(info->dst);
setup_blend();
setup_constant_buffer(ctx, info->const_buffer, info->const_buffer_len);
shader = info->setup_shader(ctx, info->user_data);
setup_samplers(ctx, info);
renderer_draw_texture(ctx->renderer,
info->src->sampler_view->texture,
info->dst->x, info->dst->y,
info->dst->x + info->dst->width,
info->dst->y + info->dst->height,
info->dst->x, info->dst->y,
info->dst->x + info->dst->width,
info->dst->y + info->dst->height);
cso_restore_framebuffer(ctx->cso_context);
cso_restore_fragment_shader(ctx->cso_context);
cso_restore_viewport(ctx->cso_context);
cso_restore_blend(ctx->cso_context);
cso_restore_samplers(ctx->cso_context);
cso_restore_fragment_sampler_views(ctx->cso_context);
vg_shader_destroy(ctx, shader);
pipe_surface_reference(&dst_surf, NULL);
}
void vegaColorMatrix(VGImage dst, VGImage src,
const VGfloat * matrix)
{
struct vg_context *ctx = vg_current_context();
struct vg_image *d, *s;
struct filter_info info;
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (!matrix || !is_aligned(matrix)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
d = (struct vg_image*)dst;
s = (struct vg_image*)src;
if (vg_image_overlaps(d, s)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
info.dst = d;
info.src = s;
info.setup_shader = &setup_color_matrix;
info.user_data = NULL;
info.const_buffer = matrix;
info.const_buffer_len = 20 * sizeof(VGfloat);
info.tiling_mode = VG_TILE_PAD;
info.extra_texture_view = NULL;
execute_filter(ctx, &info);
}
static VGfloat texture_offset(VGfloat width, VGint kernelSize, VGint current, VGint shift)
{
VGfloat diff = current - shift;
return diff / width;
}
void vegaConvolve(VGImage dst, VGImage src,
VGint kernelWidth, VGint kernelHeight,
VGint shiftX, VGint shiftY,
const VGshort * kernel,
VGfloat scale,
VGfloat bias,
VGTilingMode tilingMode)
{
struct vg_context *ctx = vg_current_context();
VGfloat *buffer;
VGint buffer_len;
VGint i, j;
VGint idx = 0;
struct vg_image *d, *s;
VGint kernel_size = kernelWidth * kernelHeight;
struct filter_info info;
const VGint max_kernel_size = vgGeti(VG_MAX_KERNEL_SIZE);
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (kernelWidth <= 0 || kernelHeight <= 0 ||
kernelWidth > max_kernel_size || kernelHeight > max_kernel_size) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (!kernel || !is_aligned_to(kernel, 2)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (tilingMode < VG_TILE_FILL ||
tilingMode > VG_TILE_REFLECT) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
d = (struct vg_image*)dst;
s = (struct vg_image*)src;
if (vg_image_overlaps(d, s)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
vg_validate_state(ctx);
buffer_len = 8 + 2 * 4 * kernel_size;
buffer = (VGfloat*)malloc(buffer_len * sizeof(VGfloat));
buffer[0] = 0.f;
buffer[1] = 1.f;
buffer[2] = 2.f; /*unused*/
buffer[3] = 4.f; /*unused*/
buffer[4] = kernelWidth * kernelHeight;
buffer[5] = scale;
buffer[6] = bias;
buffer[7] = 0.f;
idx = 8;
for (j = 0; j < kernelHeight; ++j) {
for (i = 0; i < kernelWidth; ++i) {
VGint index = j * kernelWidth + i;
VGfloat x, y;
x = texture_offset(s->width, kernelWidth, i, shiftX);
y = texture_offset(s->height, kernelHeight, j, shiftY);
buffer[idx + index*4 + 0] = x;
buffer[idx + index*4 + 1] = y;
buffer[idx + index*4 + 2] = 0.f;
buffer[idx + index*4 + 3] = 0.f;
}
}
idx += kernel_size * 4;
for (j = 0; j < kernelHeight; ++j) {
for (i = 0; i < kernelWidth; ++i) {
/* transpose the kernel */
VGint index = j * kernelWidth + i;
VGint kindex = (kernelWidth - i - 1) * kernelHeight + (kernelHeight - j - 1);
buffer[idx + index*4 + 0] = kernel[kindex];
buffer[idx + index*4 + 1] = kernel[kindex];
buffer[idx + index*4 + 2] = kernel[kindex];
buffer[idx + index*4 + 3] = kernel[kindex];
}
}
info.dst = d;
info.src = s;
info.setup_shader = &setup_convolution;
info.user_data = (void*)(long)(buffer_len/4);
info.const_buffer = buffer;
info.const_buffer_len = buffer_len * sizeof(VGfloat);
info.tiling_mode = tilingMode;
info.extra_texture_view = NULL;
execute_filter(ctx, &info);
free(buffer);
}
void vegaSeparableConvolve(VGImage dst, VGImage src,
VGint kernelWidth,
VGint kernelHeight,
VGint shiftX, VGint shiftY,
const VGshort * kernelX,
const VGshort * kernelY,
VGfloat scale,
VGfloat bias,
VGTilingMode tilingMode)
{
struct vg_context *ctx = vg_current_context();
VGshort *kernel;
VGint i, j, idx = 0;
const VGint max_kernel_size = vgGeti(VG_MAX_SEPARABLE_KERNEL_SIZE);
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (kernelWidth <= 0 || kernelHeight <= 0 ||
kernelWidth > max_kernel_size || kernelHeight > max_kernel_size) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (!kernelX || !kernelY ||
!is_aligned_to(kernelX, 2) || !is_aligned_to(kernelY, 2)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (tilingMode < VG_TILE_FILL ||
tilingMode > VG_TILE_REFLECT) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
kernel = malloc(sizeof(VGshort)*kernelWidth*kernelHeight);
for (i = 0; i < kernelWidth; ++i) {
for (j = 0; j < kernelHeight; ++j) {
kernel[idx] = kernelX[i] * kernelY[j];
++idx;
}
}
vgConvolve(dst, src, kernelWidth, kernelHeight, shiftX, shiftY,
kernel, scale, bias, tilingMode);
free(kernel);
}
static INLINE VGfloat compute_gaussian_componenet(VGfloat x, VGfloat y,
VGfloat stdDeviationX,
VGfloat stdDeviationY)
{
VGfloat mult = 1 / ( 2 * M_PI * stdDeviationX * stdDeviationY);
VGfloat e = exp( - ( pow(x, 2)/(2*pow(stdDeviationX, 2)) +
pow(y, 2)/(2*pow(stdDeviationY, 2)) ) );
return mult * e;
}
static INLINE VGint compute_kernel_size(VGfloat deviation)
{
VGint size = ceil(2.146 * deviation);
if (size > 11)
return 11;
return size;
}
static void compute_gaussian_kernel(VGfloat *kernel,
VGint width, VGint height,
VGfloat stdDeviationX,
VGfloat stdDeviationY)
{
VGint i, j;
VGfloat scale = 0.0f;
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
VGint idx = (height - j -1) * width + (width - i -1);
kernel[idx] = compute_gaussian_componenet(i-(ceil(width/2))-1,
j-ceil(height/2)-1,
stdDeviationX, stdDeviationY);
scale += kernel[idx];
}
}
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
VGint idx = j * width + i;
kernel[idx] /= scale;
}
}
}
void vegaGaussianBlur(VGImage dst, VGImage src,
VGfloat stdDeviationX,
VGfloat stdDeviationY,
VGTilingMode tilingMode)
{
struct vg_context *ctx = vg_current_context();
struct vg_image *d, *s;
VGfloat *buffer, *kernel;
VGint kernel_width, kernel_height, kernel_size;
VGint buffer_len;
VGint idx, i, j;
struct filter_info info;
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (stdDeviationX <= 0 || stdDeviationY <= 0) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (tilingMode < VG_TILE_FILL ||
tilingMode > VG_TILE_REFLECT) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
d = (struct vg_image*)dst;
s = (struct vg_image*)src;
if (vg_image_overlaps(d, s)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
kernel_width = compute_kernel_size(stdDeviationX);
kernel_height = compute_kernel_size(stdDeviationY);
kernel_size = kernel_width * kernel_height;
kernel = malloc(sizeof(VGfloat)*kernel_size);
compute_gaussian_kernel(kernel, kernel_width, kernel_height,
stdDeviationX, stdDeviationY);
buffer_len = 8 + 2 * 4 * kernel_size;
buffer = (VGfloat*)malloc(buffer_len * sizeof(VGfloat));
buffer[0] = 0.f;
buffer[1] = 1.f;
buffer[2] = 2.f; /*unused*/
buffer[3] = 4.f; /*unused*/
buffer[4] = kernel_width * kernel_height;
buffer[5] = 1.f;/*scale*/
buffer[6] = 0.f;/*bias*/
buffer[7] = 0.f;
idx = 8;
for (j = 0; j < kernel_height; ++j) {
for (i = 0; i < kernel_width; ++i) {
VGint index = j * kernel_width + i;
VGfloat x, y;
x = texture_offset(s->width, kernel_width, i, kernel_width/2);
y = texture_offset(s->height, kernel_height, j, kernel_height/2);
buffer[idx + index*4 + 0] = x;
buffer[idx + index*4 + 1] = y;
buffer[idx + index*4 + 2] = 0.f;
buffer[idx + index*4 + 3] = 0.f;
}
}
idx += kernel_size * 4;
for (j = 0; j < kernel_height; ++j) {
for (i = 0; i < kernel_width; ++i) {
/* transpose the kernel */
VGint index = j * kernel_width + i;
VGint kindex = (kernel_width - i - 1) * kernel_height + (kernel_height - j - 1);
buffer[idx + index*4 + 0] = kernel[kindex];
buffer[idx + index*4 + 1] = kernel[kindex];
buffer[idx + index*4 + 2] = kernel[kindex];
buffer[idx + index*4 + 3] = kernel[kindex];
}
}
info.dst = d;
info.src = s;
info.setup_shader = &setup_convolution;
info.user_data = (void*)(long)(buffer_len/4);
info.const_buffer = buffer;
info.const_buffer_len = buffer_len * sizeof(VGfloat);
info.tiling_mode = tilingMode;
info.extra_texture_view = NULL;
execute_filter(ctx, &info);
free(buffer);
free(kernel);
}
void vegaLookup(VGImage dst, VGImage src,
const VGubyte * redLUT,
const VGubyte * greenLUT,
const VGubyte * blueLUT,
const VGubyte * alphaLUT,
VGboolean outputLinear,
VGboolean outputPremultiplied)
{
struct vg_context *ctx = vg_current_context();
struct vg_image *d, *s;
VGuint color_data[256];
VGint i;
struct pipe_sampler_view *lut_texture_view;
VGfloat buffer[4];
struct filter_info info;
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (!redLUT || !greenLUT || !blueLUT || !alphaLUT) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
d = (struct vg_image*)dst;
s = (struct vg_image*)src;
if (vg_image_overlaps(d, s)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
for (i = 0; i < 256; ++i) {
color_data[i] = blueLUT[i] << 24 | greenLUT[i] << 16 |
redLUT[i] << 8 | alphaLUT[i];
}
lut_texture_view = create_texture_1d_view(ctx, color_data, 255);
buffer[0] = 0.f;
buffer[1] = 0.f;
buffer[2] = 1.f;
buffer[3] = 1.f;
info.dst = d;
info.src = s;
info.setup_shader = &setup_lookup;
info.user_data = NULL;
info.const_buffer = buffer;
info.const_buffer_len = 4 * sizeof(VGfloat);
info.tiling_mode = VG_TILE_PAD;
info.extra_texture_view = lut_texture_view;
execute_filter(ctx, &info);
pipe_sampler_view_reference(&lut_texture_view, NULL);
}
void vegaLookupSingle(VGImage dst, VGImage src,
const VGuint * lookupTable,
VGImageChannel sourceChannel,
VGboolean outputLinear,
VGboolean outputPremultiplied)
{
struct vg_context *ctx = vg_current_context();
struct vg_image *d, *s;
struct pipe_sampler_view *lut_texture_view;
VGfloat buffer[4];
struct filter_info info;
VGuint color_data[256];
VGint i;
if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
return;
}
if (!lookupTable || !is_aligned(lookupTable)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
if (sourceChannel != VG_RED && sourceChannel != VG_GREEN &&
sourceChannel != VG_BLUE && sourceChannel != VG_ALPHA) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
d = (struct vg_image*)dst;
s = (struct vg_image*)src;
if (vg_image_overlaps(d, s)) {
vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
return;
}
vg_validate_state(ctx);
for (i = 0; i < 256; ++i) {
VGuint rgba = lookupTable[i];
VGubyte blue, green, red, alpha;
red = (rgba & 0xff000000)>>24;
green = (rgba & 0x00ff0000)>>16;
blue = (rgba & 0x0000ff00)>> 8;
alpha = (rgba & 0x000000ff)>> 0;
color_data[i] = blue << 24 | green << 16 |
red << 8 | alpha;
}
lut_texture_view = create_texture_1d_view(ctx, color_data, 256);
buffer[0] = 0.f;
buffer[1] = 0.f;
buffer[2] = 1.f;
buffer[3] = 1.f;
info.dst = d;
info.src = s;
info.setup_shader = &setup_lookup_single;
info.user_data = (void*)sourceChannel;
info.const_buffer = buffer;
info.const_buffer_len = 4 * sizeof(VGfloat);
info.tiling_mode = VG_TILE_PAD;
info.extra_texture_view = lut_texture_view;
execute_filter(ctx, &info);
pipe_sampler_view_reference(&lut_texture_view, NULL);
}
|