Skip to content

Commit 0c35089

Browse files
committed
DOM -> SVGDOM, SVGCanvas, DeleteLocalRefs in *.cc (closes #93)
1 parent d751941 commit 0c35089

24 files changed

Lines changed: 329 additions & 26 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ ImageFilters ▓▓▓▓▓▓▓▓▓▓ Surface ▓
110110
ImageInfo ▓▓▓▓▓▓▓▓▓▓ TextBlob ▓▓▓▓▓▓▓▓▓▓
111111
MaskFilter ▓▓▓▓▓▓▓▓▓▓ TextBlobBuilder ▓▓▓▓▓▓▓▓▓▓
112112
Matrix33 ▓▓▓░░░░░░░ Typeface ▓▓▓▓▓▓▓▓░░
113-
Matrix44 ▓▓▓░░░░░░░
113+
Matrix44 ▓▓▓░░░░░░░ WStream ▓▓░░░░░░░░
114114
115115
Shaper: Paragraph:
116116
@@ -128,7 +128,9 @@ TextBlobBldRunHndlr ▓▓▓▓▓▓▓▓▓▓
128128
129129
SVG:
130130
131-
DOM ▓▓▓▓▓▓▓▓░░
131+
SVGDOM ▓▓▓▓▓▓▓▓░░
132+
SVGCanvas ▓▓▓▓▓▓▓▓▓▓
133+
132134
```
133135

134136
## Using Skija

examples/scenes/src/SVGScalingScene.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int
2323
canvas.drawString(img, 0, -20, inter13, fill);
2424

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

41-
public void drawIcon(Canvas canvas, Point size, DOM svg) {
41+
public void drawIcon(Canvas canvas, Point size, SVGDOM svg) {
4242
svg.setContainerSize(size);
4343
svg.render(canvas);
4444
canvas.drawRect(Rect.makeWH(size.getX(), size.getY()), border);

examples/scenes/src/SVGScene.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class SVGScene extends Scene {
1212
public Thread _thread = null;
13-
public DOM _dom = null;
13+
public SVGDOM _dom = null;
1414
public Throwable _error = null;
1515

1616
@SneakyThrows
@@ -55,7 +55,7 @@ public void run() {
5555
in.transferTo(out);
5656
if (_thread == this) {
5757
try (var data = org.jetbrains.skija.Data.makeFromBytes(out.toByteArray());) {
58-
_dom = new DOM(data);
58+
_dom = new SVGDOM(data);
5959
}
6060
}
6161
} catch (Exception e) {

native/cmake/FindSkia.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ find_path(SKIA_SKSG_INCLUDE_DIR SkSGInvalidationController.h HINTS "${SKIA_DIR}/
7070
find_path(SKIA_CONFIG_INCLUDE_DIR SkUserConfig.h HINTS "${SKIA_DIR}/include/config")
7171
find_path(SKIA_CORE_INCLUDE_DIR SkCanvas.h HINTS "${SKIA_DIR}/include/core")
7272
find_path(SKIA_PATHOPS_INCLUDE_DIR SkPathOps.h HINTS "${SKIA_DIR}/include/pathops")
73+
find_path(SKIA_CORE_SVG_INCLUDE_DIR SkSVGCanvas.h HINTS "${SKIA_DIR}/include/svg")
7374
find_path(SKIA_UTILS_INCLUDE_DIR SkRandom.h HINTS "${SKIA_DIR}/include/utils")
7475
find_path(SKIA_CODEC_INCLUDE_DIR SkCodec.h HINTS "${SKIA_DIR}/include/codec")
7576
find_path(SKIA_EFFECTS_INCLUDE_DIR SkImageSource.h HINTS "${SKIA_DIR}/include/effects")
@@ -92,7 +93,7 @@ target_include_directories(skia INTERFACE
9293
${SKIA_CONFIG_INCLUDE_DIR}
9394
${SKIA_CORE_INCLUDE_DIR}
9495
${SKIA_PATHOPS_INCLUDE_DIR}
95-
${SKIA_PORTS_INCLUDE_DIR}
96+
${SKIA_CORE_SVG_INCLUDE_DIR}
9697
${SKIA_UTILS_INCLUDE_DIR}
9798
${SKIA_CODEC_INCLUDE_DIR}
9899
${SKIA_EFFECTS_INCLUDE_DIR}

native/script/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def main():
3939
"--config", build_type,
4040
"-DSKIA_DIR=" + skia_dir,
4141
".."],
42-
cwd="build")
42+
cwd=os.path.abspath('build'))
4343

4444
# Ninja
45-
common.check_call(["ninja"], cwd="build")
45+
common.check_call(["ninja"], cwd=os.path.abspath('build'))
4646

4747
# icudtl
4848
icudtl_src = skia_dir + "/out/" + build_type + "-" + common.machine + "/icudtl.dat"

native/script/clean.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#! /usr/bin/env python3
2-
32
import os, shutil, sys
3+
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..')))
4+
import script.common as common
45

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

1113
if __name__ == '__main__':

native/src/Font.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ extern "C" JNIEXPORT jobjectArray JNICALL Java_org_jetbrains_skija_Font__1nGetPa
352352
orig->transform(mx, path);
353353
jobject pathObj = ctx->env->NewObject(skija::Path::cls, skija::Path::ctor, reinterpret_cast<jlong>(path));
354354
ctx->env->SetObjectArrayElement(ctx->paths, ctx->idx, pathObj);
355+
ctx->env->DeleteLocalRef(pathObj);
355356
++ctx->idx;
356357
}
357358
}, &ctx);

native/src/OutputWStream.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <jni.h>
3+
#include "interop.hh"
4+
#include "SkStream.h"
5+
6+
class OutputWStream: public SkWStream {
7+
public:
8+
OutputWStream(JNIEnv* env, jobject out): fEnv(env) {
9+
fOut = env->NewGlobalRef(out);
10+
}
11+
12+
~OutputWStream() override {
13+
fEnv->DeleteGlobalRef(fOut);
14+
}
15+
16+
size_t bytesWritten() const override {
17+
return fBytesWritten;
18+
}
19+
20+
bool write(const void* buffer, size_t size) override {
21+
jbyteArray arr = fEnv->NewByteArray((jsize) size);
22+
fEnv->SetByteArrayRegion(arr, 0, (jsize) size, static_cast<const jbyte *>(buffer));
23+
fEnv->CallVoidMethod(fOut, java::io::OutputStream::write, arr, 0, (jint) size);
24+
fEnv->DeleteLocalRef(arr);
25+
if (java::lang::Throwable::exceptionThrown(fEnv)) {
26+
return false;
27+
} else {
28+
fBytesWritten += size;
29+
return true;
30+
}
31+
}
32+
33+
void flush() override {
34+
fEnv->CallVoidMethod(fOut, java::io::OutputStream::flush);
35+
}
36+
37+
private:
38+
JNIEnv* fEnv;
39+
jobject fOut;
40+
size_t fBytesWritten = 0;
41+
};
42+
43+
static void deleteOutputWStream(OutputWStream* out) {
44+
delete out;
45+
}
46+
47+
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_OutputWStream__1nGetFinalizer
48+
(JNIEnv* env, jclass jclass) {
49+
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deleteOutputWStream));
50+
}
51+
52+
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_OutputWStream__1nMake
53+
(JNIEnv* env, jclass jclass, jobject outputStream) {
54+
OutputWStream* instance = new OutputWStream(env, outputStream);
55+
return reinterpret_cast<jlong>(instance);
56+
}

native/src/interop.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
#include "paragraph/interop.hh"
1010

1111
namespace java {
12+
namespace io {
13+
namespace OutputStream {
14+
jclass cls;
15+
jmethodID write;
16+
jmethodID flush;
17+
18+
void onLoad(JNIEnv* env) {
19+
jclass cls = env->FindClass("java/io/OutputStream");
20+
write = env->GetMethodID(cls, "write", "([BII)V");
21+
flush = env->GetMethodID(cls, "flush", "()V");
22+
}
23+
}
24+
}
25+
1226
namespace lang {
1327
namespace Float {
1428
jclass cls;
@@ -37,6 +51,25 @@ namespace java {
3751
env->DeleteGlobalRef(cls);
3852
}
3953
}
54+
55+
namespace Throwable {
56+
jmethodID printStackTrace;
57+
58+
void onLoad(JNIEnv* env) {
59+
jclass cls = env->FindClass("java/lang/Throwable");
60+
printStackTrace = env->GetMethodID(cls, "printStackTrace", "()V");
61+
}
62+
63+
bool exceptionThrown(JNIEnv* env) {
64+
if (env->ExceptionCheck()) {
65+
jthrowable th = env->ExceptionOccurred();
66+
env->CallVoidMethod(th, printStackTrace);
67+
env->ExceptionCheck(); // ignore
68+
return true;
69+
} else
70+
return false;
71+
}
72+
}
4073
}
4174

4275
namespace util {
@@ -76,8 +109,10 @@ namespace java {
76109
}
77110

78111
void onLoad(JNIEnv* env) {
112+
io::OutputStream::onLoad(env);
79113
lang::Float::onLoad(env);
80114
lang::String::onLoad(env);
115+
lang::Throwable::onLoad(env);
81116
util::Iterator::onLoad(env);
82117
util::function::BooleanSupplier::onLoad(env);
83118
}
@@ -169,6 +204,7 @@ namespace skija {
169204
static_cast<uint32_t>(env->GetIntField(featureObj, skija::FontFeature::value)),
170205
static_cast<size_t>(env->GetLongField(featureObj, skija::FontFeature::start)),
171206
static_cast<size_t>(env->GetLongField(featureObj, skija::FontFeature::end))};
207+
env->DeleteLocalRef(featureObj);
172208
}
173209
return features;
174210
}
@@ -889,6 +925,7 @@ std::vector<SkString> skStringVector(JNIEnv* env, jobjectArray arr) {
889925
for (jint i = 0; i < len; ++i) {
890926
jstring str = static_cast<jstring>(env->GetObjectArrayElement(arr, i));
891927
res[i] = skString(env, str);
928+
env->DeleteLocalRef(str);
892929
}
893930
return res;
894931
}

native/src/interop.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
#include "SkSurfaceProps.h"
1717

1818
namespace java {
19+
namespace io {
20+
namespace OutputStream {
21+
extern jmethodID write;
22+
extern jmethodID flush;
23+
void onLoad(JNIEnv* env);
24+
}
25+
}
26+
1927
namespace lang {
2028
namespace Float {
2129
extern jclass cls;
@@ -29,6 +37,12 @@ namespace java {
2937
void onLoad(JNIEnv* env);
3038
void onUnload(JNIEnv* env);
3139
}
40+
41+
namespace Throwable {
42+
extern jmethodID printStackTrace;
43+
void onLoad(JNIEnv* env);
44+
bool exceptionThrown(JNIEnv* env);
45+
}
3246
}
3347

3448
namespace util {

0 commit comments

Comments
 (0)