Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 17 | #include "GraphicsJNI.h" |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 18 | #include "utils/misc.h" |
| 19 | #include "utils/Log.h" |
| 20 | #include <nativehelper/ScopedStringChars.h> |
| 21 | #include <nativehelper/ScopedPrimitiveArray.h> |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 22 | #include <cstdint> |
| 23 | #include <vector> |
| 24 | #include <list> |
| 25 | #include <algorithm> |
| 26 | |
| 27 | #include "SkPaint.h" |
| 28 | #include "SkTypeface.h" |
| 29 | #include <hwui/MinikinSkia.h> |
| 30 | #include <hwui/MinikinUtils.h> |
| 31 | #include <hwui/Paint.h> |
| 32 | #include <minikin/FontCollection.h> |
| 33 | #include <minikin/AndroidLineBreakerHelper.h> |
| 34 | #include <minikin/MinikinFont.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | static inline minikin::MeasuredTextBuilder* toBuilder(jlong ptr) { |
| 39 | return reinterpret_cast<minikin::MeasuredTextBuilder*>(ptr); |
| 40 | } |
| 41 | |
| 42 | static inline Paint* toPaint(jlong ptr) { |
| 43 | return reinterpret_cast<Paint*>(ptr); |
| 44 | } |
| 45 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 46 | static inline minikin::MeasuredText* toMeasuredParagraph(jlong ptr) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 47 | return reinterpret_cast<minikin::MeasuredText*>(ptr); |
| 48 | } |
| 49 | |
| 50 | template<typename Ptr> static inline jlong toJLong(Ptr ptr) { |
| 51 | return reinterpret_cast<jlong>(ptr); |
| 52 | } |
| 53 | |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 54 | static void releaseMeasuredParagraph(jlong measuredTextPtr) { |
| 55 | delete toMeasuredParagraph(measuredTextPtr); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Regular JNI |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 59 | static jlong nInitBuilder(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 60 | return toJLong(new minikin::MeasuredTextBuilder()); |
| 61 | } |
| 62 | |
| 63 | // Regular JNI |
| 64 | static void nAddStyleRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr, |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 65 | jlong paintPtr, jint lbStyle, jint lbWordStyle, jint start, jint end, |
| 66 | jboolean isRtl) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 67 | Paint* paint = toPaint(paintPtr); |
| 68 | const Typeface* typeface = Typeface::resolveDefault(paint->getAndroidTypeface()); |
| 69 | minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface); |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 70 | toBuilder(builderPtr) |
| 71 | ->addStyleRun(start, end, std::move(minikinPaint), lbStyle, lbWordStyle, isRtl); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Regular JNI |
| 75 | static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr, |
| 76 | jlong paintPtr, jint start, jint end, jfloat width) { |
| 77 | toBuilder(builderPtr)->addReplacementRun(start, end, width, |
| 78 | toPaint(paintPtr)->getMinikinLocaleListId()); |
| 79 | } |
| 80 | |
| 81 | // Regular JNI |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 82 | static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr, jlong hintPtr, |
| 83 | jcharArray javaText, jboolean computeHyphenation, |
| 84 | jboolean computeLayout, jboolean fastHyphenationMode) { |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 85 | ScopedCharArrayRO text(env, javaText); |
| 86 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 87 | |
| 88 | // Pass the ownership to Java. |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 89 | return toJLong(toBuilder(builderPtr) |
| 90 | ->build(textBuffer, computeHyphenation, computeLayout, |
| 91 | fastHyphenationMode, toMeasuredParagraph(hintPtr)) |
| 92 | .release()); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Regular JNI |
| 96 | static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) { |
| 97 | delete toBuilder(builderPtr); |
| 98 | } |
| 99 | |
| 100 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 101 | static jfloat nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint start, jint end) { |
Seigo Nonaka | 783f961 | 2018-01-20 12:11:13 -0800 | [diff] [blame] | 102 | minikin::MeasuredText* mt = toMeasuredParagraph(ptr); |
| 103 | float r = 0.0f; |
| 104 | for (int i = start; i < end; ++i) { |
| 105 | r += mt->widths[i]; |
| 106 | } |
| 107 | return r; |
| 108 | } |
| 109 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 110 | static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) { |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 111 | return toMeasuredParagraph(ptr)->widths[offset]; |
| 112 | } |
| 113 | |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 114 | // Regular JNI |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 115 | static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end, |
| 116 | jobject bounds) { |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 117 | ScopedCharArrayRO text(env, javaText); |
| 118 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 119 | const minikin::Range range(start, end); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 120 | |
Seigo Nonaka | fb0abe1 | 2018-04-02 23:25:38 -0700 | [diff] [blame] | 121 | minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range); |
Seigo Nonaka | a553477 | 2018-03-15 00:22:20 -0700 | [diff] [blame] | 122 | |
| 123 | SkRect r; |
| 124 | r.fLeft = rect.mLeft; |
| 125 | r.fTop = rect.mTop; |
| 126 | r.fRight = rect.mRight; |
| 127 | r.fBottom = rect.mBottom; |
| 128 | |
| 129 | SkIRect ir; |
| 130 | r.roundOut(&ir); |
| 131 | GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 132 | } |
| 133 | |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 134 | // Regular JNI |
| 135 | static jlong nGetExtent(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, |
| 136 | jint end) { |
| 137 | ScopedCharArrayRO text(env, javaText); |
| 138 | const minikin::U16StringPiece textBuffer(text.get(), text.size()); |
| 139 | const minikin::Range range(start, end); |
| 140 | |
| 141 | minikin::MinikinExtent extent = toMeasuredParagraph(ptr)->getExtent(textBuffer, range); |
| 142 | |
| 143 | int32_t ascent = SkScalarRoundToInt(extent.ascent); |
| 144 | int32_t descent = SkScalarRoundToInt(extent.descent); |
| 145 | |
| 146 | return (((jlong)(ascent)) << 32) | ((jlong)descent); |
| 147 | } |
| 148 | |
Seigo Nonaka | 783f961 | 2018-01-20 12:11:13 -0800 | [diff] [blame] | 149 | // CriticalNative |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 150 | static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 151 | return toJLong(&releaseMeasuredParagraph); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 154 | static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) { |
Seigo Nonaka | 49ca024 | 2018-01-24 16:46:14 -0800 | [diff] [blame] | 155 | return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage()); |
| 156 | } |
| 157 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 158 | static const JNINativeMethod gMTBuilderMethods[] = { |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 159 | // MeasuredParagraphBuilder native functions. |
| 160 | {"nInitBuilder", "()J", (void*)nInitBuilder}, |
James.cf Lin | 664a75b | 2022-01-19 16:56:34 +0800 | [diff] [blame] | 161 | {"nAddStyleRun", "(JJIIIIZ)V", (void*)nAddStyleRun}, |
Seigo Nonaka | 0f76140 | 2021-08-30 01:27:10 +0000 | [diff] [blame] | 162 | {"nAddReplacementRun", "(JJIIF)V", (void*)nAddReplacementRun}, |
| 163 | {"nBuildMeasuredText", "(JJ[CZZZ)J", (void*)nBuildMeasuredText}, |
| 164 | {"nFreeBuilder", "(J)V", (void*)nFreeBuilder}, |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 165 | }; |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 166 | |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 167 | static const JNINativeMethod gMTMethods[] = { |
Seigo Nonaka | 6b9b516 | 2022-02-03 15:20:20 +0900 | [diff] [blame] | 168 | // MeasuredParagraph native functions. |
| 169 | {"nGetWidth", "(JII)F", (void*)nGetWidth}, // Critical Natives |
| 170 | {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*)nGetBounds}, // Regular JNI |
| 171 | {"nGetExtent", "(J[CII)J", (void*)nGetExtent}, // Regular JNI |
| 172 | {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc}, // Critical Natives |
| 173 | {"nGetMemoryUsage", "(J)I", (void*)nGetMemoryUsage}, // Critical Native |
| 174 | {"nGetCharWidthAt", "(JI)F", (void*)nGetCharWidthAt}, // Critical Native |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 177 | int register_android_graphics_text_MeasuredText(JNIEnv* env) { |
| 178 | return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 179 | gMTMethods, NELEM(gMTMethods)) |
Seigo Nonaka | 70200b0 | 2018-10-01 16:04:11 -0700 | [diff] [blame] | 180 | + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder", |
Seigo Nonaka | 6f11c6e | 2018-07-24 11:26:18 -0700 | [diff] [blame] | 181 | gMTBuilderMethods, NELEM(gMTBuilderMethods)); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } |