Skip to content

Commit fb9ffe0

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "First pass at implementing Canvas.drawPosText() in GL"
2 parents 1be4afe + eb9a536 commit fb9ffe0

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

core/java/android/view/GLES20Canvas.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,14 +945,38 @@ private static native void nDrawPoints(int renderer, float[] points,
945945

946946
@Override
947947
public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
948-
// TODO: Implement
948+
if (index < 0 || index + count > text.length || count * 2 > pos.length) {
949+
throw new IndexOutOfBoundsException();
950+
}
951+
952+
int modifiers = setupModifiers(paint);
953+
try {
954+
nDrawPosText(mRenderer, text, index, count, pos, paint.mNativePaint);
955+
} finally {
956+
if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
957+
}
949958
}
950959

960+
private static native void nDrawPosText(int renderer, char[] text, int index, int count,
961+
float[] pos, int paint);
962+
951963
@Override
952964
public void drawPosText(String text, float[] pos, Paint paint) {
953-
// TODO: Implement
965+
if (text.length() * 2 > pos.length) {
966+
throw new ArrayIndexOutOfBoundsException();
967+
}
968+
969+
int modifiers = setupModifiers(paint);
970+
try {
971+
nDrawPosText(mRenderer, text, 0, text.length(), pos, paint.mNativePaint);
972+
} finally {
973+
if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
974+
}
954975
}
955976

977+
private static native void nDrawPosText(int renderer, String text, int start, int end,
978+
float[] pos, int paint);
979+
956980
@Override
957981
public void drawRect(float left, float top, float right, float bottom, Paint paint) {
958982
int modifiers = setupModifiers(paint);

core/jni/android_view_GLES20Canvas.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,54 @@ static void android_view_GLES20Canvas_drawTextRun(JNIEnv* env, jobject clazz,
567567
env->ReleaseStringChars(text, textArray);
568568
}
569569

570+
static void renderPosText(OpenGLRenderer* renderer, const jchar* text, int count,
571+
const jfloat* positions, jint dirFlags, SkPaint* paint) {
572+
sp<TextLayoutCacheValue> value;
573+
#if USE_TEXT_LAYOUT_CACHE
574+
value = TextLayoutCache::getInstance().getValue(paint, text, 0, count, count, dirFlags);
575+
if (value == NULL) {
576+
ALOGE("Cannot get TextLayoutCache value for text = '%s'",
577+
String8(text, count).string());
578+
return;
579+
}
580+
#else
581+
value = new TextLayoutCacheValue(count);
582+
TextLayoutEngine::getInstance().computeValues(value.get(), paint,
583+
text, 0, count, count, dirFlags);
584+
#endif
585+
586+
const jchar* glyphs = value->getGlyphs();
587+
size_t glyphsCount = value->getGlyphsCount();
588+
int bytesCount = glyphsCount * sizeof(jchar);
589+
590+
renderer->drawPosText((const char*) glyphs, bytesCount,
591+
count < int(glyphsCount) ? count : glyphsCount, positions, paint);
592+
}
593+
594+
static void android_view_GLES20Canvas_drawPosTextArray(JNIEnv* env, jobject clazz,
595+
OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
596+
jfloatArray pos, SkPaint* paint) {
597+
jchar* textArray = env->GetCharArrayElements(text, NULL);
598+
jfloat* positions = env->GetFloatArrayElements(pos, NULL);
599+
600+
renderPosText(renderer, textArray + index, count, positions, kBidi_LTR, paint);
601+
602+
env->ReleaseFloatArrayElements(pos, positions, JNI_ABORT);
603+
env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
604+
}
605+
606+
static void android_view_GLES20Canvas_drawPosText(JNIEnv* env, jobject clazz,
607+
OpenGLRenderer* renderer, jstring text, jint start, jint end,
608+
jfloatArray pos, SkPaint* paint) {
609+
const jchar* textArray = env->GetStringChars(text, NULL);
610+
jfloat* positions = env->GetFloatArrayElements(pos, NULL);
611+
612+
renderPosText(renderer, textArray + start, end - start, positions, kBidi_LTR, paint);
613+
614+
env->ReleaseFloatArrayElements(pos, positions, JNI_ABORT);
615+
env->ReleaseStringChars(text, textArray);
616+
}
617+
570618
// ----------------------------------------------------------------------------
571619
// Display lists
572620
// ----------------------------------------------------------------------------
@@ -830,6 +878,10 @@ static JNINativeMethod gMethods[] = {
830878
{ "nDrawTextRun", "(ILjava/lang/String;IIIIFFII)V",
831879
(void*) android_view_GLES20Canvas_drawTextRun },
832880

881+
{ "nDrawPosText", "(I[CII[FI)V", (void*) android_view_GLES20Canvas_drawPosTextArray },
882+
{ "nDrawPosText", "(ILjava/lang/String;II[FI)V",
883+
(void*) android_view_GLES20Canvas_drawPosText },
884+
833885
{ "nGetClipBounds", "(ILandroid/graphics/Rect;)Z",
834886
(void*) android_view_GLES20Canvas_getClipBounds },
835887

libs/hwui/DisplayListRenderer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const char* DisplayList::OP_NAMES[] = {
6060
"DrawLines",
6161
"DrawPoints",
6262
"DrawText",
63+
"DrawPosText",
6364
"ResetShader",
6465
"SetupShader",
6566
"ResetColorFilter",
@@ -482,6 +483,15 @@ void DisplayList::output(OpenGLRenderer& renderer, uint32_t level) {
482483
text.text(), text.length(), count, x, y, paint, length);
483484
}
484485
break;
486+
case DrawPosText: {
487+
getText(&text);
488+
int count = getInt();
489+
int positionsCount = 0;
490+
float* positions = getFloats(positionsCount);
491+
SkPaint* paint = getPaint();
492+
ALOGD("%s%s %s, %d, %d, %p", (char*) indent, OP_NAMES[op],
493+
text.text(), text.length(), count, paint);
494+
}
485495
case ResetShader: {
486496
ALOGD("%s%s", (char*) indent, OP_NAMES[op]);
487497
}
@@ -844,6 +854,17 @@ bool DisplayList::replay(OpenGLRenderer& renderer, Rect& dirty, uint32_t level)
844854
renderer.drawText(text.text(), text.length(), count, x, y, paint, length);
845855
}
846856
break;
857+
case DrawPosText: {
858+
getText(&text);
859+
int count = getInt();
860+
int positionsCount = 0;
861+
float* positions = getFloats(positionsCount);
862+
SkPaint* paint = getPaint();
863+
DISPLAY_LIST_LOGD("%s%s %s, %d, %d, %p", (char*) indent,
864+
OP_NAMES[op], text.text(), text.length(), count, paint);
865+
renderer.drawPosText(text.text(), text.length(), count, positions, paint);
866+
}
867+
break;
847868
case ResetShader: {
848869
DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
849870
renderer.resetShader();
@@ -1216,6 +1237,17 @@ void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
12161237
addFloat(length < 0.0f ? paint->measureText(text, bytesCount) : length);
12171238
}
12181239

1240+
void DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
1241+
const float* positions, SkPaint* paint) {
1242+
if (count <= 0) return;
1243+
addOp(DisplayList::DrawPosText);
1244+
addText(text, bytesCount);
1245+
addInt(count);
1246+
addFloats(positions, count * 2);
1247+
paint->setAntiAlias(true);
1248+
addPaint(paint);
1249+
}
1250+
12191251
void DisplayListRenderer::resetShader() {
12201252
addOp(DisplayList::ResetShader);
12211253
}

libs/hwui/DisplayListRenderer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class DisplayList {
9696
DrawLines,
9797
DrawPoints,
9898
DrawText,
99+
DrawPosText,
99100
ResetShader,
100101
SetupShader,
101102
ResetColorFilter,
@@ -291,6 +292,8 @@ class DisplayListRenderer: public OpenGLRenderer {
291292
virtual void drawPoints(float* points, int count, SkPaint* paint);
292293
virtual void drawText(const char* text, int bytesCount, int count, float x, float y,
293294
SkPaint* paint, float length = 1.0f);
295+
virtual void drawPosText(const char* text, int bytesCount, int count, const float* positions,
296+
SkPaint* paint);
294297

295298
virtual void resetShader();
296299
virtual void setupShader(SkiaShader* shader);

libs/hwui/OpenGLRenderer.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,17 @@ void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
20822082
}
20832083
}
20842084

2085+
void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2086+
const float* positions, SkPaint* paint) {
2087+
if (text == NULL || count == 0) {
2088+
return;
2089+
}
2090+
if (mSnapshot->isIgnored()) return;
2091+
2092+
// TODO: implement properly
2093+
drawText(text, bytesCount, count, 0, 0, paint);
2094+
}
2095+
20852096
void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
20862097
float x, float y, SkPaint* paint, float length) {
20872098
if (text == NULL || count == 0) {
@@ -2120,10 +2131,11 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
21202131
y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
21212132
}
21222133

2123-
FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
21242134
#if DEBUG_GLYPHS
21252135
ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
21262136
#endif
2137+
2138+
FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
21272139
fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
21282140
paint->getTextSize());
21292141

libs/hwui/OpenGLRenderer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class OpenGLRenderer {
124124
virtual void drawPoints(float* points, int count, SkPaint* paint);
125125
virtual void drawText(const char* text, int bytesCount, int count, float x, float y,
126126
SkPaint* paint, float length = -1.0f);
127+
virtual void drawPosText(const char* text, int bytesCount, int count, const float* positions,
128+
SkPaint* paint);
127129

128130
virtual void resetShader();
129131
virtual void setupShader(SkiaShader* shader);

0 commit comments

Comments
 (0)