blob: 619a3ed552d81a5a871122e7338dd28d7bb94fae [file] [log] [blame]
Angel Aguayo79e5f582022-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 Aguayo79e5f582022-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()));
81
82 if (meshSpecResult.specification.get() == nullptr) {
83 jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
84 }
85
86 return reinterpret_cast<jlong>(meshSpecResult.specification.release());
Angel Aguayo79e5f582022-10-27 00:17:00 +000087}
88
89static jlong MakeWithCS(JNIEnv* env, jobject thiz, jobjectArray attributeArray, jint vertexStride,
90 jobjectArray varyingArray, jstring vertexShader, jstring fragmentShader,
91 jlong colorSpace) {
92 auto attributes = extractAttributes(env, attributeArray);
93 auto varyings = extractVaryings(env, varyingArray);
94 auto skVertexShader = ScopedUtfChars(env, vertexShader);
95 auto skFragmentShader = ScopedUtfChars(env, fragmentShader);
Angel Aguayo90c46ee2022-11-08 23:42:09 +000096 auto meshSpecResult = SkMeshSpecification::Make(
97 attributes, vertexStride, varyings, SkString(skVertexShader.c_str()),
98 SkString(skFragmentShader.c_str()), GraphicsJNI::getNativeColorSpace(colorSpace));
Angel Aguayo79e5f582022-10-27 00:17:00 +000099
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000100 if (meshSpecResult.specification.get() == nullptr) {
101 jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
102 }
103
104 return reinterpret_cast<jlong>(meshSpecResult.specification.release());
Angel Aguayo79e5f582022-10-27 00:17:00 +0000105}
106
107static jlong MakeWithAlpha(JNIEnv* env, jobject thiz, jobjectArray attributeArray,
108 jint vertexStride, jobjectArray varyingArray, jstring vertexShader,
109 jstring fragmentShader, jlong colorSpace, jint alphaType) {
110 auto attributes = extractAttributes(env, attributeArray);
111 auto varyings = extractVaryings(env, varyingArray);
112 auto skVertexShader = ScopedUtfChars(env, vertexShader);
113 auto skFragmentShader = ScopedUtfChars(env, fragmentShader);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000114 auto meshSpecResult = SkMeshSpecification::Make(
115 attributes, vertexStride, varyings, SkString(skVertexShader.c_str()),
116 SkString(skFragmentShader.c_str()), GraphicsJNI::getNativeColorSpace(colorSpace),
117 SkAlphaType(alphaType));
118
119 if (meshSpecResult.specification.get() == nullptr) {
120 jniThrowException(env, "java/lang/IllegalArgumentException", meshSpecResult.error.c_str());
121 }
122
123 return reinterpret_cast<jlong>(meshSpecResult.specification.release());
Angel Aguayo79e5f582022-10-27 00:17:00 +0000124}
125
126static void MeshSpecification_safeUnref(SkMeshSpecification* meshSpec) {
127 SkSafeUnref(meshSpec);
128}
129
130static jlong getMeshSpecificationFinalizer() {
131 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&MeshSpecification_safeUnref));
132}
133
134static const JNINativeMethod gMeshSpecificationMethods[] = {
135 {"nativeGetFinalizer", "()J", (void*)getMeshSpecificationFinalizer},
136 {"nativeMake",
137 "([Landroid/graphics/MeshSpecification$Attribute;I[Landroid/graphics/"
138 "MeshSpecification$Varying;"
139 "Ljava/lang/String;Ljava/lang/String;)J",
140 (void*)Make},
141 {"nativeMakeWithCS",
142 "([Landroid/graphics/MeshSpecification$Attribute;I"
143 "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;J)J",
144 (void*)MakeWithCS},
145 {"nativeMakeWithAlpha",
146 "([Landroid/graphics/MeshSpecification$Attribute;I"
147 "[Landroid/graphics/MeshSpecification$Varying;Ljava/lang/String;Ljava/lang/String;JI)J",
148 (void*)MakeWithAlpha}};
149
150int register_android_graphics_MeshSpecification(JNIEnv* env) {
151 android::RegisterMethodsOrDie(env, "android/graphics/MeshSpecification",
152 gMeshSpecificationMethods, NELEM(gMeshSpecificationMethods));
153
154 gAttributeInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Attribute");
155 gAttributeInfo.type = env->GetFieldID(gAttributeInfo.clazz, "mType", "I");
156 gAttributeInfo.offset = env->GetFieldID(gAttributeInfo.clazz, "mOffset", "I");
157 gAttributeInfo.name = env->GetFieldID(gAttributeInfo.clazz, "mName", "Ljava/lang/String;");
158
159 gVaryingInfo.clazz = env->FindClass("android/graphics/MeshSpecification$Varying");
160 gVaryingInfo.type = env->GetFieldID(gVaryingInfo.clazz, "mType", "I");
161 gVaryingInfo.name = env->GetFieldID(gVaryingInfo.clazz, "mName", "Ljava/lang/String;");
162 return 0;
163}
164
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000165} // namespace android