812f8bba335893cbb8bcb6a9c4e3e44d91758a00
[platform/upstream/libSkiaSharp.git] / src / c / sk_shader.cpp
1 /*
2  * Copyright 2016 Xamarin Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "SkShader.h"
9 #include "SkComposeShader.h"
10 #include "SkColorFilter.h"
11 #include "SkGradientShader.h"
12 #include "SkPerlinNoiseShader.h"
13
14 #include "sk_shader.h"
15
16 #include "sk_types_priv.h"
17
18 sk_shader_t* sk_shader_new_empty() {
19     return ToShader(SkShader::MakeEmptyShader().release());
20 }
21
22 sk_shader_t* sk_shader_new_color(sk_color_t color) {
23     return ToShader(SkShader::MakeColorShader(color).release());
24 }
25
26 sk_shader_t* sk_shader_new_bitmap(const sk_bitmap_t* src,
27                                   sk_shader_tilemode_t tmx,
28                                   sk_shader_tilemode_t tmy,
29                                   const sk_matrix_t* localMatrix) {
30     SkMatrix matrix;
31     if (localMatrix) {
32         from_c(localMatrix, &matrix);
33     } else {
34         matrix.setIdentity();
35     }
36     sk_sp<SkShader> s = SkShader::MakeBitmapShader(
37         *AsBitmap(src),
38         (SkShader::TileMode)tmx,
39         (SkShader::TileMode)tmy,
40         &matrix);
41     return ToShader(s.release());
42 }
43
44 sk_shader_t* sk_shader_new_picture(sk_picture_t* src,
45                                   sk_shader_tilemode_t tmx,
46                                   sk_shader_tilemode_t tmy,
47                                   const sk_matrix_t* localMatrix,
48                                   const sk_rect_t* tile) {
49     SkMatrix matrix;
50     if (localMatrix) {
51         from_c(localMatrix, &matrix);
52     }
53     else {
54         matrix.setIdentity();
55     }
56     sk_sp<SkShader> s = SkShader::MakePictureShader(
57         sk_ref_sp(AsPicture(src)),
58         (SkShader::TileMode)tmx,
59         (SkShader::TileMode)tmy,
60         &matrix,
61         AsRect(tile));
62     return ToShader(s.release());
63 }
64
65 sk_shader_t* sk_shader_new_color_filter(sk_shader_t* proxy,
66                                         sk_colorfilter_t* filter) {
67     sk_sp<SkShader> s = AsShader(proxy)->makeWithColorFilter(sk_ref_sp(AsColorFilter(filter)));
68     return ToShader(s.release());
69 }
70
71 sk_shader_t* sk_shader_new_local_matrix(sk_shader_t* proxy,
72                                         const sk_matrix_t* localMatrix) {
73     SkMatrix matrix;
74     if (localMatrix) {
75         from_c(localMatrix, &matrix);
76     } else {
77         matrix.setIdentity();
78     }
79     sk_sp<SkShader> s = AsShader(proxy)->makeWithLocalMatrix(matrix);
80     return ToShader(s.release());
81 }
82
83 sk_shader_t* sk_shader_new_perlin_noise_fractal_noise(
84     float baseFrequencyX,
85     float baseFrequencyY,
86     int numOctaves,
87     float seed,
88     const sk_isize_t* ctileSize) {
89
90     const SkISize* tileSize = AsISize(ctileSize);
91     sk_sp<SkShader> s = SkPerlinNoiseShader::MakeFractalNoise(
92         baseFrequencyX,
93         baseFrequencyY,
94         numOctaves,
95         seed,
96         tileSize);
97     return ToShader(s.release());
98 }
99
100 sk_shader_t* sk_shader_new_perlin_noise_turbulence(
101     float baseFrequencyX,
102     float baseFrequencyY,
103     int numOctaves,
104     float seed,
105     const sk_isize_t* ctileSize) {
106
107     const SkISize* tileSize = AsISize(ctileSize);
108     sk_sp<SkShader> s = SkPerlinNoiseShader::MakeTurbulence(
109         baseFrequencyX,
110         baseFrequencyY, 
111         numOctaves, 
112         seed, 
113         tileSize);
114     return ToShader(s.release());
115 }
116
117 sk_shader_t* sk_shader_new_compose(
118     sk_shader_t* shaderA,
119     sk_shader_t* shaderB) {
120
121     sk_sp<SkShader> s = SkShader::MakeComposeShader(
122         sk_ref_sp(AsShader(shaderA)),
123         sk_ref_sp(AsShader(shaderB)),
124         SkXfermode::kSrcOver_Mode);
125     return ToShader(s.release());
126 }
127
128 sk_shader_t* sk_shader_new_compose_with_mode(
129     sk_shader_t* shaderA,
130     sk_shader_t* shaderB,
131     sk_xfermode_mode_t cmode) {
132
133     sk_sp<SkShader> s = SkShader::MakeComposeShader(
134         sk_ref_sp(AsShader(shaderA)),
135         sk_ref_sp(AsShader(shaderB)),
136         (SkXfermode::Mode)cmode);
137     return ToShader(s.release());
138 }
139
140 void sk_shader_ref(sk_shader_t* cshader) {
141     SkSafeRef(AsShader(cshader));
142 }
143
144 void sk_shader_unref(sk_shader_t* cshader) {
145     SkSafeUnref(AsShader(cshader));
146 }
147
148 sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
149                                            const sk_color_t colors[],
150                                            const float colorPos[],
151                                            int colorCount,
152                                            sk_shader_tilemode_t cmode,
153                                            const sk_matrix_t* cmatrix) {
154     SkMatrix matrix;
155     if (cmatrix) {
156         from_c(cmatrix, &matrix);
157     } else {
158         matrix.setIdentity();
159     }
160     return (sk_shader_t*)SkGradientShader::MakeLinear(reinterpret_cast<const SkPoint*>(pts),
161                                                       reinterpret_cast<const SkColor*>(colors),
162                                                       colorPos, colorCount,
163                                                       (SkShader::TileMode)cmode, 0, &matrix).release();
164 }
165
166 sk_shader_t* sk_shader_new_radial_gradient(const sk_point_t* ccenter,
167                                            float radius,
168                                            const sk_color_t colors[],
169                                            const float colorPos[],
170                                            int colorCount,
171                                            sk_shader_tilemode_t cmode,
172                                            const sk_matrix_t* cmatrix) {
173     SkMatrix matrix;
174     if (cmatrix) {
175         from_c(cmatrix, &matrix);
176     } else {
177         matrix.setIdentity();
178     }
179     SkPoint center = AsPoint(*ccenter);
180     return (sk_shader_t*)SkGradientShader::MakeRadial(center, (SkScalar)radius,
181                                                       reinterpret_cast<const SkColor*>(colors),
182                                                       reinterpret_cast<const SkScalar*>(colorPos),
183                                                       colorCount, (SkShader::TileMode)cmode, 0, &matrix).release();
184 }
185
186 sk_shader_t* sk_shader_new_sweep_gradient(const sk_point_t* ccenter,
187                                           const sk_color_t colors[],
188                                           const float colorPos[],
189                                           int colorCount,
190                                           const sk_matrix_t* cmatrix) {
191     SkMatrix matrix;
192     if (cmatrix) {
193         from_c(cmatrix, &matrix);
194     } else {
195         matrix.setIdentity();
196     }
197     return (sk_shader_t*)SkGradientShader::MakeSweep((SkScalar)(ccenter->x),
198                                                      (SkScalar)(ccenter->y),
199                                                      reinterpret_cast<const SkColor*>(colors),
200                                                      reinterpret_cast<const SkScalar*>(colorPos),
201                                                      colorCount, 0, &matrix).release();
202 }
203
204 sk_shader_t* sk_shader_new_two_point_conical_gradient(const sk_point_t* start,
205                                                       float startRadius,
206                                                       const sk_point_t* end,
207                                                       float endRadius,
208                                                       const sk_color_t colors[],
209                                                       const float colorPos[],
210                                                       int colorCount,
211                                                       sk_shader_tilemode_t cmode,
212                                                       const sk_matrix_t* cmatrix) {
213     SkMatrix matrix;
214     if (cmatrix) {
215         from_c(cmatrix, &matrix);
216     } else {
217         matrix.setIdentity();
218     }
219     SkPoint skstart = AsPoint(*start);
220     SkPoint skend = AsPoint(*end);
221     return (sk_shader_t*)SkGradientShader::MakeTwoPointConical(skstart, (SkScalar)startRadius,
222                                                         skend, (SkScalar)endRadius,
223                                                         reinterpret_cast<const SkColor*>(colors),
224                                                         reinterpret_cast<const SkScalar*>(colorPos),
225                                                         colorCount, (SkShader::TileMode)cmode, 0, &matrix).release();
226 }