Angel Aguayo | 79e5f58 | 2022-10-27 00:17:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 <SkMesh.h> |
| 18 | |
| 19 | #include "GraphicsJNI.h" |
| 20 | #include "graphics_jni_helpers.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | using Attribute = SkMeshSpecification::Attribute; |
| 25 | using Varying = SkMeshSpecification::Varying; |
| 26 | |
| 27 | static struct { |
| 28 | jclass clazz{}; |
| 29 | jfieldID type{}; |
| 30 | jfieldID offset{}; |
| 31 | jfieldID name{}; |
| 32 | } gAttributeInfo; |
| 33 | |
| 34 | static struct { |
| 35 | jclass clazz{}; |
| 36 | jfieldID type{}; |
| 37 | jfieldID name{}; |
| 38 | } gVaryingInfo; |
| 39 | |
| 40 | std::vector<Attribute> extractAttributes(JNIEnv* env, jobjectArray attributes) { |
| 41 | int size = env->GetArrayLength(attributes); |
| 42 | std::vector<Attribute> attVector; |
| 43 | attVector.reserve(size); |
| 44 | for (int i = 0; i < size; i++) { |
| 45 | jobject attribute = env->GetObjectArrayElement(attributes, i); |
| 46 | auto name = (jstring)env->GetObjectField(attribute, gAttributeInfo.name); |
| 47 | auto attName = ScopedUtfChars(env, name); |
| 48 | Attribute temp{Attribute::Type(env->GetIntField(attribute, gAttributeInfo.type)), |
| 49 | static_cast<size_t>(env->GetIntField(attribute, gAttributeInfo.offset)), |
| 50 | SkString(attName.c_str())}; |
| 51 | attVector.push_back(std::move(temp)); |
| 52 | } |
| 53 | |
| 54 | return attVector; |
| 55 | } |
| 56 | |
| 57 | std::vector<Varying> extractVaryings(JNIEnv* env, jobjectArray varyings) { |
| 58 | int size = env->GetArrayLength(varyings); |
| 59 | std::vector<Varying> varyVector; |
| 60 | varyVector.reserve(size); |
| 61 | for (int i = 0; i < size; i++) { |
| 62 | jobject varying = env->GetObjectArrayElement(varyings, i); |
| 63 | auto name = (jstring)env->GetObjectField(varying, gVaryingInfo.name); |
| 64 | auto varyName = ScopedUtfChars(env, name); |
| 65 | Varying temp{Varying::Type(env->GetIntField(varying, gVaryingInfo.type)), |
| 66 | SkString(varyName.c_str())}; |
| 67 | varyVector.push_back(std::move(temp)); |
| 68 | } |
| 69 | |
| 70 | return varyVector; |
| 71 | } |
| 72 | |
| 73 | static jlong Make(JNIEnv* env, jobject thiz, jobjectArray attributeArray, jint vertexStride, |
| 74 | jobjectArray varyingArray, jstring vertexShader, jstring fragmentShader) { |
| 75 | auto attributes = extractAttributes(env, attributeArray); |
| 76 | auto varyings = extractVaryings(env, varyingArray); |
| 77 | auto skVertexShader = ScopedUtfChars(env, vertexShader); |
| 78 | auto skFragmentShader = ScopedUtfChars(env, fragmentShader); |
| 79 | auto meshSpec = SkMeshSpecification::Make(attributes, vertexStride, varyings, |
| 80 | SkString(skVertexShader.c_str()), |
| 81 | SkString(skFragmentShader.c_str())) |
| 82 | .specification; |
| 83 | return reinterpret_cast<jlong>(meshSpec.release()); |
| 84 | } |
| 85 | |
| 86 | static jlong MakeWithCS(JNIEnv* env, jobject thiz, jobjectArray attributeArray, jint vertexStride, |
| 87 | jobjectArray varyingArray, jstring vertexShader, jstring fragmentShader, |
| 88 | jlong colorSpace) { |
| 89 | auto attributes = extractAttributes(env, attributeArray); |
| 90 | auto varyings = extractVaryings(env, varyingArray); |
| 91 | auto skVertexShader = ScopedUtfChars(env, vertexShader); |
| 92 | auto skFragmentShader = ScopedUtfChars(env, fragmentShader); |
| 93 | auto meshSpec = SkMeshSpecification::Make(attributes, vertexStride, varyings, |
| 94 | SkString(skVertexShader.c_str()), |
| 95 | SkString(skFragmentShader.c_str()), |
| 96 | GraphicsJNI::getNativeColorSpace(colorSpace)) |
| 97 | .specification; |
| 98 | |
| 99 | return reinterpret_cast<jlong>(meshSpec.release()); |
| 100 | } |
| 101 | |
| 102 | static jlong MakeWithAlpha(JNIEnv* env, jobject thiz, jobjectArray attributeArray, |
| 103 | jint vertexStride, jobjectArray varyingArray, jstring vertexShader, |
| 104 | jstring fragmentShader, jlong colorSpace, jint alphaType) { |
| 105 | auto attributes = extractAttributes(env, attributeArray); |
| 106 | auto varyings = extractVaryings(env, varyingArray); |
| 107 | auto skVertexShader = ScopedUtfChars(env, vertexShader); |
| 108 | auto skFragmentShader = ScopedUtfChars(env, fragmentShader); |
| 109 | auto meshSpec = SkMeshSpecification::Make( |
| 110 | attributes, vertexStride, varyings, SkString(skVertexShader.c_str()), |
| 111 | SkString(skFragmentShader.c_str()), |
| 112 | GraphicsJNI::getNativeColorSpace(colorSpace), SkAlphaType(alphaType)) |
| 113 | .specification; |
| 114 | return reinterpret_cast<jlong>(meshSpec.release()); |
| 115 | } |
| 116 | |
| 117 | static void MeshSpecification_safeUnref(SkMeshSpecification* meshSpec) { |
| 118 | SkSafeUnref(meshSpec); |
| 119 | } |
| 120 | |
| 121 | static jlong getMeshSpecificationFinalizer() { |
| 122 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&MeshSpecification_safeUnref)); |
| 123 | } |
| 124 | |
| 125 | static const JNINativeMethod gMeshSpecificationMethods[] = { |
| 126 | {"nativeGetFinalizer", "()J", (void*)getMeshSpecificationFinalizer}, |
| 127 | {"nativeMake", |
| 128 | "([Landroid/graphics/MeshSpecification$Attribute;I[Landroid/graphics/" |
| 129 | "MeshSpecification$Varying;" |
| 130 | "Ljava/lang/String;Ljava/lang/String;)J", |
| 131 | (void*)Make}, |
| 132 | {"nativeMakeWithCS", |
| 133 | "([Landroid/graphics/MeshSpecification$Attribute;I" |
| 134 | "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;J)J", |
| 135 | (void*)MakeWithCS}, |
| 136 | {"nativeMakeWithAlpha", |
| 137 | "([Landroid/graphics/MeshSpecification$Attribute;I" |
| 138 | "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;JI)J", |
| 139 | (void*)MakeWithAlpha}}; |
| 140 | |
| 141 | int register_android_graphics_MeshSpecification(JNIEnv* env) { |
| 142 | android::RegisterMethodsOrDie(env, "android/graphics/MeshSpecification", |
| 143 | gMeshSpecificationMethods, NELEM(gMeshSpecificationMethods)); |
| 144 | |
| 145 | gAttributeInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Attribute"); |
| 146 | gAttributeInfo.type = env->GetFieldID(gAttributeInfo.clazz, "mType", "I"); |
| 147 | gAttributeInfo.offset = env->GetFieldID(gAttributeInfo.clazz, "mOffset", "I"); |
| 148 | gAttributeInfo.name = env->GetFieldID(gAttributeInfo.clazz, "mName", "Ljava/lang/String;"); |
| 149 | |
| 150 | gVaryingInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Varying"); |
| 151 | gVaryingInfo.type = env->GetFieldID(gVaryingInfo.clazz, "mType", "I"); |
| 152 | gVaryingInfo.name = env->GetFieldID(gVaryingInfo.clazz, "mName", "Ljava/lang/String;"); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | } // namespace android |