• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "GraphicsJNI.h"
18 #include "utils/misc.h"
19 #include "utils/Log.h"
20 #include <nativehelper/ScopedStringChars.h>
21 #include <nativehelper/ScopedPrimitiveArray.h>
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 
toBuilder(jlong ptr)38 static inline minikin::MeasuredTextBuilder* toBuilder(jlong ptr) {
39     return reinterpret_cast<minikin::MeasuredTextBuilder*>(ptr);
40 }
41 
toPaint(jlong ptr)42 static inline Paint* toPaint(jlong ptr) {
43     return reinterpret_cast<Paint*>(ptr);
44 }
45 
toMeasuredParagraph(jlong ptr)46 static inline minikin::MeasuredText* toMeasuredParagraph(jlong ptr) {
47     return reinterpret_cast<minikin::MeasuredText*>(ptr);
48 }
49 
toJLong(Ptr ptr)50 template<typename Ptr> static inline jlong toJLong(Ptr ptr) {
51     return reinterpret_cast<jlong>(ptr);
52 }
53 
releaseMeasuredParagraph(jlong measuredTextPtr)54 static void releaseMeasuredParagraph(jlong measuredTextPtr) {
55     delete toMeasuredParagraph(measuredTextPtr);
56 }
57 
58 // Regular JNI
nInitBuilder(CRITICAL_JNI_PARAMS)59 static jlong nInitBuilder(CRITICAL_JNI_PARAMS) {
60     return toJLong(new minikin::MeasuredTextBuilder());
61 }
62 
63 // Regular JNI
nAddStyleRun(JNIEnv *,jclass,jlong builderPtr,jlong paintPtr,jint lbStyle,jint lbWordStyle,jboolean hyphenation,jint start,jint end,jboolean isRtl)64 static void nAddStyleRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
65                          jlong paintPtr, jint lbStyle, jint lbWordStyle, jboolean hyphenation,
66                          jint start, jint end, jboolean isRtl) {
67     Paint* paint = toPaint(paintPtr);
68     const Typeface* typeface = Typeface::resolveDefault(paint->getAndroidTypeface());
69     minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface);
70     toBuilder(builderPtr)
71             ->addStyleRun(start, end, std::move(minikinPaint), lbStyle, lbWordStyle, hyphenation,
72                           isRtl);
73 }
74 
75 // Regular JNI
nAddReplacementRun(JNIEnv *,jclass,jlong builderPtr,jlong paintPtr,jint start,jint end,jfloat width)76 static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
77                                jlong paintPtr, jint start, jint end, jfloat width) {
78     toBuilder(builderPtr)->addReplacementRun(start, end, width,
79                                              toPaint(paintPtr)->getMinikinLocaleListId());
80 }
81 
82 // Regular JNI
nBuildMeasuredText(JNIEnv * env,jclass,jlong builderPtr,jlong hintPtr,jcharArray javaText,jboolean computeHyphenation,jboolean computeLayout,jboolean computeBounds,jboolean fastHyphenationMode)83 static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr, jlong hintPtr,
84                                 jcharArray javaText, jboolean computeHyphenation,
85                                 jboolean computeLayout, jboolean computeBounds,
86                                 jboolean fastHyphenationMode) {
87     ScopedCharArrayRO text(env, javaText);
88     const minikin::U16StringPiece textBuffer(text.get(), text.size());
89 
90     // Pass the ownership to Java.
91     return toJLong(toBuilder(builderPtr)
92                            ->build(textBuffer, computeHyphenation, computeLayout, computeBounds,
93                                    fastHyphenationMode, toMeasuredParagraph(hintPtr))
94                            .release());
95 }
96 
97 // Regular JNI
nFreeBuilder(JNIEnv * env,jclass,jlong builderPtr)98 static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) {
99     delete toBuilder(builderPtr);
100 }
101 
102 // CriticalNative
nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint start,jint end)103 static jfloat nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint start, jint end) {
104     minikin::MeasuredText* mt = toMeasuredParagraph(ptr);
105     float r = 0.0f;
106     for (int i = start; i < end; ++i) {
107         r += mt->widths[i];
108     }
109     return r;
110 }
111 
nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint offset)112 static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) {
113     return toMeasuredParagraph(ptr)->widths[offset];
114 }
115 
116 // Regular JNI
nGetBounds(JNIEnv * env,jobject,jlong ptr,jcharArray javaText,jint start,jint end,jobject bounds)117 static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end,
118                        jobject bounds) {
119     ScopedCharArrayRO text(env, javaText);
120     const minikin::U16StringPiece textBuffer(text.get(), text.size());
121     const minikin::Range range(start, end);
122 
123     minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range);
124 
125     SkRect r;
126     r.fLeft = rect.mLeft;
127     r.fTop = rect.mTop;
128     r.fRight = rect.mRight;
129     r.fBottom = rect.mBottom;
130 
131     SkIRect ir;
132     r.roundOut(&ir);
133     GraphicsJNI::irect_to_jrect(ir, env, bounds);
134 }
135 
136 // Regular JNI
nGetExtent(JNIEnv * env,jobject,jlong ptr,jcharArray javaText,jint start,jint end)137 static jlong nGetExtent(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start,
138                         jint end) {
139     ScopedCharArrayRO text(env, javaText);
140     const minikin::U16StringPiece textBuffer(text.get(), text.size());
141     const minikin::Range range(start, end);
142 
143     minikin::MinikinExtent extent = toMeasuredParagraph(ptr)->getExtent(textBuffer, range);
144 
145     int32_t ascent = SkScalarRoundToInt(extent.ascent);
146     int32_t descent = SkScalarRoundToInt(extent.descent);
147 
148     return (((jlong)(ascent)) << 32) | ((jlong)descent);
149 }
150 
151 // CriticalNative
nGetReleaseFunc(CRITICAL_JNI_PARAMS)152 static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) {
153     return toJLong(&releaseMeasuredParagraph);
154 }
155 
nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr)156 static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
157     return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage());
158 }
159 
160 static const JNINativeMethod gMTBuilderMethods[] = {
161         // MeasuredParagraphBuilder native functions.
162         {"nInitBuilder", "()J", (void*)nInitBuilder},
163         {"nAddStyleRun", "(JJIIZIIZ)V", (void*)nAddStyleRun},
164         {"nAddReplacementRun", "(JJIIF)V", (void*)nAddReplacementRun},
165         {"nBuildMeasuredText", "(JJ[CZZZZ)J", (void*)nBuildMeasuredText},
166         {"nFreeBuilder", "(J)V", (void*)nFreeBuilder},
167 };
168 
169 static const JNINativeMethod gMTMethods[] = {
170         // MeasuredParagraph native functions.
171         {"nGetWidth", "(JII)F", (void*)nGetWidth},                             // Critical Natives
172         {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*)nGetBounds},  // Regular JNI
173         {"nGetExtent", "(J[CII)J", (void*)nGetExtent},                         // Regular JNI
174         {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc},                    // Critical Natives
175         {"nGetMemoryUsage", "(J)I", (void*)nGetMemoryUsage},                   // Critical Native
176         {"nGetCharWidthAt", "(JI)F", (void*)nGetCharWidthAt},                  // Critical Native
177 };
178 
register_android_graphics_text_MeasuredText(JNIEnv * env)179 int register_android_graphics_text_MeasuredText(JNIEnv* env) {
180     return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText",
181             gMTMethods, NELEM(gMTMethods))
182         + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder",
183             gMTBuilderMethods, NELEM(gMTBuilderMethods));
184 }
185 
186 }
187