[JetSki] pass SkPaint into experimental SkText drawText calls

tests currently through jetski demo

Change-Id: I0847e98579b62104e5d9ddaa7ca008f5a75be5c7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/441427
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
This commit is contained in:
Jorge Betancourt 2021-08-25 09:58:31 -04:00 committed by SkCQ
parent a3a8cba62e
commit c1727fc217
7 changed files with 84 additions and 40 deletions

View File

@ -47,7 +47,6 @@ protected:
const std::u16string& text,
TextAlign align,
TextDirection direction = TextDirection::kLtr) {
SkColor background = SK_ColorGRAY;
const std::u16string& ellipsis = u"\u2026";
SkScalar margin = 20;
@ -56,10 +55,12 @@ protected:
canvas->clipRect(SkRect::MakeWH(w, h));
canvas->drawColor(SK_ColorWHITE);
SkPaint foregroundPaint(SkColors::kBlack);
SkPaint backgroundPaint(SkColors::kLtGray);
Paint::drawText(direction == TextDirection::kRtl ? mirror(text) : normal(text),
canvas,
direction, align,
SK_ColorBLACK, SK_ColorLTGRAY,
foregroundPaint, backgroundPaint,
SkString("Roboto"), 12.0f, SkFontStyle::Normal(),
0, 0);
}

View File

@ -5,19 +5,22 @@ namespace skia {
namespace text {
bool Paint::drawText(std::u16string text, SkCanvas* canvas, SkScalar x, SkScalar y) {
return drawText(std::move(text), canvas, TextDirection::kLtr, TextAlign::kLeft, SK_ColorBLACK, SK_ColorWHITE, SkString("Roboto"), 14, SkFontStyle::Normal(), x, y);
SkPaint foregroundPaint(SkColors::kBlack);
SkPaint backgroundPaint(SkColors::kWhite);
return drawText(std::move(text), canvas, TextDirection::kLtr, TextAlign::kLeft, foregroundPaint, backgroundPaint, SkString("Roboto"), 14, SkFontStyle::Normal(), x, y);
}
bool Paint::drawText(std::u16string text, SkCanvas* canvas, SkScalar width) {
SkPaint foregroundPaint(SkColors::kBlack);
SkPaint backgroundPaint(SkColors::kWhite);
return drawText(std::move(text), canvas,
TextDirection::kLtr, TextAlign::kLeft, SK_ColorBLACK, SK_ColorWHITE, SkString("Roboto"), 14, SkFontStyle::Normal(),
TextDirection::kLtr, TextAlign::kLeft, foregroundPaint, backgroundPaint, SkString("Roboto"), 14, SkFontStyle::Normal(),
SkSize::Make(width, SK_ScalarInfinity), 0, 0);
}
bool Paint::drawText(std::u16string text, SkCanvas* canvas,
TextDirection textDirection, TextAlign textAlign,
SkColor foreground, SkColor background,
SkPaint foreground, SkPaint background,
const SkString& fontFamily, SkScalar fontSize, SkFontStyle fontStyle, SkScalar x, SkScalar y) {
return drawText(std::move(text), canvas,
textDirection, textAlign, foreground, background,
@ -26,7 +29,7 @@ namespace text {
bool Paint::drawText(std::u16string text, SkCanvas* canvas,
TextDirection textDirection, TextAlign textAlign,
SkColor foreground, SkColor background,
SkPaint foreground, SkPaint background,
const SkString& fontFamily, SkScalar fontSize, SkFontStyle fontStyle, SkSize reqSize, SkScalar x, SkScalar y) {
size_t textSize = text.size();
@ -41,9 +44,7 @@ namespace text {
return false;
}
SkPaint backgroundPaint; backgroundPaint.setColor(background);
SkPaint foregroundPaint; foregroundPaint.setColor(foreground);
DecoratedBlock decoratedBlock(textSize, foregroundPaint, backgroundPaint);
DecoratedBlock decoratedBlock(textSize, foreground, background);
Paint paint;
paint.paint(canvas, SkPoint::Make(x, y), formattedText.get(), SkSpan<DecoratedBlock>(&decoratedBlock, 1));

View File

@ -47,12 +47,12 @@ namespace text {
static bool drawText(std::u16string text, SkCanvas* canvas, SkScalar width);
static bool drawText(std::u16string text, SkCanvas* canvas,
TextDirection textDirection, TextAlign textAlign,
SkColor foreground, SkColor background,
SkPaint foreground, SkPaint background,
const SkString& fontFamily, SkScalar fontSize, SkFontStyle fontStyle,
SkScalar x, SkScalar y);
static bool drawText(std::u16string text, SkCanvas* canvas,
TextDirection textDirection, TextAlign textAlign,
SkColor foreground, SkColor background,
SkPaint foreground, SkPaint background,
const SkString& fontFamily, SkScalar fontSize, SkFontStyle fontStyle,
SkSize reqSize, SkScalar x, SkScalar y);

View File

@ -23,7 +23,7 @@ static long ImageFilter_DistantLitDiffuse(JNIEnv* env, jobject, jfloat x, jfloat
jfloat surfaceScale, jfloat kd,
jlong native_input) {
SkPoint3 direction = SkPoint3::Make(x, y, z);
auto color = SkColor4f{r, g, b, 1}.toSkColor();
auto color = SkColor4f{r, g, b, 1}.toSkColor();
auto input = sk_ref_sp(reinterpret_cast<SkImageFilter*>(native_input));
auto filter = SkImageFilters::DistantLitDiffuse(direction, color, surfaceScale, kd, std::move(input));
return reinterpret_cast<jlong>(filter.release());

View File

@ -24,12 +24,16 @@ static std::u16string JStringToU16String(JNIEnv* env, const jstring& jstr) {
}
static void Text_RenderText(JNIEnv* env, jobject, jstring jtext,
jlong native_canvas, jfloat x, jfloat y) {
jlong native_canvas, jlong native_fg_paint,
jfloat x, jfloat y) {
auto* canvas = reinterpret_cast<SkCanvas*>(native_canvas);
auto foreground = reinterpret_cast<SkPaint*>(native_fg_paint);
std::u16string u16str = JStringToU16String(env, jtext);
if (auto* canvas = reinterpret_cast<SkCanvas*>(native_canvas)) {
if (canvas && foreground) {
SkPaint background(SkColors::kTransparent);
Paint::drawText(u16str, canvas,
skia::text::TextDirection::kLtr, skia::text::TextAlign::kLeft,
0xffff0000, 0xff00ff00, SkString("arial"), 50, SkFontStyle::Bold(), x, y);
*foreground, background, SkString("arial"), 50, SkFontStyle::Bold(), x, y);
}
}
@ -37,7 +41,7 @@ static void Text_RenderText(JNIEnv* env, jobject, jstring jtext,
int register_androidkit_Text(JNIEnv* env) {
static const JNINativeMethod methods[] = {
{"nRenderText", "(Ljava/lang/String;JFF)V", reinterpret_cast<void*>(Text_RenderText)},
{"nRenderText", "(Ljava/lang/String;JJFF)V", reinterpret_cast<void*>(Text_RenderText)},
};
const auto clazz = env->FindClass("org/skia/androidkit/Text");

View File

@ -15,9 +15,13 @@ public class Text {
mRawText = text;
}
public void renderText(Canvas canvas, float x, float y) {
nRenderText(mRawText, canvas.getNativeInstance(), x, y);
public void setText(String text) {
mRawText = text;
}
private static native void nRenderText(String text, long nativeCanvas, float x, float y);
public void renderText(Canvas canvas, Paint foreground, float x, float y) {
nRenderText(mRawText, canvas.getNativeInstance(), foreground.getNativeInstance(), x, y);
}
private static native void nRenderText(String text, long nativeCanvas, long foreground, float x, float y);
}

View File

@ -1,19 +1,68 @@
package org.skia.androidkitdemo1;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import org.skia.androidkit.Canvas;
import org.skia.androidkit.Paint;
import org.skia.androidkit.RuntimeShaderBuilder;
import org.skia.androidkit.Surface;
import org.skia.androidkit.Text;
import org.skia.androidkit.util.SurfaceRenderer;
import org.skia.androidkitdemo1.samples.RuntimeSample;
public class TextActivity extends Activity implements SurfaceHolder.Callback {
import java.io.InputStream;
class TextRenderer extends SurfaceRenderer {
Surface mSurface;
Text mText;
Paint foreground;
private static final String sksl =
"uniform half u_time; " +
"uniform half u_w; " +
"uniform half u_h; " +
"float f(vec3 p) { " +
" p.z -= u_time * 10.; " +
" float a = p.z * .1; " +
" p.xy *= mat2(cos(a), sin(a), -sin(a), cos(a)); " +
" return .1 - length(cos(p.xy) + sin(p.yz)); " +
"} " +
"half4 main(vec2 fragcoord) { " +
" vec3 d = .5 - fragcoord.xy1 / u_h; " +
" vec3 p=vec3(0); " +
" for (int i = 0; i < 32; i++) p += f(p) * d; " +
" return ((sin(p) + vec3(2, 5, 9)) / length(p)).xyz1;" +
"}";
private RuntimeShaderBuilder builder;
private float W;
private float H;
@Override
protected void onSurfaceInitialized(Surface surface) {
mSurface = surface;
W = surface.getWidth();
H = surface.getHeight();
mText = new Text("Hello World!");
foreground = new Paint().setColor(0, 1, 0, 1);
builder = new RuntimeShaderBuilder(sksl);
}
@Override
protected void onRenderFrame(Canvas canvas, long ms) {
canvas.drawColor(1, 1, 1, 1);
builder.setUniform("u_time", ms / 1000.0f)
.setUniform("u_w", W)
.setUniform("u_h", H);
foreground.setShader(builder.makeShader());
mText.renderText(canvas, foreground, 0, 200);
}
}
public class TextActivity extends Activity {
static {
System.loadLibrary("androidkit");
}
@ -25,23 +74,8 @@ public class TextActivity extends Activity implements SurfaceHolder.Callback {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
TextRenderer renderer = new TextRenderer();
SurfaceView sv = findViewById(R.id.surfaceView);
sv.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
mSurface = Surface.CreateGL(holder.getSurface());
Text text = new Text("Hello World!");
text.renderText(mSurface.getCanvas(), 0, 200);
mSurface.flushAndSubmit();
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
sv.getHolder().addCallback(renderer);
}
}