1#include "eglcommon.h"
2
3#include <VG/openvg.h>
4
5#include <stdio.h>
6#include <string.h>
7
8static const VGfloat white_color[4] = {1.0, 1.0, 1.0, 1.0};
9
10static VGPath path;
11static VGPaint lingrad, radgrad, pattern;
12
13VGColorRampSpreadMode spread = VG_COLOR_RAMP_SPREAD_PAD;
14
15static void
16init_linear_gradient(VGPaint paint)
17{
18   VGfloat rampStop[] = {0.00f, 1.0f, 1.0f, 1.0f, 1.0f,
19                         0.33f, 1.0f, 0.0f, 0.0f, 1.0f,
20                         0.66f, 0.0f, 1.0f, 0.0f, 1.0f,
21                         1.00f, 0.0f, 0.0f,  1.0f, 1.0f};
22   VGfloat linearGradient[4] = {25.0f, 25.0f, 75.0f, 75.0f};
23
24   vgSetParameteri(paint, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
25   vgSetParameteri(paint, VG_PAINT_COLOR_RAMP_SPREAD_MODE, spread);
26   vgSetParameterfv(paint, VG_PAINT_LINEAR_GRADIENT, 4, linearGradient);
27   vgSetParameterfv(paint, VG_PAINT_COLOR_RAMP_STOPS, 20, rampStop);
28}
29
30static void
31init_radial_gradient(VGPaint paint, VGboolean centered)
32{
33   VGfloat centeredGradient[5] = {50.0f, 50.0f, 50.0f, 50.0f, 50};
34   VGfloat noncenteredGradient[5] = {50.0f, 50.0f, 62.5f, 62.5f, 50};
35   VGfloat rampStop[] = {0.00f, 1.0f, 1.0f, 1.0f, 1.0f,
36                         0.33f, 1.0f, 0.0f, 0.0f, 1.0f,
37                         0.66f, 0.0f, 1.0f, 0.0f, 1.0f,
38                         1.00f, 0.0f, 0.0f,  1.0f, 1.0f};
39   VGfloat *radialGradient =
40      (centered) ? centeredGradient : noncenteredGradient;
41
42   vgSetParameteri(paint, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
43   vgSetParameteri(paint, VG_PAINT_COLOR_RAMP_SPREAD_MODE, spread);
44   vgSetParameterfv(paint, VG_PAINT_RADIAL_GRADIENT, 5, radialGradient);
45   vgSetParameterfv(paint, VG_PAINT_COLOR_RAMP_STOPS, 20, rampStop);
46}
47
48static void
49init_pattern(VGPaint paint)
50{
51   const VGfloat red[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
52   const VGfloat green[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
53   const VGfloat blue[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
54   const VGfloat white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
55   VGImage img;
56
57   img = vgCreateImage(VG_sRGBA_8888, 32, 32,
58                       VG_IMAGE_QUALITY_NONANTIALIASED);
59
60   vgSetfv(VG_CLEAR_COLOR, 4, red);
61   vgClearImage(img, 0, 0, 16, 16);
62   vgSetfv(VG_CLEAR_COLOR, 4, green);
63   vgClearImage(img, 16, 0, 16, 16);
64   vgSetfv(VG_CLEAR_COLOR, 4, blue);
65   vgClearImage(img, 0, 16, 16, 16);
66   vgSetfv(VG_CLEAR_COLOR, 4, white);
67   vgClearImage(img, 16, 16, 16, 16);
68
69   vgSetParameteri(paint, VG_PAINT_TYPE, VG_PAINT_TYPE_PATTERN);
70   vgSetParameteri(paint, VG_PAINT_PATTERN_TILING_MODE, VG_TILE_REPEAT);
71   vgPaintPattern(paint, img);
72}
73
74static void
75init(void)
76{
77   static const VGubyte sqrCmds[5] = {VG_MOVE_TO_ABS, VG_HLINE_TO_ABS, VG_VLINE_TO_ABS, VG_HLINE_TO_ABS, VG_CLOSE_PATH};
78   static const VGfloat sqrCoords[5]   = {0.0f, 0.0f, 100.0f, 100.0f, 0.0f};
79
80   path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1, 0, 0, 0,
81                       VG_PATH_CAPABILITY_APPEND_TO);
82   vgAppendPathData(path, 5, sqrCmds, sqrCoords);
83
84   lingrad = vgCreatePaint();
85   init_linear_gradient(lingrad);
86
87   radgrad = vgCreatePaint();
88   init_radial_gradient(radgrad, VG_TRUE);
89
90   pattern = vgCreatePaint();
91   init_pattern(pattern);
92
93   vgSetfv(VG_CLEAR_COLOR, 4, white_color);
94   vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_NONANTIALIASED);
95}
96
97/* new window size or exposure */
98static void
99reshape(int w, int h)
100{
101   vgLoadIdentity();
102}
103
104static void
105draw_with_paint(VGPaint paint, VGint x, VGint y)
106{
107   vgSetPaint(paint, VG_FILL_PATH | VG_STROKE_PATH);
108
109   /*
110    * The paint and the path are both 100x100.  Scale the paint by 2
111    * horizontally and draw the path twice to cover the scaled paint.
112    */
113
114   /* scale the paint */
115   vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
116   vgLoadIdentity();
117   vgScale(2.0f, 1);
118   vgSeti(VG_MATRIX_MODE, VG_MATRIX_STROKE_PAINT_TO_USER);
119   vgLoadIdentity();
120   vgScale(2.0f, 1);
121
122   /* draw the left half */
123   vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
124   vgLoadIdentity();
125   vgTranslate(x, y);
126   vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
127
128   /* draw the right half */
129   vgTranslate(100, 0);
130   vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
131   vgTranslate(-50, 0);
132   vgSeti(VG_MATRIX_MODE, VG_MATRIX_STROKE_PAINT_TO_USER);
133   vgTranslate(-50, 0);
134
135   vgDrawPath(path, VG_FILL_PATH | VG_STROKE_PATH);
136}
137
138static void
139draw(void)
140{
141   vgClear(0, 0, window_width(), window_height());
142
143   draw_with_paint(lingrad, 100, 25);
144   draw_with_paint(radgrad, 100, 150);
145   draw_with_paint(pattern, 100, 275);
146}
147
148
149int main(int argc, char **argv)
150{
151   set_window_size(400, 400);
152
153   return run(argc, argv, init, reshape,
154              draw, 0);
155}
156