Jorge Betancourt | 2c9a688 | 2024-09-09 20:34:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "RuntimeEffectUtils.h" |
| 18 | |
| 19 | #include "include/effects/SkRuntimeEffect.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | static inline int ThrowIAEFmt(JNIEnv* env, const char* fmt, ...) { |
| 25 | va_list args; |
| 26 | va_start(args, fmt); |
| 27 | int ret = jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", fmt, args); |
| 28 | va_end(args); |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | bool isIntUniformType(const SkRuntimeEffect::Uniform::Type& type) { |
| 33 | switch (type) { |
| 34 | case SkRuntimeEffect::Uniform::Type::kFloat: |
| 35 | case SkRuntimeEffect::Uniform::Type::kFloat2: |
| 36 | case SkRuntimeEffect::Uniform::Type::kFloat3: |
| 37 | case SkRuntimeEffect::Uniform::Type::kFloat4: |
| 38 | case SkRuntimeEffect::Uniform::Type::kFloat2x2: |
| 39 | case SkRuntimeEffect::Uniform::Type::kFloat3x3: |
| 40 | case SkRuntimeEffect::Uniform::Type::kFloat4x4: |
| 41 | return false; |
| 42 | case SkRuntimeEffect::Uniform::Type::kInt: |
| 43 | case SkRuntimeEffect::Uniform::Type::kInt2: |
| 44 | case SkRuntimeEffect::Uniform::Type::kInt3: |
| 45 | case SkRuntimeEffect::Uniform::Type::kInt4: |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void UpdateFloatUniforms(JNIEnv* env, SkRuntimeEffectBuilder* builder, const char* uniformName, |
| 51 | const float values[], int count, bool isColor) { |
| 52 | SkRuntimeEffectBuilder::BuilderUniform uniform = builder->uniform(uniformName); |
| 53 | if (uniform.fVar == nullptr) { |
| 54 | ThrowIAEFmt(env, "unable to find uniform named %s", uniformName); |
| 55 | } else if (isColor != ((uniform.fVar->flags & SkRuntimeEffect::Uniform::kColor_Flag) != 0)) { |
| 56 | if (isColor) { |
| 57 | jniThrowExceptionFmt( |
| 58 | env, "java/lang/IllegalArgumentException", |
| 59 | "attempting to set a color uniform using the non-color specific APIs: %s %x", |
| 60 | uniformName, uniform.fVar->flags); |
| 61 | } else { |
| 62 | ThrowIAEFmt(env, |
| 63 | "attempting to set a non-color uniform using the setColorUniform APIs: %s", |
| 64 | uniformName); |
| 65 | } |
| 66 | } else if (isIntUniformType(uniform.fVar->type)) { |
| 67 | ThrowIAEFmt(env, "attempting to set a int uniform using the setUniform APIs: %s", |
| 68 | uniformName); |
| 69 | } else if (!uniform.set<float>(values, count)) { |
| 70 | ThrowIAEFmt(env, "mismatch in byte size for uniform [expected: %zu actual: %zu]", |
| 71 | uniform.fVar->sizeInBytes(), sizeof(float) * count); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void UpdateIntUniforms(JNIEnv* env, SkRuntimeEffectBuilder* builder, const char* uniformName, |
| 76 | const int values[], int count) { |
| 77 | SkRuntimeEffectBuilder::BuilderUniform uniform = builder->uniform(uniformName); |
| 78 | if (uniform.fVar == nullptr) { |
| 79 | ThrowIAEFmt(env, "unable to find uniform named %s", uniformName); |
| 80 | } else if (!isIntUniformType(uniform.fVar->type)) { |
| 81 | ThrowIAEFmt(env, "attempting to set a non-int uniform using the setIntUniform APIs: %s", |
| 82 | uniformName); |
| 83 | } else if (!uniform.set<int>(values, count)) { |
| 84 | ThrowIAEFmt(env, "mismatch in byte size for uniform [expected: %zu actual: %zu]", |
| 85 | uniform.fVar->sizeInBytes(), sizeof(float) * count); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void UpdateChild(JNIEnv* env, SkRuntimeEffectBuilder* builder, const char* childName, |
| 90 | SkFlattenable* childEffect) { |
| 91 | SkRuntimeShaderBuilder::BuilderChild builderChild = builder->child(childName); |
| 92 | if (builderChild.fChild == nullptr) { |
| 93 | ThrowIAEFmt(env, "unable to find shader named %s", childName); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | builderChild = sk_ref_sp(childEffect); |
| 98 | } |
| 99 | |
| 100 | } // namespace uirenderer |
| 101 | } // namespace android |