blob: b943496ae9f76a032d7d821dc5440692e107b1aa [file] [log] [blame]
Angel Aguayo79e5f58f2022-10-27 00:17:00 +00001/*
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
22namespace android {
23
24using Attribute = SkMeshSpecification::Attribute;
25using Varying = SkMeshSpecification::Varying;
26
27static struct {
28 jclass clazz{};
29 jfieldID type{};
30 jfieldID offset{};
31 jfieldID name{};
32} gAttributeInfo;
33
34static struct {
35 jclass clazz{};
36 jfieldID type{};
37 jfieldID name{};
38} gVaryingInfo;
39
40std::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 }
Angel Aguayo79e5f58f2022-10-27 00:17:00 +000053 return attVector;
54}
55
56std::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
72static 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);
Angel Aguayo90c46ee2022-11-08 23:42:09 +000078 auto meshSpecResult = SkMeshSpecification::Make(attributes, vertexStride, varyings,
79 SkString(skVertexShader.c_str()),
80 SkString(skFragmentShader.c_str()));
Angel Aguayo90c46ee2022-11-08 23:42:09 +000081 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());
Angel Aguayo79e5f58f2022-10-27 00:17:00 +000086}
87
88static 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);
Angel Aguayo90c46ee2022-11-08 23:42:09 +000095 auto meshSpecResult = SkMeshSpecification::Make(
96 attributes, vertexStride, varyings, SkString(skVertexShader.c_str()),
97 SkString(skFragmentShader.c_str()), GraphicsJNI::getNativeColorSpace(colorSpace));
Angel Aguayo79e5f58f2022-10-27 00:17:00 +000098
Angel Aguayo90c46ee2022-11-08 23:42:09 +000099 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());
Angel Aguayo79e5f58f2022-10-27 00:17:00 +0000104}
105
106static 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);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000113 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());
Angel Aguayo79e5f58f2022-10-27 00:17:00 +0000123}
124
125static void MeshSpecification_safeUnref(SkMeshSpecification* meshSpec) {
126 SkSafeUnref(meshSpec);
127}
128
Jerome Gaillardcd7eda92024-07-11 16:01:22 +0100129static jlong getMeshSpecificationFinalizer(CRITICAL_JNI_PARAMS) {
Angel Aguayo79e5f58f2022-10-27 00:17:00 +0000130 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&MeshSpecification_safeUnref));
131}
132
133static 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
149int 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
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000164} // namespace android