Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 1 | #undef LOG_TAG |
| 2 | #define LOG_TAG "ShaderJNI" |
| 3 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | #include "GraphicsJNI.h" |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 5 | #include "SkColorFilter.h" |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 6 | #include "SkGradientShader.h" |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 7 | #include "SkImagePriv.h" |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 8 | #include "SkShader.h" |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 9 | #include "SkBlendMode.h" |
Brian Osman | bc44fa0 | 2020-01-03 15:28:06 -0500 | [diff] [blame] | 10 | #include "include/effects/SkRuntimeEffect.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 14 | using namespace android::uirenderer; |
| 15 | |
Derek Sollenberger | 669b15a | 2017-03-31 12:09:24 -0400 | [diff] [blame] | 16 | /** |
| 17 | * By default Skia gradients will interpolate their colors in unpremul space |
| 18 | * and then premultiply each of the results. We must set this flag to preserve |
| 19 | * backwards compatiblity by premultiplying the colors of the gradient first, |
| 20 | * and then interpolating between them. |
| 21 | */ |
| 22 | static const uint32_t sGradientShaderFlags = SkGradientShader::kInterpolateColorsInPremul_Flag; |
| 23 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 24 | #define ThrowIAE_IfNull(env, ptr) \ |
| 25 | if (nullptr == ptr) { \ |
| 26 | doThrowIAE(env); \ |
| 27 | return 0; \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 30 | static void Color_RGBToHSV(JNIEnv* env, jobject, jint red, jint green, jint blue, jfloatArray hsvArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | { |
| 32 | SkScalar hsv[3]; |
| 33 | SkRGBToHSV(red, green, blue, hsv); |
| 34 | |
| 35 | AutoJavaFloatArray autoHSV(env, hsvArray, 3); |
| 36 | float* values = autoHSV.ptr(); |
| 37 | for (int i = 0; i < 3; i++) { |
| 38 | values[i] = SkScalarToFloat(hsv[i]); |
| 39 | } |
| 40 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 41 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 42 | static jint Color_HSVToColor(JNIEnv* env, jobject, jint alpha, jfloatArray hsvArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | { |
| 44 | AutoJavaFloatArray autoHSV(env, hsvArray, 3); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 45 | #ifdef SK_SCALAR_IS_FLOAT |
| 46 | SkScalar* hsv = autoHSV.ptr(); |
| 47 | #else |
| 48 | #error Need to convert float array to SkScalar array before calling the following function. |
| 49 | #endif |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 50 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 51 | return static_cast<jint>(SkHSVToColor(alpha, hsv)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 56 | static void Shader_safeUnref(SkShader* shader) { |
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 57 | SkSafeUnref(shader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
John Reck | 8bde0be | 2017-05-19 10:55:20 -0700 | [diff] [blame] | 60 | static jlong Shader_getNativeFinalizer(JNIEnv*, jobject) { |
| 61 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Shader_safeUnref)); |
| 62 | } |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 66 | static jlong BitmapShader_constructor(JNIEnv* env, jobject o, jlong matrixPtr, jlong bitmapHandle, |
Mike Reed | 3ed3089 | 2020-12-11 12:25:30 -0500 | [diff] [blame] | 67 | jint tileModeX, jint tileModeY, bool filter) { |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 68 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 69 | sk_sp<SkImage> image; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 70 | if (bitmapHandle) { |
Chris Craik | b786bbd | 2015-06-10 16:58:42 -0700 | [diff] [blame] | 71 | // Only pass a valid SkBitmap object to the constructor if the Bitmap exists. Otherwise, |
| 72 | // we'll pass an empty SkBitmap to avoid crashing/excepting for compatibility. |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 73 | image = android::bitmap::toBitmap(bitmapHandle).makeImage(); |
Chris Craik | b786bbd | 2015-06-10 16:58:42 -0700 | [diff] [blame] | 74 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 75 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 76 | if (!image.get()) { |
| 77 | SkBitmap bitmap; |
| 78 | image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode); |
| 79 | } |
Mike Reed | 3ed3089 | 2020-12-11 12:25:30 -0500 | [diff] [blame] | 80 | SkSamplingOptions sampling(filter ? SkFilterMode::kLinear : SkFilterMode::kNearest, |
| 81 | SkMipmapMode::kNone); |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 82 | sk_sp<SkShader> shader = image->makeShader( |
Mike Reed | 3ed3089 | 2020-12-11 12:25:30 -0500 | [diff] [blame] | 83 | (SkTileMode)tileModeX, (SkTileMode)tileModeY, sampling); |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 84 | ThrowIAE_IfNull(env, shader.get()); |
Derek Sollenberger | 450756a | 2016-11-07 15:55:47 -0500 | [diff] [blame] | 85 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 86 | if (matrix) { |
| 87 | shader = shader->makeWithLocalMatrix(*matrix); |
| 88 | } |
ztenghui | b244fb5 | 2017-04-04 17:33:40 -0700 | [diff] [blame] | 89 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 90 | return reinterpret_cast<jlong>(shader.release()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 94 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 95 | static std::vector<SkColor4f> convertColorLongs(JNIEnv* env, jlongArray colorArray) { |
| 96 | const size_t count = env->GetArrayLength(colorArray); |
| 97 | const jlong* colorValues = env->GetLongArrayElements(colorArray, nullptr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 99 | std::vector<SkColor4f> colors(count); |
| 100 | for (size_t i = 0; i < count; ++i) { |
| 101 | colors[i] = GraphicsJNI::convertColorLong(colorValues[i]); |
ztenghui | b244fb5 | 2017-04-04 17:33:40 -0700 | [diff] [blame] | 102 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 103 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 104 | env->ReleaseLongArrayElements(colorArray, const_cast<jlong*>(colorValues), JNI_ABORT); |
| 105 | return colors; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 109 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 110 | static jlong LinearGradient_create(JNIEnv* env, jobject, jlong matrixPtr, |
| 111 | jfloat x0, jfloat y0, jfloat x1, jfloat y1, jlongArray colorArray, |
Leon Scroggins III | 2befe1d | 2019-01-31 13:19:26 -0500 | [diff] [blame] | 112 | jfloatArray posArray, jint tileMode, jlong colorSpaceHandle) { |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 113 | SkPoint pts[2]; |
| 114 | pts[0].set(x0, y0); |
| 115 | pts[1].set(x1, y1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 117 | std::vector<SkColor4f> colors = convertColorLongs(env, colorArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 119 | AutoJavaFloatArray autoPos(env, posArray, colors.size()); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 120 | #ifdef SK_SCALAR_IS_FLOAT |
| 121 | SkScalar* pos = autoPos.ptr(); |
| 122 | #else |
| 123 | #error Need to convert float array to SkScalar array before calling the following function. |
| 124 | #endif |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 125 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 126 | sk_sp<SkShader> shader(SkGradientShader::MakeLinear(pts, &colors[0], |
| 127 | GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(), |
| 128 | static_cast<SkTileMode>(tileMode), sGradientShaderFlags, nullptr)); |
| 129 | ThrowIAE_IfNull(env, shader); |
ztenghui | b244fb5 | 2017-04-04 17:33:40 -0700 | [diff] [blame] | 130 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 131 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
| 132 | if (matrix) { |
| 133 | shader = shader->makeWithLocalMatrix(*matrix); |
| 134 | } |
| 135 | |
| 136 | return reinterpret_cast<jlong>(shader.release()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 139 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Nader Jawad | e7ab841 | 2020-10-16 18:00:04 -0700 | [diff] [blame] | 141 | static jlong RadialGradient_create(JNIEnv* env, |
| 142 | jobject, |
| 143 | jlong matrixPtr, |
| 144 | jfloat startX, |
| 145 | jfloat startY, |
| 146 | jfloat startRadius, |
| 147 | jfloat endX, |
| 148 | jfloat endY, |
| 149 | jfloat endRadius, |
| 150 | jlongArray colorArray, |
| 151 | jfloatArray posArray, |
| 152 | jint tileMode, |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 153 | jlong colorSpaceHandle) { |
Nader Jawad | e7ab841 | 2020-10-16 18:00:04 -0700 | [diff] [blame] | 154 | |
| 155 | SkPoint start; |
| 156 | start.set(startX, startY); |
| 157 | |
| 158 | SkPoint end; |
| 159 | end.set(endX, endY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 161 | std::vector<SkColor4f> colors = convertColorLongs(env, colorArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 163 | AutoJavaFloatArray autoPos(env, posArray, colors.size()); |
| 164 | #ifdef SK_SCALAR_IS_FLOAT |
| 165 | SkScalar* pos = autoPos.ptr(); |
| 166 | #else |
| 167 | #error Need to convert float array to SkScalar array before calling the following function. |
| 168 | #endif |
ztenghui | b244fb5 | 2017-04-04 17:33:40 -0700 | [diff] [blame] | 169 | |
Nader Jawad | e7ab841 | 2020-10-16 18:00:04 -0700 | [diff] [blame] | 170 | auto colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle); |
| 171 | auto skTileMode = static_cast<SkTileMode>(tileMode); |
| 172 | sk_sp<SkShader> shader = SkGradientShader::MakeTwoPointConical(start, startRadius, end, |
| 173 | endRadius, &colors[0], std::move(colorSpace), pos, colors.size(), skTileMode, |
| 174 | sGradientShaderFlags, nullptr); |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 175 | ThrowIAE_IfNull(env, shader); |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 176 | |
Nader Jawad | e7ab841 | 2020-10-16 18:00:04 -0700 | [diff] [blame] | 177 | // Explicitly create a new shader with the specified matrix to match existing behavior. |
| 178 | // Passing in the matrix in the instantiation above can throw exceptions for non-invertible |
| 179 | // matrices. However, makeWithLocalMatrix will still allow for the shader to be created |
| 180 | // and skia handles null-shaders internally (i.e. is ignored) |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 181 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
| 182 | if (matrix) { |
| 183 | shader = shader->makeWithLocalMatrix(*matrix); |
| 184 | } |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 185 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 186 | return reinterpret_cast<jlong>(shader.release()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 191 | static jlong SweepGradient_create(JNIEnv* env, jobject, jlong matrixPtr, jfloat x, jfloat y, |
| 192 | jlongArray colorArray, jfloatArray jpositions, jlong colorSpaceHandle) { |
| 193 | std::vector<SkColor4f> colors = convertColorLongs(env, colorArray); |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 194 | |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 195 | AutoJavaFloatArray autoPos(env, jpositions, colors.size()); |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 196 | #ifdef SK_SCALAR_IS_FLOAT |
| 197 | SkScalar* pos = autoPos.ptr(); |
| 198 | #else |
| 199 | #error Need to convert float array to SkScalar array before calling the following function. |
| 200 | #endif |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 201 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 202 | sk_sp<SkShader> shader = SkGradientShader::MakeSweep(x, y, &colors[0], |
| 203 | GraphicsJNI::getNativeColorSpace(colorSpaceHandle), pos, colors.size(), |
| 204 | sGradientShaderFlags, nullptr); |
| 205 | ThrowIAE_IfNull(env, shader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 207 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
| 208 | if (matrix) { |
| 209 | shader = shader->makeWithLocalMatrix(*matrix); |
| 210 | } |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 211 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 212 | return reinterpret_cast<jlong>(shader.release()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 216 | |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 217 | static jlong ComposeShader_create(JNIEnv* env, jobject o, jlong matrixPtr, |
| 218 | jlong shaderAHandle, jlong shaderBHandle, jint xfermodeHandle) { |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 219 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
| 220 | SkShader* shaderA = reinterpret_cast<SkShader *>(shaderAHandle); |
| 221 | SkShader* shaderB = reinterpret_cast<SkShader *>(shaderBHandle); |
| 222 | SkBlendMode mode = static_cast<SkBlendMode>(xfermodeHandle); |
| 223 | sk_sp<SkShader> baseShader(SkShaders::Blend(mode, |
| 224 | sk_ref_sp(shaderA), sk_ref_sp(shaderB))); |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 225 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 226 | SkShader* shader; |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 227 | |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 228 | if (matrix) { |
| 229 | shader = baseShader->makeWithLocalMatrix(*matrix).release(); |
| 230 | } else { |
| 231 | shader = baseShader.release(); |
| 232 | } |
| 233 | return reinterpret_cast<jlong>(shader); |
Nader Jawad | 322cb9c | 2020-08-17 17:15:54 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 237 | |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 238 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 239 | |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 240 | static jlong RuntimeShader_createShaderBuilder(JNIEnv* env, jobject, jstring sksl) { |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 241 | ScopedUtfChars strSksl(env, sksl); |
Brian Osman | 7556777 | 2021-04-20 19:46:39 +0000 | [diff] [blame] | 242 | auto result = SkRuntimeEffect::MakeForShader(SkString(strSksl.c_str()), |
| 243 | SkRuntimeEffect::Options{}); |
John Stiles | 5f2f3e4 | 2021-02-10 16:58:04 +0000 | [diff] [blame] | 244 | if (result.effect.get() == nullptr) { |
| 245 | doThrowIAE(env, result.errorText.c_str()); |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 246 | return 0; |
John Reck | c784d9a | 2020-08-03 10:45:39 -0700 | [diff] [blame] | 247 | } |
John Stiles | 5f2f3e4 | 2021-02-10 16:58:04 +0000 | [diff] [blame] | 248 | return reinterpret_cast<jlong>(new SkRuntimeShaderBuilder(std::move(result.effect))); |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 251 | static void SkRuntimeShaderBuilder_delete(SkRuntimeShaderBuilder* builder) { |
| 252 | delete builder; |
Nader Jawad | 5bed1f5 | 2020-09-25 00:27:29 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 255 | static jlong RuntimeShader_getNativeFinalizer(JNIEnv*, jobject) { |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 256 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&SkRuntimeShaderBuilder_delete)); |
| 257 | } |
| 258 | |
| 259 | static jlong RuntimeShader_create(JNIEnv* env, jobject, jlong shaderBuilder, jlong matrixPtr, |
| 260 | jboolean isOpaque) { |
| 261 | SkRuntimeShaderBuilder* builder = reinterpret_cast<SkRuntimeShaderBuilder*>(shaderBuilder); |
| 262 | const SkMatrix* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr); |
| 263 | sk_sp<SkShader> shader = builder->makeShader(matrix, isOpaque == JNI_TRUE); |
| 264 | ThrowIAE_IfNull(env, shader); |
| 265 | return reinterpret_cast<jlong>(shader.release()); |
| 266 | } |
| 267 | |
| 268 | static inline int ThrowIAEFmt(JNIEnv* env, const char* fmt, ...) { |
| 269 | va_list args; |
| 270 | va_start(args, fmt); |
Greg Kaiser | 9c1f79f | 2021-01-19 07:35:49 -0800 | [diff] [blame] | 271 | int ret = jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", fmt, args); |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 272 | va_end(args); |
Greg Kaiser | 9c1f79f | 2021-01-19 07:35:49 -0800 | [diff] [blame] | 273 | return ret; |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void RuntimeShader_updateUniforms(JNIEnv* env, jobject, jlong shaderBuilder, |
| 277 | jstring jUniformName, jfloatArray jvalues) { |
| 278 | SkRuntimeShaderBuilder* builder = reinterpret_cast<SkRuntimeShaderBuilder*>(shaderBuilder); |
| 279 | ScopedUtfChars name(env, jUniformName); |
| 280 | AutoJavaFloatArray autoValues(env, jvalues, 0, kRO_JNIAccess); |
| 281 | |
| 282 | SkRuntimeShaderBuilder::BuilderUniform uniform = builder->uniform(name.c_str()); |
| 283 | if (uniform.fVar == nullptr) { |
| 284 | ThrowIAEFmt(env, "unable to find uniform named %s", name.c_str()); |
| 285 | } else if (!uniform.set<float>(autoValues.ptr(), autoValues.length())) { |
| 286 | ThrowIAEFmt(env, "mismatch in byte size for uniform [expected: %zu actual: %zu]", |
| 287 | uniform.fVar->sizeInBytes(), sizeof(float) * autoValues.length()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void RuntimeShader_updateShader(JNIEnv* env, jobject, jlong shaderBuilder, |
| 292 | jstring jUniformName, jlong shaderHandle) { |
| 293 | SkRuntimeShaderBuilder* builder = reinterpret_cast<SkRuntimeShaderBuilder*>(shaderBuilder); |
| 294 | ScopedUtfChars name(env, jUniformName); |
| 295 | SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle); |
| 296 | |
| 297 | SkRuntimeShaderBuilder::BuilderChild child = builder->child(name.c_str()); |
| 298 | if (child.fIndex == -1) { |
| 299 | ThrowIAEFmt(env, "unable to find shader named %s", name.c_str()); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | builder->child(name.c_str()) = sk_ref_sp(shader); |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 307 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 308 | static const JNINativeMethod gColorMethods[] = { |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 309 | { "nativeRGBToHSV", "(III[F)V", (void*)Color_RGBToHSV }, |
| 310 | { "nativeHSVToColor", "(I[F)I", (void*)Color_HSVToColor } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 313 | static const JNINativeMethod gShaderMethods[] = { |
John Reck | 8bde0be | 2017-05-19 10:55:20 -0700 | [diff] [blame] | 314 | { "nativeGetFinalizer", "()J", (void*)Shader_getNativeFinalizer }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 317 | static const JNINativeMethod gBitmapShaderMethods[] = { |
Mike Reed | 3ed3089 | 2020-12-11 12:25:30 -0500 | [diff] [blame] | 318 | { "nativeCreate", "(JJIIZ)J", (void*)BitmapShader_constructor }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 321 | static const JNINativeMethod gLinearGradientMethods[] = { |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 322 | { "nativeCreate", "(JFFFF[J[FIJ)J", (void*)LinearGradient_create }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | }; |
| 324 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 325 | static const JNINativeMethod gRadialGradientMethods[] = { |
Nader Jawad | e7ab841 | 2020-10-16 18:00:04 -0700 | [diff] [blame] | 326 | { "nativeCreate", "(JFFFFFF[J[FIJ)J", (void*)RadialGradient_create }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 329 | static const JNINativeMethod gSweepGradientMethods[] = { |
Leon Scroggins III | b0aecc2 | 2019-01-18 11:09:59 -0500 | [diff] [blame] | 330 | { "nativeCreate", "(JFF[J[FJ)J", (void*)SweepGradient_create }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 333 | static const JNINativeMethod gComposeShaderMethods[] = { |
Chris Craik | b581e67 | 2017-03-07 15:27:36 -0800 | [diff] [blame] | 334 | { "nativeCreate", "(JJJI)J", (void*)ComposeShader_create }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | }; |
| 336 | |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 337 | static const JNINativeMethod gRuntimeShaderMethods[] = { |
Derek Sollenberger | 783e5ae | 2021-01-11 15:44:46 -0500 | [diff] [blame] | 338 | {"nativeGetFinalizer", "()J", (void*)RuntimeShader_getNativeFinalizer}, |
| 339 | {"nativeCreateShader", "(JJZ)J", (void*)RuntimeShader_create}, |
| 340 | {"nativeCreateBuilder", "(Ljava/lang/String;)J", (void*)RuntimeShader_createShaderBuilder}, |
| 341 | {"nativeUpdateUniforms", "(JLjava/lang/String;[F)V", (void*)RuntimeShader_updateUniforms}, |
| 342 | {"nativeUpdateShader", "(JLjava/lang/String;J)V", (void*)RuntimeShader_updateShader}, |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 343 | }; |
| 344 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | int register_android_graphics_Shader(JNIEnv* env) |
| 346 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 347 | android::RegisterMethodsOrDie(env, "android/graphics/Color", gColorMethods, |
| 348 | NELEM(gColorMethods)); |
| 349 | android::RegisterMethodsOrDie(env, "android/graphics/Shader", gShaderMethods, |
| 350 | NELEM(gShaderMethods)); |
| 351 | android::RegisterMethodsOrDie(env, "android/graphics/BitmapShader", gBitmapShaderMethods, |
| 352 | NELEM(gBitmapShaderMethods)); |
| 353 | android::RegisterMethodsOrDie(env, "android/graphics/LinearGradient", gLinearGradientMethods, |
| 354 | NELEM(gLinearGradientMethods)); |
| 355 | android::RegisterMethodsOrDie(env, "android/graphics/RadialGradient", gRadialGradientMethods, |
| 356 | NELEM(gRadialGradientMethods)); |
| 357 | android::RegisterMethodsOrDie(env, "android/graphics/SweepGradient", gSweepGradientMethods, |
| 358 | NELEM(gSweepGradientMethods)); |
| 359 | android::RegisterMethodsOrDie(env, "android/graphics/ComposeShader", gComposeShaderMethods, |
| 360 | NELEM(gComposeShaderMethods)); |
Stan Iliev | 6867fc8 | 2019-11-22 18:00:01 -0500 | [diff] [blame] | 361 | android::RegisterMethodsOrDie(env, "android/graphics/RuntimeShader", gRuntimeShaderMethods, |
| 362 | NELEM(gRuntimeShaderMethods)); |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 363 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 364 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |