|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <jni.h> |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <utility> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "absl/base/nullability.h" |
| 22 | +#include "absl/status/status.h" |
| 23 | +#include "absl/status/statusor.h" |
| 24 | +#include "ink/brush/brush_family.h" |
| 25 | +#include "ink/brush/internal/jni/brush_native_helper.h" |
| 26 | +#include "ink/brush/version.h" |
| 27 | +#include "ink/jni/internal/jni_defines.h" |
| 28 | +#include "ink/jni/internal/jni_proto_util.h" |
| 29 | +#include "ink/jni/internal/status_jni_helper.h" |
| 30 | +#include "ink/storage/brush.h" |
| 31 | +#include "ink/storage/internal/jni/brush_family_serialization_jni_helper.h" |
| 32 | +#include "ink/storage/proto/brush.pb.h" |
| 33 | +#include "ink/storage/proto/brush_family.pb.h" |
| 34 | + |
| 35 | +using ::ink::BrushFamily; |
| 36 | +using ::ink::ClientTextureIdProviderAndBitmapReceiver; |
| 37 | +using ::ink::DecodeBrushFamily; |
| 38 | +using ::ink::DecodeMultipleBrushFamilies; |
| 39 | +using ::ink::EncodeBrushFamily; |
| 40 | +using ::ink::EncodeMultipleBrushFamilies; |
| 41 | +using ::ink::TextureBitmapProvider; |
| 42 | +using ::ink::Version; |
| 43 | +using ::ink::jni::CastToMutableMultipleBrushFamilies; |
| 44 | +using ::ink::jni::CreateDecodeTextureJniWrapper; |
| 45 | +using ::ink::jni::CreateTextureBitmapProvider; |
| 46 | +using ::ink::jni::DeleteMultipleBrushFamilies; |
| 47 | +using ::ink::jni::NewNativeMultipleBrushFamilies; |
| 48 | +using ::ink::jni::ParseProtoFromByteArray; |
| 49 | +using ::ink::jni::SerializeProto; |
| 50 | +using ::ink::jni::ThrowExceptionFromStatus; |
| 51 | +using ::ink::native::CastToBrushFamily; |
| 52 | +using ::ink::native::IntToVersion; |
| 53 | +using ::ink::native::NewNativeBrushFamily; |
| 54 | + |
| 55 | +extern "C" { |
| 56 | + |
| 57 | +JNI_METHOD(storage, BrushFamilySerializationNative, jbyteArray, encode) |
| 58 | +(JNIEnv* env, jobject object, jlong brush_family_native_pointer, |
| 59 | + jobjectArray texture_map_keys, jobjectArray texture_map_values) { |
| 60 | + TextureBitmapProvider texture_bitmap_provider = |
| 61 | + CreateTextureBitmapProvider(env, texture_map_keys, texture_map_values); |
| 62 | + |
| 63 | + ink::proto::BrushFamily brush_family_proto; |
| 64 | + EncodeBrushFamily(CastToBrushFamily(brush_family_native_pointer), |
| 65 | + brush_family_proto, texture_bitmap_provider); |
| 66 | + return SerializeProto(env, brush_family_proto); |
| 67 | +} |
| 68 | + |
| 69 | +JNI_METHOD(storage, BrushFamilySerializationNative, jbyteArray, encodeMultiple) |
| 70 | +(JNIEnv* env, jobject object, jlongArray brush_family_native_pointers, |
| 71 | + jobjectArray texture_map_keys, jobjectArray texture_map_values) { |
| 72 | + TextureBitmapProvider texture_bitmap_provider = |
| 73 | + CreateTextureBitmapProvider(env, texture_map_keys, texture_map_values); |
| 74 | + |
| 75 | + jsize pointers_length = env->GetArrayLength(brush_family_native_pointers); |
| 76 | + jlong* pointers = |
| 77 | + env->GetLongArrayElements(brush_family_native_pointers, nullptr); |
| 78 | + std::vector<BrushFamily> families; |
| 79 | + families.reserve(pointers_length); |
| 80 | + for (jsize i = 0; i < pointers_length; ++i) { |
| 81 | + families.push_back(CastToBrushFamily(pointers[i])); |
| 82 | + } |
| 83 | + env->ReleaseLongArrayElements(brush_family_native_pointers, pointers, |
| 84 | + JNI_ABORT); |
| 85 | + |
| 86 | + ink::proto::BrushFamily brush_family_proto; |
| 87 | + EncodeMultipleBrushFamilies(families, brush_family_proto, |
| 88 | + texture_bitmap_provider); |
| 89 | + return SerializeProto(env, brush_family_proto); |
| 90 | +} |
| 91 | + |
| 92 | +JNI_METHOD(storage, BrushFamilySerializationNative, jlong, createFromProto) |
| 93 | +(JNIEnv* env, jobject object, jbyteArray brush_family_byte_array, jint length, |
| 94 | + absl_nullable jobject callback, jint max_version_value) { |
| 95 | + ink::proto::BrushFamily brush_family_proto; |
| 96 | + constexpr int kOffset = 0; |
| 97 | + if (absl::Status status = ParseProtoFromByteArray( |
| 98 | + env, brush_family_byte_array, kOffset, length, brush_family_proto); |
| 99 | + !status.ok()) { |
| 100 | + ThrowExceptionFromStatus(env, status); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | + ClientTextureIdProviderAndBitmapReceiver decode_texture_jni_wrapper = |
| 105 | + CreateDecodeTextureJniWrapper(env, callback); |
| 106 | + |
| 107 | + absl::StatusOr<Version> max_version = IntToVersion(max_version_value); |
| 108 | + if (!max_version.ok()) { |
| 109 | + ThrowExceptionFromStatus(env, max_version.status()); |
| 110 | + return 0; |
| 111 | + } |
| 112 | + absl::StatusOr<BrushFamily> brush_family = DecodeBrushFamily( |
| 113 | + brush_family_proto, decode_texture_jni_wrapper, max_version.value()); |
| 114 | + if (!brush_family.ok()) { |
| 115 | + // If the callback raised an exception we want to raise that as-is |
| 116 | + // instead of replacing it with the status. |
| 117 | + if (!env->ExceptionCheck()) { |
| 118 | + ThrowExceptionFromStatus(env, brush_family.status()); |
| 119 | + } |
| 120 | + return 0; |
| 121 | + } |
| 122 | + return NewNativeBrushFamily(*std::move(brush_family)); |
| 123 | +} |
| 124 | + |
| 125 | +JNI_METHOD(storage, MultipleBrushFamiliesNative, jlong, createFromProto) |
| 126 | +(JNIEnv* env, jobject object, jbyteArray brush_family_byte_array, jint length, |
| 127 | + absl_nullable jobject callback, jint max_version_value) { |
| 128 | + constexpr int kOffset = 0; |
| 129 | + ink::proto::BrushFamily brush_family_proto; |
| 130 | + if (absl::Status status = ParseProtoFromByteArray( |
| 131 | + env, brush_family_byte_array, kOffset, length, brush_family_proto); |
| 132 | + !status.ok()) { |
| 133 | + ThrowExceptionFromStatus(env, status); |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + ClientTextureIdProviderAndBitmapReceiver decode_texture_jni_wrapper = |
| 138 | + CreateDecodeTextureJniWrapper(env, callback); |
| 139 | + |
| 140 | + absl::StatusOr<Version> max_version = IntToVersion(max_version_value); |
| 141 | + if (!max_version.ok()) { |
| 142 | + ThrowExceptionFromStatus(env, max_version.status()); |
| 143 | + return 0; |
| 144 | + } |
| 145 | + absl::StatusOr<std::vector<BrushFamily>> brush_families = |
| 146 | + DecodeMultipleBrushFamilies( |
| 147 | + brush_family_proto, decode_texture_jni_wrapper, max_version.value()); |
| 148 | + if (!brush_families.ok()) { |
| 149 | + if (!env->ExceptionCheck()) { |
| 150 | + ThrowExceptionFromStatus(env, brush_families.status()); |
| 151 | + } |
| 152 | + return 0; |
| 153 | + } |
| 154 | + |
| 155 | + std::vector<std::unique_ptr<BrushFamily>> result; |
| 156 | + result.reserve(brush_families->size()); |
| 157 | + for (BrushFamily& brush_family : *brush_families) { |
| 158 | + result.push_back(std::make_unique<BrushFamily>(std::move(brush_family))); |
| 159 | + } |
| 160 | + return NewNativeMultipleBrushFamilies(std::move(result)); |
| 161 | +} |
| 162 | + |
| 163 | +JNI_METHOD(storage, MultipleBrushFamiliesNative, jint, getBrushFamilyCount) |
| 164 | +(JNIEnv* env, jobject object, jlong native_pointer) { |
| 165 | + return CastToMutableMultipleBrushFamilies(native_pointer).size(); |
| 166 | +} |
| 167 | + |
| 168 | +JNI_METHOD(storage, MultipleBrushFamiliesNative, jlong, releaseBrushFamily) |
| 169 | +(JNIEnv* env, jobject object, jlong native_pointer, jint index) { |
| 170 | + // This is already a heap-allocated BrushFamily, handed off so that the new |
| 171 | + // Kotlin BrushFamily will start managing cleanup. |
| 172 | + return reinterpret_cast<jlong>( |
| 173 | + CastToMutableMultipleBrushFamilies(native_pointer)[index].release()); |
| 174 | +} |
| 175 | + |
| 176 | +JNI_METHOD(storage, MultipleBrushFamiliesNative, void, free) |
| 177 | +(JNIEnv* env, jobject object, jlong native_pointer) { |
| 178 | + DeleteMultipleBrushFamilies(native_pointer); |
| 179 | +} |
| 180 | + |
| 181 | +} // extern "C" |
0 commit comments