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 
extractAttributes(JNIEnv * env,jobjectArray attributes)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     return attVector;
54 }
55 
extractVaryings(JNIEnv * env,jobjectArray varyings)56 std::vector<Varying> extractVaryings(JNIEnv* env, jobjectArray varyings) {
57     int size = env->GetArrayLength(varyings);
58     std::vector<Varying> varyVector;
59     varyVector.reserve(size);
60     for (int i = 0; i < size; i++) {
61         jobject varying = env->GetObjectArrayElement(varyings, i);
62         auto name = (jstring)env->GetObjectField(varying, gVaryingInfo.name);
63         auto varyName = ScopedUtfChars(env, name);
64         Varying temp{Varying::Type(env->GetIntField(varying, gVaryingInfo.type)),
65                      SkString(varyName.c_str())};
66         varyVector.push_back(std::move(temp));
67     }
68 
69     return varyVector;
70 }
71 
Make(JNIEnv * env,jobject thiz,jobjectArray attributeArray,jint vertexStride,jobjectArray varyingArray,jstring vertexShader,jstring fragmentShader)72 static jlong Make(JNIEnv* env, jobject thiz, jobjectArray attributeArray, jint vertexStride,
73                   jobjectArray varyingArray, jstring vertexShader, jstring fragmentShader) {
74     auto attributes = extractAttributes(env, attributeArray);
75     auto varyings = extractVaryings(env, varyingArray);
76     auto skVertexShader = ScopedUtfChars(env, vertexShader);
77     auto skFragmentShader = ScopedUtfChars(env, fragmentShader);
78     auto meshSpecResult = SkMeshSpecification::Make(attributes, vertexStride, varyings,
79                                                     SkString(skVertexShader.c_str()),
80                                                     SkString(skFragmentShader.c_str()));
81     if (meshSpecResult.specification.get() == nullptr) {
82         jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
83     }
84 
85     return reinterpret_cast<jlong>(meshSpecResult.specification.release());
86 }
87 
MakeWithCS(JNIEnv * env,jobject thiz,jobjectArray attributeArray,jint vertexStride,jobjectArray varyingArray,jstring vertexShader,jstring fragmentShader,jlong colorSpace)88 static jlong MakeWithCS(JNIEnv* env, jobject thiz, jobjectArray attributeArray, jint vertexStride,
89                         jobjectArray varyingArray, jstring vertexShader, jstring fragmentShader,
90                         jlong colorSpace) {
91     auto attributes = extractAttributes(env, attributeArray);
92     auto varyings = extractVaryings(env, varyingArray);
93     auto skVertexShader = ScopedUtfChars(env, vertexShader);
94     auto skFragmentShader = ScopedUtfChars(env, fragmentShader);
95     auto meshSpecResult = SkMeshSpecification::Make(
96             attributes, vertexStride, varyings, SkString(skVertexShader.c_str()),
97             SkString(skFragmentShader.c_str()), GraphicsJNI::getNativeColorSpace(colorSpace));
98 
99     if (meshSpecResult.specification.get() == nullptr) {
100         jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
101     }
102 
103     return reinterpret_cast<jlong>(meshSpecResult.specification.release());
104 }
105 
MakeWithAlpha(JNIEnv * env,jobject thiz,jobjectArray attributeArray,jint vertexStride,jobjectArray varyingArray,jstring vertexShader,jstring fragmentShader,jlong colorSpace,jint alphaType)106 static jlong MakeWithAlpha(JNIEnv* env, jobject thiz, jobjectArray attributeArray,
107                            jint vertexStride, jobjectArray varyingArray, jstring vertexShader,
108                            jstring fragmentShader, jlong colorSpace, jint alphaType) {
109     auto attributes = extractAttributes(env, attributeArray);
110     auto varyings = extractVaryings(env, varyingArray);
111     auto skVertexShader = ScopedUtfChars(env, vertexShader);
112     auto skFragmentShader = ScopedUtfChars(env, fragmentShader);
113     auto meshSpecResult = SkMeshSpecification::Make(
114             attributes, vertexStride, varyings, SkString(skVertexShader.c_str()),
115             SkString(skFragmentShader.c_str()), GraphicsJNI::getNativeColorSpace(colorSpace),
116             SkAlphaType(alphaType));
117 
118     if (meshSpecResult.specification.get() == nullptr) {
119         jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
120     }
121 
122     return reinterpret_cast<jlong>(meshSpecResult.specification.release());
123 }
124 
MeshSpecification_safeUnref(SkMeshSpecification * meshSpec)125 static void MeshSpecification_safeUnref(SkMeshSpecification* meshSpec) {
126     SkSafeUnref(meshSpec);
127 }
128 
getMeshSpecificationFinalizer()129 static jlong getMeshSpecificationFinalizer() {
130     return static_cast<jlong>(reinterpret_cast<uintptr_t>(&MeshSpecification_safeUnref));
131 }
132 
133 static const JNINativeMethod gMeshSpecificationMethods[] = {
134         {"nativeGetFinalizer", "()J", (void*)getMeshSpecificationFinalizer},
135         {"nativeMake",
136          "([Landroid/graphics/MeshSpecification$Attribute;I[Landroid/graphics/"
137          "MeshSpecification$Varying;"
138          "Ljava/lang/String;Ljava/lang/String;)J",
139          (void*)Make},
140         {"nativeMakeWithCS",
141          "([Landroid/graphics/MeshSpecification$Attribute;I"
142          "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;J)J",
143          (void*)MakeWithCS},
144         {"nativeMakeWithAlpha",
145          "([Landroid/graphics/MeshSpecification$Attribute;I"
146          "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;JI)J",
147          (void*)MakeWithAlpha}};
148 
register_android_graphics_MeshSpecification(JNIEnv * env)149 int register_android_graphics_MeshSpecification(JNIEnv* env) {
150     android::RegisterMethodsOrDie(env, "android/graphics/MeshSpecification",
151                                   gMeshSpecificationMethods, NELEM(gMeshSpecificationMethods));
152 
153     gAttributeInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Attribute");
154     gAttributeInfo.type = env->GetFieldID(gAttributeInfo.clazz, "mType", "I");
155     gAttributeInfo.offset = env->GetFieldID(gAttributeInfo.clazz, "mOffset", "I");
156     gAttributeInfo.name = env->GetFieldID(gAttributeInfo.clazz, "mName", "Ljava/lang/String;");
157 
158     gVaryingInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Varying");
159     gVaryingInfo.type = env->GetFieldID(gVaryingInfo.clazz, "mType", "I");
160     gVaryingInfo.name = env->GetFieldID(gVaryingInfo.clazz, "mName", "Ljava/lang/String;");
161     return 0;
162 }
163 
164 }  // namespace android
165