Skip to content

Commit

Permalink
DOM -> SVGDOM, SVGCanvas, DeleteLocalRefs in *.cc (closes #93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Feb 19, 2021
1 parent d751941 commit 0c35089
Show file tree
Hide file tree
Showing 24 changed files with 329 additions and 26 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ImageFilters ▓▓▓▓▓▓▓▓▓▓ Surface ▓
ImageInfo ▓▓▓▓▓▓▓▓▓▓ TextBlob ▓▓▓▓▓▓▓▓▓▓
MaskFilter ▓▓▓▓▓▓▓▓▓▓ TextBlobBuilder ▓▓▓▓▓▓▓▓▓▓
Matrix33 ▓▓▓░░░░░░░ Typeface ▓▓▓▓▓▓▓▓░░
Matrix44 ▓▓▓░░░░░░░
Matrix44 ▓▓▓░░░░░░░ WStream ▓▓░░░░░░░░
Shaper: Paragraph:
Expand All @@ -128,7 +128,9 @@ TextBlobBldRunHndlr ▓▓▓▓▓▓▓▓▓▓
SVG:
DOM ▓▓▓▓▓▓▓▓░░
SVGDOM ▓▓▓▓▓▓▓▓░░
SVGCanvas ▓▓▓▓▓▓▓▓▓▓
```

## Using Skija
Expand Down
4 changes: 2 additions & 2 deletions examples/scenes/src/SVGScalingScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int
canvas.drawString(img, 0, -20, inter13, fill);

try (var data = Data.makeFromFileName(file(img));
var svg = new DOM(data);)
var svg = new SVGDOM(data);)
{
var containerSize = svg.getContainerSize();
if (!containerSize.isEmpty())
Expand All @@ -38,7 +38,7 @@ public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int
}
}

public void drawIcon(Canvas canvas, Point size, DOM svg) {
public void drawIcon(Canvas canvas, Point size, SVGDOM svg) {
svg.setContainerSize(size);
svg.render(canvas);
canvas.drawRect(Rect.makeWH(size.getX(), size.getY()), border);
Expand Down
4 changes: 2 additions & 2 deletions examples/scenes/src/SVGScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class SVGScene extends Scene {
public Thread _thread = null;
public DOM _dom = null;
public SVGDOM _dom = null;
public Throwable _error = null;

@SneakyThrows
Expand Down Expand Up @@ -55,7 +55,7 @@ public void run() {
in.transferTo(out);
if (_thread == this) {
try (var data = org.jetbrains.skija.Data.makeFromBytes(out.toByteArray());) {
_dom = new DOM(data);
_dom = new SVGDOM(data);
}
}
} catch (Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion native/cmake/FindSkia.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ find_path(SKIA_SKSG_INCLUDE_DIR SkSGInvalidationController.h HINTS "${SKIA_DIR}/
find_path(SKIA_CONFIG_INCLUDE_DIR SkUserConfig.h HINTS "${SKIA_DIR}/include/config")
find_path(SKIA_CORE_INCLUDE_DIR SkCanvas.h HINTS "${SKIA_DIR}/include/core")
find_path(SKIA_PATHOPS_INCLUDE_DIR SkPathOps.h HINTS "${SKIA_DIR}/include/pathops")
find_path(SKIA_CORE_SVG_INCLUDE_DIR SkSVGCanvas.h HINTS "${SKIA_DIR}/include/svg")
find_path(SKIA_UTILS_INCLUDE_DIR SkRandom.h HINTS "${SKIA_DIR}/include/utils")
find_path(SKIA_CODEC_INCLUDE_DIR SkCodec.h HINTS "${SKIA_DIR}/include/codec")
find_path(SKIA_EFFECTS_INCLUDE_DIR SkImageSource.h HINTS "${SKIA_DIR}/include/effects")
Expand All @@ -92,7 +93,7 @@ target_include_directories(skia INTERFACE
${SKIA_CONFIG_INCLUDE_DIR}
${SKIA_CORE_INCLUDE_DIR}
${SKIA_PATHOPS_INCLUDE_DIR}
${SKIA_PORTS_INCLUDE_DIR}
${SKIA_CORE_SVG_INCLUDE_DIR}
${SKIA_UTILS_INCLUDE_DIR}
${SKIA_CODEC_INCLUDE_DIR}
${SKIA_EFFECTS_INCLUDE_DIR}
Expand Down
4 changes: 2 additions & 2 deletions native/script/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def main():
"--config", build_type,
"-DSKIA_DIR=" + skia_dir,
".."],
cwd="build")
cwd=os.path.abspath('build'))

# Ninja
common.check_call(["ninja"], cwd="build")
common.check_call(["ninja"], cwd=os.path.abspath('build'))

# icudtl
icudtl_src = skia_dir + "/out/" + build_type + "-" + common.machine + "/icudtl.dat"
Expand Down
6 changes: 4 additions & 2 deletions native/script/clean.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#! /usr/bin/env python3

import os, shutil, sys
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..')))
import script.common as common

def main():
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
common.pushd(os.path.join(os.path.dirname(__file__), os.pardir))
shutil.rmtree("build", ignore_errors = True)
shutil.rmtree(os.path.join(os.path.expanduser('~'), '.m2', 'repository', 'org', 'jetbrains', 'skija', 'skija-native', '0.0.0-SNAPSHOT'), ignore_errors = True)
common.popd()
return 0

if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions native/src/Font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_Font__1nGetPa
orig->transform(mx, path);
jobject pathObj = ctx->env->NewObject(skija::Path::cls, skija::Path::ctor, reinterpret_cast<jlong>(path));
ctx->env->SetObjectArrayElement(ctx->paths, ctx->idx, pathObj);
ctx->env->DeleteLocalRef(pathObj);
++ctx->idx;
}
}, &ctx);
Expand Down
56 changes: 56 additions & 0 deletions native/src/OutputWStream.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <jni.h>
#include "interop.hh"
#include "SkStream.h"

class OutputWStream: public SkWStream {
public:
OutputWStream(JNIEnv* env, jobject out): fEnv(env) {
fOut = env->NewGlobalRef(out);
}

~OutputWStream() override {
fEnv->DeleteGlobalRef(fOut);
}

size_t bytesWritten() const override {
return fBytesWritten;
}

bool write(const void* buffer, size_t size) override {
jbyteArray arr = fEnv->NewByteArray((jsize) size);
fEnv->SetByteArrayRegion(arr, 0, (jsize) size, static_cast<const jbyte *>(buffer));
fEnv->CallVoidMethod(fOut, java::io::OutputStream::write, arr, 0, (jint) size);
fEnv->DeleteLocalRef(arr);
if (java::lang::Throwable::exceptionThrown(fEnv)) {
return false;
} else {
fBytesWritten += size;
return true;
}
}

void flush() override {
fEnv->CallVoidMethod(fOut, java::io::OutputStream::flush);
}

private:
JNIEnv* fEnv;
jobject fOut;
size_t fBytesWritten = 0;
};

static void deleteOutputWStream(OutputWStream* out) {
delete out;
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_OutputWStream__1nGetFinalizer
(JNIEnv* env, jclass jclass) {
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deleteOutputWStream));
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_OutputWStream__1nMake
(JNIEnv* env, jclass jclass, jobject outputStream) {
OutputWStream* instance = new OutputWStream(env, outputStream);
return reinterpret_cast<jlong>(instance);
}
37 changes: 37 additions & 0 deletions native/src/interop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
#include "paragraph/interop.hh"

namespace java {
namespace io {
namespace OutputStream {
jclass cls;
jmethodID write;
jmethodID flush;

void onLoad(JNIEnv* env) {
jclass cls = env->FindClass("java/io/OutputStream");
write = env->GetMethodID(cls, "write", "([BII)V");
flush = env->GetMethodID(cls, "flush", "()V");
}
}
}

namespace lang {
namespace Float {
jclass cls;
Expand Down Expand Up @@ -37,6 +51,25 @@ namespace java {
env->DeleteGlobalRef(cls);
}
}

namespace Throwable {
jmethodID printStackTrace;

void onLoad(JNIEnv* env) {
jclass cls = env->FindClass("java/lang/Throwable");
printStackTrace = env->GetMethodID(cls, "printStackTrace", "()V");
}

bool exceptionThrown(JNIEnv* env) {
if (env->ExceptionCheck()) {
jthrowable th = env->ExceptionOccurred();
env->CallVoidMethod(th, printStackTrace);
env->ExceptionCheck(); // ignore
return true;
} else
return false;
}
}
}

namespace util {
Expand Down Expand Up @@ -76,8 +109,10 @@ namespace java {
}

void onLoad(JNIEnv* env) {
io::OutputStream::onLoad(env);
lang::Float::onLoad(env);
lang::String::onLoad(env);
lang::Throwable::onLoad(env);
util::Iterator::onLoad(env);
util::function::BooleanSupplier::onLoad(env);
}
Expand Down Expand Up @@ -169,6 +204,7 @@ namespace skija {
static_cast<uint32_t>(env->GetIntField(featureObj, skija::FontFeature::value)),
static_cast<size_t>(env->GetLongField(featureObj, skija::FontFeature::start)),
static_cast<size_t>(env->GetLongField(featureObj, skija::FontFeature::end))};
env->DeleteLocalRef(featureObj);
}
return features;
}
Expand Down Expand Up @@ -889,6 +925,7 @@ std::vector<SkString> skStringVector(JNIEnv* env, jobjectArray arr) {
for (jint i = 0; i < len; ++i) {
jstring str = static_cast<jstring>(env->GetObjectArrayElement(arr, i));
res[i] = skString(env, str);
env->DeleteLocalRef(str);
}
return res;
}
Expand Down
14 changes: 14 additions & 0 deletions native/src/interop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
#include "SkSurfaceProps.h"

namespace java {
namespace io {
namespace OutputStream {
extern jmethodID write;
extern jmethodID flush;
void onLoad(JNIEnv* env);
}
}

namespace lang {
namespace Float {
extern jclass cls;
Expand All @@ -29,6 +37,12 @@ namespace java {
void onLoad(JNIEnv* env);
void onUnload(JNIEnv* env);
}

namespace Throwable {
extern jmethodID printStackTrace;
void onLoad(JNIEnv* env);
bool exceptionThrown(JNIEnv* env);
}
}

namespace util {
Expand Down
1 change: 1 addition & 0 deletions native/src/paragraph/FontCollection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extern "C" JNIEXPORT jlongArray JNICALL Java_org_jetbrains_skija_paragraph_FontC
for (int i = 0; i < len; ++i) {
jstring str = static_cast<jstring>(env->GetObjectArrayElement(familyNamesArray, i));
familyNames.push_back(skString(env, str));
env->DeleteLocalRef(str);
}

vector<sk_sp<SkTypeface>> found = instance->findTypefaces(familyNames, skija::FontStyle::fromJava(fontStyle));
Expand Down
3 changes: 3 additions & 0 deletions native/src/paragraph/Paragraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_paragraph_Par
TextBox box = rects[i];
jobject boxObj = env->NewObject(skija::paragraph::TextBox::cls, skija::paragraph::TextBox::ctor, box.rect.fLeft, box.rect.fTop, box.rect.fRight, box.rect.fBottom, static_cast<jint>(box.direction));
env->SetObjectArrayElement(rectsArray, i, boxObj);
env->DeleteLocalRef(boxObj);
}
return rectsArray;
}
Expand All @@ -101,6 +102,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_paragraph_Par
TextBox box = rects[i];
jobject boxObj = env->NewObject(skija::paragraph::TextBox::cls, skija::paragraph::TextBox::ctor, box.rect.fLeft, box.rect.fTop, box.rect.fRight, box.rect.fBottom, static_cast<jint>(box.direction));
env->SetObjectArrayElement(rectsArray, i, boxObj);
env->DeleteLocalRef(boxObj);
}
return rectsArray;
}
Expand Down Expand Up @@ -138,6 +140,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_paragraph_Par
size_t endIncludingNewline = conv.from8To16(lm.fEndIncludingNewline);
jobject lmObj = env->NewObject(skija::paragraph::LineMetrics::cls, skija::paragraph::LineMetrics::ctor, startIndex, endIndex, endExcludingWhitespaces, endIncludingNewline, lm.fHardBreak, lm.fAscent, lm.fDescent, lm.fUnscaledAscent, lm.fHeight, lm.fWidth, lm.fLeft, lm.fBaseline, lm.fLineNumber);
env->SetObjectArrayElement(resArray, i, lmObj);
env->DeleteLocalRef(lmObj);
}
return resArray;
}
Expand Down
8 changes: 6 additions & 2 deletions native/src/paragraph/TextStyle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_paragraph_Tex
jobjectArray shadowsArr = env->NewObjectArray((jsize) shadows.size(), skija::paragraph::Shadow::cls, nullptr);
for (int i = 0; i < shadows.size(); ++i) {
const TextShadow& s = shadows[i];
env->SetObjectArrayElement(shadowsArr, i, env->NewObject(skija::paragraph::Shadow::cls, skija::paragraph::Shadow::ctor, s.fColor, s.fOffset.fX, s.fOffset.fY, s.fBlurRadius));
auto shadowObj = env->NewObject(skija::paragraph::Shadow::cls, skija::paragraph::Shadow::ctor, s.fColor, s.fOffset.fX, s.fOffset.fY, s.fBlurRadius);
env->SetObjectArrayElement(shadowsArr, i, shadowObj);
env->DeleteLocalRef(shadowObj);
}
return shadowsArr;
}
Expand All @@ -157,7 +159,9 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_paragraph_Tex
jobjectArray fontFeaturesArr = env->NewObjectArray((jsize) fontFeatures.size(), skija::FontFeature::cls, nullptr);
for (int i = 0; i < fontFeatures.size(); ++i) {
const FontFeature& ff = fontFeatures[i];
env->SetObjectArrayElement(fontFeaturesArr, i, env->NewObject(skija::FontFeature::cls, skija::FontFeature::ctor, javaString(env, ff.fName), ff.fValue));
auto featureObj = env->NewObject(skija::FontFeature::cls, skija::FontFeature::ctor, javaString(env, ff.fName), ff.fValue);
env->SetObjectArrayElement(fontFeaturesArr, i, featureObj);
env->DeleteLocalRef(featureObj);
}
return fontFeaturesArr;
}
Expand Down
11 changes: 11 additions & 0 deletions native/src/svg/SVGCanvas.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <jni.h>
#include "SkSVGCanvas.h"
#include "SkStream.h"

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_svg_SVGCanvas__1nMake
(JNIEnv* env, jclass jclass, jfloat left, jfloat top, jfloat right, jfloat bottom, jlong wstreamPtr, jint flags) {
SkWStream* wstream = reinterpret_cast<SkWStream*>(static_cast<uintptr_t>(wstreamPtr));
SkRect bounds {left, top, right, bottom};
SkCanvas* instance = SkSVGCanvas::Make(bounds, wstream, flags).release();
return reinterpret_cast<jlong>(instance);
}
8 changes: 4 additions & 4 deletions native/src/svg/DOM.cc → native/src/svg/SVGDOM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "SkStream.h"
#include "SkSVGDOM.h"

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_svg_DOM__1nMakeFromData
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_svg_SVGDOM__1nMakeFromData
(JNIEnv* env, jclass jclass, jlong dataPtr) {
SkData* data = reinterpret_cast<SkData*>(static_cast<uintptr_t>(dataPtr));

Expand All @@ -14,20 +14,20 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_svg_DOM__1nMakeFromD
return reinterpret_cast<jlong>(instance.release());
}

extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_svg_DOM__1nGetContainerSize
extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_svg_SVGDOM__1nGetContainerSize
(JNIEnv* env, jclass jclass, jlong ptr) {
SkSVGDOM* instance = reinterpret_cast<SkSVGDOM*>(static_cast<uintptr_t>(ptr));
const SkSize& size = instance->containerSize();
return skija::Point::make(env, size.fWidth, size.fHeight);
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_svg_DOM__1nSetContainerSize
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_svg_SVGDOM__1nSetContainerSize
(JNIEnv* env, jclass jclass, jlong ptr, jfloat width, jfloat height) {
SkSVGDOM* instance = reinterpret_cast<SkSVGDOM*>(static_cast<uintptr_t>(ptr));
instance->setContainerSize(SkSize{width, height});
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_svg_DOM__1nRender
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_svg_SVGDOM__1nRender
(JNIEnv* env, jclass jclass, jlong ptr, jlong canvasPtr) {
SkSVGDOM* instance = reinterpret_cast<SkSVGDOM*>(static_cast<uintptr_t>(ptr));
SkCanvas* canvas = reinterpret_cast<SkCanvas*>(static_cast<uintptr_t>(canvasPtr));
Expand Down
1 change: 0 additions & 1 deletion script/clean.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env python3

import os, sys
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '..')))
import native.script.clean as native_clean
Expand Down
6 changes: 4 additions & 2 deletions shared/script/clean.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#! /usr/bin/env python3

import os, shutil, sys
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..')))
import script.common as common

def main():
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
common.pushd(os.path.join(os.path.dirname(__file__), os.pardir))
shutil.rmtree('target', ignore_errors = True)
shutil.rmtree("../docs/apidocs", ignore_errors = True)
shutil.rmtree(os.path.join(os.path.expanduser('~'), '.m2', 'repository', 'org', 'jetbrains', 'skija', 'skija-shared', '0.0.0-SNAPSHOT'), ignore_errors = True)
common.popd()
return 0

if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions shared/script/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def main():
'--class-path', common.classpath_separator.join(classpath + ['target/test-classes']),
'-ea',
'-esa',
'-Xcheck:jni',
'org.jetbrains.skija.TestSuite',
])

Expand Down
Loading

0 comments on commit 0c35089

Please sign in to comment.