The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include <jni.h> |
| 2 | #include "GraphicsJNI.h" |
| 3 | |
| 4 | #include "SkShader.h" |
| 5 | #include "SkGradientShader.h" |
| 6 | #include "SkPorterDuff.h" |
| 7 | #include "SkComposeShader.h" |
| 8 | #include "SkTemplates.h" |
| 9 | #include "SkXfermode.h" |
| 10 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 11 | #include <SkiaShader.h> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 12 | #include <Caches.h> |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace android::uirenderer; |
| 15 | |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 16 | static struct { |
| 17 | jclass clazz; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 18 | jfieldID shader; |
| 19 | } gShaderClassInfo; |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | static void ThrowIAE_IfNull(JNIEnv* env, void* ptr) { |
| 22 | if (NULL == ptr) { |
| 23 | doThrowIAE(env); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | static void Color_RGBToHSV(JNIEnv* env, jobject, int red, int green, int blue, jfloatArray hsvArray) |
| 28 | { |
| 29 | SkScalar hsv[3]; |
| 30 | SkRGBToHSV(red, green, blue, hsv); |
| 31 | |
| 32 | AutoJavaFloatArray autoHSV(env, hsvArray, 3); |
| 33 | float* values = autoHSV.ptr(); |
| 34 | for (int i = 0; i < 3; i++) { |
| 35 | values[i] = SkScalarToFloat(hsv[i]); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static int Color_HSVToColor(JNIEnv* env, jobject, int alpha, jfloatArray hsvArray) |
| 40 | { |
| 41 | AutoJavaFloatArray autoHSV(env, hsvArray, 3); |
| 42 | float* values = autoHSV.ptr();; |
| 43 | SkScalar hsv[3]; |
| 44 | |
| 45 | for (int i = 0; i < 3; i++) { |
| 46 | hsv[i] = SkFloatToScalar(values[i]); |
| 47 | } |
| 48 | |
| 49 | return SkHSVToColor(alpha, hsv); |
| 50 | } |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 53 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 54 | static void Shader_destructor(JNIEnv* env, jobject o, SkShader* shader, SkiaShader* skiaShader) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | { |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 56 | #ifdef USE_OPENGL_RENDERER |
| 57 | if (android::uirenderer::Caches::hasInstance()) { |
| 58 | android::uirenderer::Caches::getInstance().gradientCache.remove(shader); |
| 59 | } |
| 60 | #endif |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 61 | delete skiaShader; |
Mike Reed | 6af2552 | 2009-07-30 11:46:47 -0400 | [diff] [blame] | 62 | shader->safeUnref(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static bool Shader_getLocalMatrix(JNIEnv* env, jobject, const SkShader* shader, SkMatrix* matrix) |
| 66 | { |
Mike Reed | a04e555 | 2009-07-30 11:05:57 -0400 | [diff] [blame] | 67 | return shader ? shader->getLocalMatrix(matrix) : false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 70 | static void Shader_setLocalMatrix(JNIEnv* env, jobject o, SkShader* shader, SkiaShader* skiaShader, |
| 71 | const SkMatrix* matrix) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
Mike Reed | a04e555 | 2009-07-30 11:05:57 -0400 | [diff] [blame] | 73 | if (shader) { |
| 74 | if (NULL == matrix) { |
| 75 | shader->resetLocalMatrix(); |
| 76 | } |
| 77 | else { |
| 78 | shader->setLocalMatrix(*matrix); |
| 79 | } |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 80 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 81 | skiaShader->setMatrix(const_cast<SkMatrix*>(matrix)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 82 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 87 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 88 | static SkShader* BitmapShader_constructor(JNIEnv* env, jobject o, const SkBitmap* bitmap, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | int tileModeX, int tileModeY) |
| 90 | { |
| 91 | SkShader* s = SkShader::CreateBitmapShader(*bitmap, |
| 92 | (SkShader::TileMode)tileModeX, |
| 93 | (SkShader::TileMode)tileModeY); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | ThrowIAE_IfNull(env, s); |
| 96 | return s; |
| 97 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 98 | |
| 99 | static SkiaShader* BitmapShader_postConstructor(JNIEnv* env, jobject o, SkShader* shader, |
| 100 | SkBitmap* bitmap, int tileModeX, int tileModeY) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 101 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 102 | SkiaShader* skiaShader = new SkiaBitmapShader(bitmap, shader, |
| 103 | static_cast<SkShader::TileMode>(tileModeX), static_cast<SkShader::TileMode>(tileModeY), |
| 104 | NULL, (shader->getFlags() & SkShader::kOpaqueAlpha_Flag) == 0); |
| 105 | return skiaShader; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 106 | #else |
| 107 | return NULL; |
| 108 | #endif |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
| 111 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 112 | |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 113 | static SkShader* LinearGradient_create1(JNIEnv* env, jobject o, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | float x0, float y0, float x1, float y1, |
| 115 | jintArray colorArray, jfloatArray posArray, int tileMode) |
| 116 | { |
| 117 | SkPoint pts[2]; |
| 118 | pts[0].set(SkFloatToScalar(x0), SkFloatToScalar(y0)); |
| 119 | pts[1].set(SkFloatToScalar(x1), SkFloatToScalar(y1)); |
| 120 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 121 | size_t count = env->GetArrayLength(colorArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | const jint* colorValues = env->GetIntArrayElements(colorArray, NULL); |
| 123 | |
| 124 | SkAutoSTMalloc<8, SkScalar> storage(posArray ? count : 0); |
| 125 | SkScalar* pos = NULL; |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | if (posArray) { |
| 128 | AutoJavaFloatArray autoPos(env, posArray, count); |
| 129 | const float* posValues = autoPos.ptr(); |
| 130 | pos = (SkScalar*)storage.get(); |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 131 | for (size_t i = 0; i < count; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | pos[i] = SkFloatToScalar(posValues[i]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | SkShader* shader = SkGradientShader::CreateLinear(pts, |
| 137 | reinterpret_cast<const SkColor*>(colorValues), |
| 138 | pos, count, |
| 139 | static_cast<SkShader::TileMode>(tileMode)); |
| 140 | |
| 141 | env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues), JNI_ABORT); |
| 142 | ThrowIAE_IfNull(env, shader); |
| 143 | return shader; |
| 144 | } |
| 145 | |
| 146 | static SkiaShader* LinearGradient_postCreate1(JNIEnv* env, jobject o, SkShader* shader, |
| 147 | float x0, float y0, float x1, float y1, jintArray colorArray, |
| 148 | jfloatArray posArray, int tileMode) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 149 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 150 | size_t count = env->GetArrayLength(colorArray); |
| 151 | const jint* colorValues = env->GetIntArrayElements(colorArray, NULL); |
| 152 | |
| 153 | jfloat* storedBounds = new jfloat[4]; |
| 154 | storedBounds[0] = x0; storedBounds[1] = y0; |
| 155 | storedBounds[2] = x1; storedBounds[3] = y1; |
| 156 | jfloat* storedPositions = new jfloat[count]; |
| 157 | uint32_t* storedColors = new uint32_t[count]; |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 158 | for (size_t i = 0; i < count; i++) { |
| 159 | storedColors[i] = static_cast<uint32_t>(colorValues[i]); |
| 160 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 161 | |
| 162 | if (posArray) { |
| 163 | AutoJavaFloatArray autoPos(env, posArray, count); |
| 164 | const float* posValues = autoPos.ptr(); |
| 165 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 166 | storedPositions[i] = posValues[i]; |
| 167 | } |
| 168 | } else { |
| 169 | storedPositions[0] = 0.0f; |
| 170 | storedPositions[1] = 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 172 | |
| 173 | SkiaShader* skiaShader = new SkiaLinearGradientShader(storedBounds, storedColors, |
| 174 | storedPositions, count, shader, static_cast<SkShader::TileMode>(tileMode), NULL, |
| 175 | (shader->getFlags() & SkShader::kOpaqueAlpha_Flag) == 0); |
| 176 | |
| 177 | env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues), JNI_ABORT); |
| 178 | return skiaShader; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 179 | #else |
| 180 | return NULL; |
| 181 | #endif |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static SkiaShader* LinearGradient_postCreate2(JNIEnv* env, jobject o, SkShader* shader, |
| 185 | float x0, float y0, float x1, float y1, int color0, int color1, int tileMode) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 186 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 187 | float* storedBounds = new float[4]; |
| 188 | storedBounds[0] = x0; storedBounds[1] = y0; |
| 189 | storedBounds[2] = x1; storedBounds[3] = y1; |
| 190 | |
| 191 | float* storedPositions = new float[2]; |
| 192 | storedPositions[0] = 0.0f; |
| 193 | storedPositions[1] = 1.0f; |
| 194 | |
| 195 | uint32_t* storedColors = new uint32_t[2]; |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 196 | storedColors[0] = static_cast<uint32_t>(color0); |
| 197 | storedColors[1] = static_cast<uint32_t>(color1); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 198 | |
| 199 | SkiaShader* skiaShader = new SkiaLinearGradientShader(storedBounds, storedColors, |
| 200 | storedPositions, 2, shader, static_cast<SkShader::TileMode>(tileMode), NULL, |
| 201 | (shader->getFlags() & SkShader::kOpaqueAlpha_Flag) == 0); |
| 202 | |
| 203 | return skiaShader; |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 204 | #else |
| 205 | return NULL; |
| 206 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 209 | static SkShader* LinearGradient_create2(JNIEnv* env, jobject o, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | float x0, float y0, float x1, float y1, |
| 211 | int color0, int color1, int tileMode) |
| 212 | { |
| 213 | SkPoint pts[2]; |
| 214 | pts[0].set(SkFloatToScalar(x0), SkFloatToScalar(y0)); |
| 215 | pts[1].set(SkFloatToScalar(x1), SkFloatToScalar(y1)); |
| 216 | |
| 217 | SkColor colors[2]; |
| 218 | colors[0] = color0; |
| 219 | colors[1] = color1; |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, (SkShader::TileMode)tileMode); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | ThrowIAE_IfNull(env, s); |
| 224 | return s; |
| 225 | } |
| 226 | |
| 227 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 228 | |
| 229 | static SkShader* RadialGradient_create1(JNIEnv* env, jobject, |
| 230 | float x, float y, float radius, |
| 231 | jintArray colorArray, jfloatArray posArray, int tileMode) |
| 232 | { |
| 233 | SkPoint center; |
| 234 | center.set(SkFloatToScalar(x), SkFloatToScalar(y)); |
| 235 | |
| 236 | size_t count = env->GetArrayLength(colorArray); |
| 237 | const jint* colorValues = env->GetIntArrayElements(colorArray, NULL); |
| 238 | |
| 239 | SkAutoSTMalloc<8, SkScalar> storage(posArray ? count : 0); |
| 240 | SkScalar* pos = NULL; |
| 241 | |
| 242 | if (posArray) { |
| 243 | AutoJavaFloatArray autoPos(env, posArray, count); |
| 244 | const float* posValues = autoPos.ptr(); |
| 245 | pos = (SkScalar*)storage.get(); |
| 246 | for (size_t i = 0; i < count; i++) |
| 247 | pos[i] = SkFloatToScalar(posValues[i]); |
| 248 | } |
| 249 | |
| 250 | SkShader* shader = SkGradientShader::CreateRadial(center, |
| 251 | SkFloatToScalar(radius), |
| 252 | reinterpret_cast<const SkColor*>(colorValues), |
| 253 | pos, count, |
| 254 | static_cast<SkShader::TileMode>(tileMode)); |
| 255 | env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues), |
| 256 | JNI_ABORT); |
| 257 | |
| 258 | ThrowIAE_IfNull(env, shader); |
| 259 | return shader; |
| 260 | } |
| 261 | |
| 262 | static SkShader* RadialGradient_create2(JNIEnv* env, jobject, |
| 263 | float x, float y, float radius, |
| 264 | int color0, int color1, int tileMode) |
| 265 | { |
| 266 | SkPoint center; |
| 267 | center.set(SkFloatToScalar(x), SkFloatToScalar(y)); |
| 268 | |
| 269 | SkColor colors[2]; |
| 270 | colors[0] = color0; |
| 271 | colors[1] = color1; |
| 272 | |
| 273 | SkShader* s = SkGradientShader::CreateRadial(center, SkFloatToScalar(radius), colors, NULL, |
| 274 | 2, (SkShader::TileMode)tileMode); |
| 275 | ThrowIAE_IfNull(env, s); |
| 276 | return s; |
| 277 | } |
| 278 | |
| 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
| 281 | static SkShader* SweepGradient_create1(JNIEnv* env, jobject, float x, float y, |
| 282 | jintArray jcolors, jfloatArray jpositions) |
| 283 | { |
| 284 | size_t count = env->GetArrayLength(jcolors); |
| 285 | const jint* colors = env->GetIntArrayElements(jcolors, NULL); |
| 286 | |
| 287 | SkAutoSTMalloc<8, SkScalar> storage(jpositions ? count : 0); |
| 288 | SkScalar* pos = NULL; |
| 289 | |
| 290 | if (NULL != jpositions) { |
| 291 | AutoJavaFloatArray autoPos(env, jpositions, count); |
| 292 | const float* posValues = autoPos.ptr(); |
| 293 | pos = (SkScalar*)storage.get(); |
| 294 | for (size_t i = 0; i < count; i++) { |
| 295 | pos[i] = SkFloatToScalar(posValues[i]); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | SkShader* shader = SkGradientShader::CreateSweep(SkFloatToScalar(x), |
| 300 | SkFloatToScalar(y), |
| 301 | reinterpret_cast<const SkColor*>(colors), |
| 302 | pos, count); |
| 303 | env->ReleaseIntArrayElements(jcolors, const_cast<jint*>(colors), |
| 304 | JNI_ABORT); |
| 305 | ThrowIAE_IfNull(env, shader); |
| 306 | return shader; |
| 307 | } |
| 308 | |
| 309 | static SkShader* SweepGradient_create2(JNIEnv* env, jobject, float x, float y, |
| 310 | int color0, int color1) |
| 311 | { |
| 312 | SkColor colors[2]; |
| 313 | colors[0] = color0; |
| 314 | colors[1] = color1; |
| 315 | SkShader* s = SkGradientShader::CreateSweep(SkFloatToScalar(x), SkFloatToScalar(y), |
| 316 | colors, NULL, 2); |
| 317 | ThrowIAE_IfNull(env, s); |
| 318 | return s; |
| 319 | } |
| 320 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame^] | 321 | static SkiaShader* SweepGradient_postCreate1(JNIEnv* env, jobject o, SkShader* shader, |
| 322 | float x, float y, jintArray colorArray, jfloatArray posArray) { |
| 323 | #ifdef USE_OPENGL_RENDERER |
| 324 | size_t count = env->GetArrayLength(colorArray); |
| 325 | const jint* colorValues = env->GetIntArrayElements(colorArray, NULL); |
| 326 | |
| 327 | jfloat* storedPositions = new jfloat[count]; |
| 328 | uint32_t* storedColors = new uint32_t[count]; |
| 329 | for (size_t i = 0; i < count; i++) { |
| 330 | storedColors[i] = static_cast<uint32_t>(colorValues[i]); |
| 331 | } |
| 332 | |
| 333 | if (posArray) { |
| 334 | AutoJavaFloatArray autoPos(env, posArray, count); |
| 335 | const float* posValues = autoPos.ptr(); |
| 336 | for (size_t i = 0; i < count; i++) { |
| 337 | storedPositions[i] = posValues[i]; |
| 338 | } |
| 339 | } else { |
| 340 | storedPositions[0] = 0.0f; |
| 341 | storedPositions[1] = 1.0f; |
| 342 | } |
| 343 | |
| 344 | SkiaShader* skiaShader = new SkiaSweepGradientShader(x, y, storedColors, storedPositions, count, |
| 345 | shader, NULL, (shader->getFlags() & SkShader::kOpaqueAlpha_Flag) == 0); |
| 346 | |
| 347 | env->ReleaseIntArrayElements(colorArray, const_cast<jint*>(colorValues), JNI_ABORT); |
| 348 | return skiaShader; |
| 349 | #else |
| 350 | return NULL; |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | static SkiaShader* SweepGradient_postCreate2(JNIEnv* env, jobject o, SkShader* shader, |
| 355 | float x, float y, int color0, int color1) { |
| 356 | #ifdef USE_OPENGL_RENDERER |
| 357 | float* storedPositions = new float[2]; |
| 358 | storedPositions[0] = 0.0f; |
| 359 | storedPositions[1] = 1.0f; |
| 360 | |
| 361 | uint32_t* storedColors = new uint32_t[2]; |
| 362 | storedColors[0] = static_cast<uint32_t>(color0); |
| 363 | storedColors[1] = static_cast<uint32_t>(color1); |
| 364 | |
| 365 | SkiaShader* skiaShader = new SkiaSweepGradientShader(x, y, storedColors, storedPositions, 2, |
| 366 | shader, NULL, (shader->getFlags() & SkShader::kOpaqueAlpha_Flag) == 0); |
| 367 | |
| 368 | return skiaShader; |
| 369 | #else |
| 370 | return NULL; |
| 371 | #endif |
| 372 | } |
| 373 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 376 | static SkShader* ComposeShader_create1(JNIEnv* env, jobject o, |
| 377 | SkShader* shaderA, SkShader* shaderB, SkXfermode* mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | { |
| 379 | return new SkComposeShader(shaderA, shaderB, mode); |
| 380 | } |
| 381 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 382 | static SkShader* ComposeShader_create2(JNIEnv* env, jobject o, |
| 383 | SkShader* shaderA, SkShader* shaderB, SkPorterDuff::Mode porterDuffMode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 385 | SkAutoUnref au(SkPorterDuff::CreateXfermode(porterDuffMode)); |
| 386 | SkXfermode* mode = (SkXfermode*) au.get(); |
| 387 | return new SkComposeShader(shaderA, shaderB, mode); |
| 388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 390 | static SkiaShader* ComposeShader_postCreate2(JNIEnv* env, jobject o, SkShader* shader, |
| 391 | SkiaShader* shaderA, SkiaShader* shaderB, SkPorterDuff::Mode porterDuffMode) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 392 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 393 | SkXfermode::Mode mode = SkPorterDuff::ToXfermodeMode(porterDuffMode); |
| 394 | return new SkiaComposeShader(shaderA, shaderB, mode, shader); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 395 | #else |
| 396 | return NULL; |
| 397 | #endif |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static SkiaShader* ComposeShader_postCreate1(JNIEnv* env, jobject o, SkShader* shader, |
| 401 | SkiaShader* shaderA, SkiaShader* shaderB, SkXfermode* mode) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 402 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 403 | SkXfermode::Mode skiaMode; |
| 404 | if (!SkXfermode::IsMode(mode, &skiaMode)) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 405 | // TODO: Support other modes |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 406 | skiaMode = SkXfermode::kSrcOver_Mode; |
| 407 | } |
| 408 | return new SkiaComposeShader(shaderA, shaderB, skiaMode, shader); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 409 | #else |
| 410 | return NULL; |
| 411 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 415 | |
| 416 | static JNINativeMethod gColorMethods[] = { |
| 417 | { "nativeRGBToHSV", "(III[F)V", (void*)Color_RGBToHSV }, |
| 418 | { "nativeHSVToColor", "(I[F)I", (void*)Color_HSVToColor } |
| 419 | }; |
| 420 | |
| 421 | static JNINativeMethod gShaderMethods[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 422 | { "nativeDestructor", "(II)V", (void*)Shader_destructor }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | { "nativeGetLocalMatrix", "(II)Z", (void*)Shader_getLocalMatrix }, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 424 | { "nativeSetLocalMatrix", "(III)V", (void*)Shader_setLocalMatrix } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | static JNINativeMethod gBitmapShaderMethods[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 428 | { "nativeCreate", "(III)I", (void*)BitmapShader_constructor }, |
| 429 | { "nativePostCreate", "(IIII)I", (void*)BitmapShader_postConstructor } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | static JNINativeMethod gLinearGradientMethods[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 433 | { "nativeCreate1", "(FFFF[I[FI)I", (void*)LinearGradient_create1 }, |
| 434 | { "nativeCreate2", "(FFFFIII)I", (void*)LinearGradient_create2 }, |
| 435 | { "nativePostCreate1", "(IFFFF[I[FI)I", (void*)LinearGradient_postCreate1 }, |
| 436 | { "nativePostCreate2", "(IFFFFIII)I", (void*)LinearGradient_postCreate2 } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | static JNINativeMethod gRadialGradientMethods[] = { |
| 440 | {"nativeCreate1", "(FFF[I[FI)I", (void*)RadialGradient_create1 }, |
| 441 | {"nativeCreate2", "(FFFIII)I", (void*)RadialGradient_create2 } |
| 442 | }; |
| 443 | |
| 444 | static JNINativeMethod gSweepGradientMethods[] = { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame^] | 445 | {"nativeCreate1", "(FF[I[F)I", (void*)SweepGradient_create1 }, |
| 446 | {"nativeCreate2", "(FFII)I", (void*)SweepGradient_create2 }, |
| 447 | { "nativePostCreate1", "(IFF[I[F)I", (void*)SweepGradient_postCreate1 }, |
| 448 | { "nativePostCreate2", "(IFFII)I", (void*)SweepGradient_postCreate2 } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | static JNINativeMethod gComposeShaderMethods[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 452 | {"nativeCreate1", "(III)I", (void*)ComposeShader_create1 }, |
| 453 | {"nativeCreate2", "(III)I", (void*)ComposeShader_create2 }, |
| 454 | {"nativePostCreate1", "(IIII)I", (void*)ComposeShader_postCreate1 }, |
| 455 | {"nativePostCreate2", "(IIII)I", (void*)ComposeShader_postCreate2 } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | #include <android_runtime/AndroidRuntime.h> |
| 459 | |
| 460 | #define REG(env, name, array) \ |
| 461 | result = android::AndroidRuntime::registerNativeMethods(env, name, array, SK_ARRAY_COUNT(array)); \ |
| 462 | if (result < 0) return result |
| 463 | |
| 464 | int register_android_graphics_Shader(JNIEnv* env); |
| 465 | int register_android_graphics_Shader(JNIEnv* env) |
| 466 | { |
| 467 | int result; |
| 468 | |
| 469 | REG(env, "android/graphics/Color", gColorMethods); |
| 470 | REG(env, "android/graphics/Shader", gShaderMethods); |
| 471 | REG(env, "android/graphics/BitmapShader", gBitmapShaderMethods); |
| 472 | REG(env, "android/graphics/LinearGradient", gLinearGradientMethods); |
| 473 | REG(env, "android/graphics/RadialGradient", gRadialGradientMethods); |
| 474 | REG(env, "android/graphics/SweepGradient", gSweepGradientMethods); |
| 475 | REG(env, "android/graphics/ComposeShader", gComposeShaderMethods); |
| 476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | return result; |
| 478 | } |
| 479 | |