Skija: Java bindings for Skia
Skia is an open source 2D graphics library which provides common APIs that work across a variety of hardware and software platforms.
Skija is a high-quality Java bindings for Skia.
For a supported version of Skija checkout HumbleUI/Skija
Motivation: Why Skija?
Because Java doesnβt have a powerful and modern 2D graphics β still!
There are many occasions when you might need graphics:
- custom UI widget libraries and whole toolkits,
- graphs, diagrams,
- visualizations,
- games.
Existing offerings are: Graphics2D from AWT, GraphicsContext from JavaFX. They are good, but underwhelming.
Enter Skia. Skia has a proven track record of industrial-scale project relying on it for all things graphics: Google Chrome, Android, Flutter, Firefox Canvas, Xamarin, LibreOffice. Skia outperforms all existing Java2D toolkits in almost every benchmark, and also provides:
- extensive color spaces support,
- modern typography with open type features, variable typefaces, correct multi-script text handling, emojis,
- highly-optimized GPU rendering,
- modern GPU backends, including Vulkan and Metal (already in Skia, coming to Skija soon),
- built-in caching and compositing facilities.
Skija brings all this power to the tips of your fingers, as simple as adding a jar to classpath.
Why hand-crafted bindings?
Automatically generated bindings for Skia exist, but donβt seem to have high adoption:
Skija project has a goal of providing great Java-native API that is natural to use. In particular:
- full automatic memory management, no pointer abstractions leaking,
- natural use of Java classes, interfaces, inheritance, singletons,
- consistent naming following Java conventions, including getters/setters for properties,
- typed enums instead of integer constants,
- native Java platform abstractions instead of wrapped Skia/C++ ones (strings, arrays, streams, files, byte buffers, AutoCloseable),
- hiding implementation details, e.g. transparent string encoding conversion, byte/code point indices conversion,
- fluent builder-style APIs where possible,
- lightweight data classes where possible (Point, Rect, FontMetrics, etc are not mirrored by native instances).
The ultimate goal for Skija is to feel as a Java library and not having to think about native part at all.
Built with Skija
Compose for Desktop, declarative UI toolkit for Kotlin:
Skija Graphics2D, an implementation of Java2D API:
Robert Felker demos:
Harold videos:
Current status
Public alpha. Things might change without notice.
Please note that Skia is known to change its API quite often (monthly). Skija will do its best to protect from these changes, but sometimes it might be impossible (e.g. method was removed). Normally itβs auxiliary stuff though, donβt worry too much.
Platforms:
- Windows
- Linux
- macOS
Backends:
- Bitmap
- OpenGL
- Direct3D
- Metal
- Vulkan
APIs:
Bitmap ββββββββββ Paint ββββββββββ
Canvas ββββββββββ Path ββββββββββ
Codec ββββββββββ PathEffects ββββββββββ
Color ββββββββββ PathMeasure ββββββββββ
ColorFilter ββββββββββ PaintFilterCanvas ββββββββββ
ColorInfo ββββββββββ Picture ββββββββββ
ColorSpace ββββββββββ PictureRecorder ββββββββββ
Data ββββββββββ PixelRef ββββββββββ
Drawable ββββββββββ Pixmap ββββββββββ
Flattenable ββββββββββ Region ββββββββββ
Font ββββββββββ RuntimeEffect ββββββββββ
FontData ββββββββββ ScalerContext ββββββββββ
FontManager ββββββββββ Shader ββββββββββ
FontStyle ββββββββββ ShadowUtils ββββββββββ
FontStyleSet ββββββββββ Stream ββββββββββ
Image ββββββββββ String ββββββββββ
ImageFilters ββββββββββ Surface ββββββββββ
ImageInfo ββββββββββ TextBlob ββββββββββ
MaskFilter ββββββββββ TextBlobBuilder ββββββββββ
Matrix33 ββββββββββ Typeface ββββββββββ
Matrix44 ββββββββββ WStream ββββββββββ
Shaper: Paragraph:
BiDiRunIterator ββββββββββ FontCollection ββββββββββ
FontMgrRunIterator ββββββββββ LineMetrics ββββββββββ
FontRunIterator ββββββββββ Paragraph ββββββββββ
HbIcuScriptRunIter ββββββββββ ParagraphCache ββββββββββ
IcuBidiRunIterator ββββββββββ ParagraphStyle ββββββββββ
LanguageRunIterator ββββββββββ ParagraphBuilder ββββββββββ
RunHandler ββββββββββ TextStyle ββββββββββ
RunInfo ββββββββββ TypefaceFontProvider ββββββββββ
ScriptRunIterator ββββββββββ
Shaper ββββββββββ
TextBlobBldRunHndlr ββββββββββ
SVG:
SVGDOM ββββββββββ
SVGCanvas ββββββββββ
SVGSVG ββββββββββ
Using Skija
Using Skija is as simple as adding a jar file.
Maven:
<repositories>
<repository>
<id>space-maven</id>
<url>https://packages.jetbrains.team/maven/p/skija/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jetbrains.skija</groupId>
<artifactId>skija-${platform}</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
Gradle:
repositories {
maven {
url "https://packages.jetbrains.team/maven/p/skija/maven"
}
}
dependencies {
api "org.jetbrains.skija:${artifact}:${version}"
}
Replace ${artifact}
and ${version}
with:
Platform | ${artifact} |
${version} |
---|---|---|
macOS Intel | skija-macos-x64 |
|
macOS M1 | skija-macos-arm64 |
|
Linux | skija-linux |
|
Windows | skija-windows |
Documentation
Get started by reading Getting Started.
API docs are under development β consult source code and JavaDoc comments.
I found SkiaSharp documentation to be excellent resource on what can be done in Skia. They have nice examples and visual explanations, too.
If Skija is missing a documentation for a particular method or class, check the same class in Skia Documentation instead. It might be that we didnβt move it over to Java yet. PRs are welcome!
Finally, LWJGL demo app has examples of most of the APIs that are currently implemented.
Resources
- Overview youtube.com/watch?v=t1X-Oln1u24
- Coding game in Skija and Clojure youtube.com/watch?v=KES-lKTq-3M
- SWT and Skija on Raspberry Pi spket.com/blog/native-gui-app-with-swt-and-skija-on-raspberry-pi
Building Skija from scratch
Prerequisites: Git, CMake, Ninja, JDK 9+, $JAVA_HOME, Python 3.
Checkout:
git clone https://github.com/JetBrains/skija.git
cd skija
./script/build.py
To codesign:
security find-identity
export APPLE_CODESIGN_IDENTITY="<...>"
./script/build.py
For building Skia itself, see https://github.com/JetBrains/skia-build/
Running examples
Examples require local build of Skija (see Building Skija).
See examples/.
Contributing
Contributions are welcome!
Make sure to read Code Conventions.
Remember: the goal of Skija is to map Skia API as close as possible.
These things have a place in Skija:
- Everything that is in Skia and makes sense in Java world.
- Convenience methods (e.g. a method that uses a default value for a parameter).
- Documentation on Skija methods is very welcome!
These donβt:
- Things that donβt directly map to anything in Java (pointers, etc).
- New classes, new methods that donβt exist in Skia.
- Code that combines Skia APIs together for special use-cases.
Useful things built on top of Skia/Skija are always welcome β as libraries.