Skip to content

Commit

Permalink
Unbundled Image encoders to their own classes, kept backward-compatib…
Browse files Browse the repository at this point in the history
…ility methods
  • Loading branch information
tonsky committed Aug 8, 2023
1 parent 395789c commit a4a6ee8
Show file tree
Hide file tree
Showing 23 changed files with 393 additions and 266 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ jobs:
with:
name: linux-x64-jars
path: target/skija-linux-x64-*
- run: python3 script/clean.py
- run: python3 script/build.py --arch arm64
- run: python3 script/package_platform.py --arch arm64
- uses: actions/upload-artifact@v3
with:
name: linux-arm64-jars
path: target/skija-linux-arm64-*
# Disabled until https://github.com/HumbleUI/SkiaBuild/issues/8
# - run: python3 script/clean.py
# - run: python3 script/build.py --arch arm64
# - run: python3 script/package_platform.py --arch arm64
# - uses: actions/upload-artifact@v3
# with:
# name: linux-arm64-jars
# path: target/skija-linux-arm64-*

build_windows:
runs-on: windows-2019
Expand Down
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# 0.116.0 - Aug 8, 2023

Added:

- ARM64 jars thx @Glavo

Fixed:

- Race condition in shared library unpacking #54 #56 thx @dzaima
Expand All @@ -13,8 +9,13 @@ Changed:

- Skia version m109-664500fa93 -> m116-f44dbc40d8
- [ BREAKING ] `Bitmap::erase(Color4f)` no longer takes ColorSpace
- [ BREAKING ] `Image::encodeToData` has been replaced with `::encodeJPEG`, `::encodePNG`, `::encodeWEBP`, plus the encoding options
- [ BREAKING ] `ImageFilter::MakeMagnifier` required two more arguments: `zoomAmount` and `SamplingMode`
- `Image::makeRaster(ImageInfo, byte[], long)` -> `makeRasterFromBytes`
- `Image::makeRaster(ImageInfo, Data, long)` -> `makeRasterFromData`
- `Image::makeRasterBitmap(Bitmap)` -> `makeRasterFromBitmap`
- `Image::makeRasterPixmap(Pixmap)` -> `makeRasterFromPixmap`
- `Image::makeFromEncoded(byte[])` -> `makeDeferredFromEncodedBytes`
- `Image::encodeToData` has been replaced with `EncoderJPEG`, `EncoderPNG`, `EncoderWEBP`, plus the encoding options

Removed:

Expand Down
13 changes: 13 additions & 0 deletions platform/cc/EncoderJPEG.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <jni.h>
#include "SkData.h"
#include "SkImage.h"
#include "SkJpegEncoder.h"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_EncoderJPEG__1nEncode
(JNIEnv* env, jclass jclass, jlong ctxPtr, jlong imagePtr, jint quality, jint alphaMode, jint downsampleMode) {
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkImage* image = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(imagePtr));
SkJpegEncoder::Options opts {quality, (SkJpegEncoder::Downsample) downsampleMode, (SkJpegEncoder::AlphaOption) alphaMode};
sk_sp<SkData> data = SkJpegEncoder::Encode(ctx, image, opts);
return reinterpret_cast<jlong>(data.release());
}
14 changes: 14 additions & 0 deletions platform/cc/EncoderPNG.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <jni.h>
#include "SkData.h"
#include "SkImage.h"
#include "SkPngEncoder.h"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_EncoderPNG__1nEncode
(JNIEnv* env, jclass jclass, jlong ctxPtr, jlong imagePtr, jint flagsInt, jint zlibLevel) {
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkImage* image = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(imagePtr));
SkPngEncoder::FilterFlag flags = static_cast<SkPngEncoder::FilterFlag>(flagsInt);
SkPngEncoder::Options opts {flags, zlibLevel};
sk_sp<SkData> data = SkPngEncoder::Encode(ctx, image, opts);
return reinterpret_cast<jlong>(data.release());
}
13 changes: 13 additions & 0 deletions platform/cc/EncoderWEBP.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <jni.h>
#include "SkData.h"
#include "SkImage.h"
#include "SkWebpEncoder.h"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_EncoderWEBP__1nEncode
(JNIEnv* env, jclass jclass, jlong ctxPtr, jlong imagePtr, jint compressionMode, jfloat quality) {
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkImage* image = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(imagePtr));
SkWebpEncoder::Options opts {(SkWebpEncoder::Compression) compressionMode, quality};
sk_sp<SkData> data = SkWebpEncoder::Encode(ctx, image, opts);
return reinterpret_cast<jlong>(data.release());
}
41 changes: 5 additions & 36 deletions platform/cc/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#include "SkBitmap.h"
#include "SkData.h"
#include "SkImage.h"
#include "SkJpegEncoder.h"
#include "SkPngEncoder.h"
#include "SkWebpEncoder.h"
#include "SkShader.h"
#include "interop.hh"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRaster
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromBytes
(JNIEnv* env, jclass jclass, jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr, jbyteArray bytesArr, jlong rowBytes) {
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(static_cast<uintptr_t>(colorSpacePtr));
SkImageInfo imageInfo = SkImageInfo::Make(width,
Expand All @@ -23,7 +20,7 @@ extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRa
return reinterpret_cast<jlong>(image.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterData
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromData
(JNIEnv* env, jclass jclass, jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr, jlong dataPtr, jlong rowBytes) {
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(static_cast<uintptr_t>(colorSpacePtr));
SkImageInfo imageInfo = SkImageInfo::Make(width,
Expand All @@ -36,21 +33,21 @@ extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRa
return reinterpret_cast<jlong>(image.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeFromBitmap
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromBitmap
(JNIEnv* env, jclass jclass, jlong bitmapPtr) {
SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(static_cast<uintptr_t>(bitmapPtr));
sk_sp<SkImage> image = SkImages::RasterFromBitmap(*bitmap);
return reinterpret_cast<jlong>(image.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeFromPixmap
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeRasterFromPixmap
(JNIEnv* env, jclass jclass, jlong pixmapPtr) {
SkPixmap* pixmap = reinterpret_cast<SkPixmap*>(static_cast<uintptr_t>(pixmapPtr));
sk_sp<SkImage> image = SkImages::RasterFromPixmap(*pixmap, nullptr, nullptr);
return reinterpret_cast<jlong>(image.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeFromEncoded
extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeDeferredFromEncodedBytes
(JNIEnv* env, jclass jclass, jbyteArray encodedArray) {
jsize encodedLen = env->GetArrayLength(encodedArray);
jbyte* encoded = env->GetByteArrayElements(encodedArray, 0);
Expand All @@ -68,34 +65,6 @@ extern "C" JNIEXPORT jobject JNICALL Java_io_github_humbleui_skija_Image__1nGetI
return skija::ImageInfo::toJava(env, instance->imageInfo());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nEncodePNG
(JNIEnv* env, jclass jclass, jlong ptr, jlong ctxPtr, jint flagsInt, jint zlibLevel) {
SkImage* instance = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(ptr));
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkPngEncoder::FilterFlag flags = static_cast<SkPngEncoder::FilterFlag>(flagsInt);
SkPngEncoder::Options opts {flags, zlibLevel};
sk_sp<SkData> data = SkPngEncoder::Encode(ctx, instance, opts);
return reinterpret_cast<jlong>(data.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nEncodeJPEG
(JNIEnv* env, jclass jclass, jlong ptr, jlong ctxPtr, jint quality, jint alphaMode, jint downsampleMode) {
SkImage* instance = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(ptr));
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkJpegEncoder::Options opts {quality, (SkJpegEncoder::Downsample) downsampleMode, (SkJpegEncoder::AlphaOption) alphaMode};
sk_sp<SkData> data = SkJpegEncoder::Encode(ctx, instance, opts);
return reinterpret_cast<jlong>(data.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nEncodeWEBP
(JNIEnv* env, jclass jclass, jlong ptr, jlong ctxPtr, jint compressionMode, jfloat quality) {
SkImage* instance = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(ptr));
GrDirectContext* ctx = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(ctxPtr));
SkWebpEncoder::Options opts {(SkWebpEncoder::Compression) compressionMode, quality};
sk_sp<SkData> data = SkWebpEncoder::Encode(ctx, instance, opts);
return reinterpret_cast<jlong>(data.release());
}

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Image__1nMakeShader
(JNIEnv* env, jclass jclass, jlong ptr, jint tmx, jint tmy, jlong sampling, jfloatArray localMatrixArr) {
SkImage* instance = reinterpret_cast<SkImage*>(static_cast<uintptr_t>(ptr));
Expand Down
3 changes: 2 additions & 1 deletion platform/cc/Surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "GrDirectContext.h"
#include "SkSurface.h"
#include "ganesh/SkSurfaceGanesh.h"
#include "ganesh/mtl/SkSurfaceMetal.h"
#include "interop.hh"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Surface__1nMakeRasterDirect
Expand Down Expand Up @@ -93,6 +92,8 @@ extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Surface__1nMake
}

#ifdef SK_METAL
#include "ganesh/mtl/SkSurfaceMetal.h"

extern "C" JNIEXPORT jlong JNICALL Java_io_github_humbleui_skija_Surface__1nMakeFromMTKView
(JNIEnv* env, jclass jclass, jlong contextPtr, jlong mtkViewPtr, jint surfaceOrigin, jint sampleCount, jint colorType, jlong colorSpacePtr, jobject surfacePropsObj) {
GrDirectContext* context = reinterpret_cast<GrDirectContext*>(static_cast<uintptr_t>(contextPtr));
Expand Down
2 changes: 1 addition & 1 deletion script/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true')
parser.add_argument('--arch', default=build_utils.arch)
parser.add_argument('--skia-dir') # , default='/Users/tonsky/ws/skia-build/skia')
parser.add_argument('--skia-dir')
parser.add_argument('--skia-release', default='m116-f44dbc40d8')
parser.add_argument('--cmake-toolchain-file')
(args, _) = parser.parse_known_args()
Expand Down
4 changes: 2 additions & 2 deletions script/build_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def fetch_maven(group, name, version, classifier=None, repo='https://repo1.maven
return file

def fetch_all_maven(deps, repo='https://repo1.maven.org/maven2'):
return [fetch_maven(dep['group'], dep['name'], dep['version'], repo=repo) for dep in deps]
return [fetch_maven(dep['group'], dep['name'], dep['version'], repo=dep.get('repo', repo)) for dep in deps]

def javac(sources, target, classpath = [], modulepath = [], add_modules = [], release = '11', opts=[]):
makedirs(target)
Expand Down Expand Up @@ -125,7 +125,7 @@ def jar(target: str, *content: List[Tuple[str, str]], opts=[]) -> str:

@functools.lru_cache(maxsize=1)
def lombok():
return fetch_maven('org.projectlombok', 'lombok', '1.18.26')
return fetch_maven('org.projectlombok', 'lombok', '1.18.28')

def delombok(dirs: List[str], target: str, classpath: List[str] = [], modulepath: List[str] = []):
sources = files(*[dir + "/**/*.java" for dir in dirs])
Expand Down
2 changes: 1 addition & 1 deletion script/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

compile_deps = [
{'group': 'org.jetbrains', 'name': 'annotations', 'version': '20.1.0'},
{'group': 'org.projectlombok', 'name': 'lombok', 'version': '1.18.26'},
{'group': 'org.projectlombok', 'name': 'lombok', 'version': '1.18.28'},
]

@functools.lru_cache(maxsize=1)
Expand Down
6 changes: 6 additions & 0 deletions shared/java/EncodeJPEGAlphaMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.humbleui.skija;

public enum EncodeJPEGAlphaMode {
IGNORE,
BLEND_ON_BLACK
}
18 changes: 18 additions & 0 deletions shared/java/EncodeJPEGDownsampleMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.humbleui.skija;

public enum EncodeJPEGDownsampleMode {
/**
* Reduction by a factor of two in both the horizontal and vertical directions.
*/
DS_420,

/**
* Reduction by a factor of two in the horizontal direction.
*/
DS_422,

/**
* No downsampling.
*/
DS_444
}
28 changes: 3 additions & 25 deletions shared/java/EncodeJPEGOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,14 @@

@lombok.Data @With
public class EncodeJPEGOptions {
public enum DownsampleMode {
/**
* Reduction by a factor of two in both the horizontal and vertical directions.
*/
DS_420,

/**
* Reduction by a factor of two in the horizontal direction.
*/
DS_422,

/**
* No downsampling.
*/
DS_444
}

public enum AlphaMode {
IGNORE,
BLEND_ON_BLACK
}

public static final EncodeJPEGOptions DEFAULT = new EncodeJPEGOptions(100, DownsampleMode.DS_420, AlphaMode.IGNORE);
public static final EncodeJPEGOptions DEFAULT = new EncodeJPEGOptions(100, EncodeJPEGDownsampleMode.DS_420, EncodeJPEGAlphaMode.IGNORE);

@ApiStatus.Internal
public final int _quality;

@ApiStatus.Internal
public final DownsampleMode _downsampleMode;
public final EncodeJPEGDownsampleMode _downsampleMode;

@ApiStatus.Internal
public final AlphaMode _alphaMode;
public final EncodeJPEGAlphaMode _alphaMode;
}
19 changes: 19 additions & 0 deletions shared/java/EncodePNGFilterFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.humbleui.skija;

import org.jetbrains.annotations.*;

public enum EncodePNGFilterFlag {
ZERO (0x00),
NONE (0x08),
SUB (0x10),
UP (0x20),
AVG (0x40),
PAETH (0x80),
ALL (0x08 | 0x10 | 0x20 | 0x40 | 0x80);

@ApiStatus.Internal public final int _value;

EncodePNGFilterFlag(int value) {
this._value = value;
}
};
22 changes: 3 additions & 19 deletions shared/java/EncodePNGOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,7 @@
import org.jetbrains.annotations.*;

public class EncodePNGOptions {
public enum FilterFlag {
ZERO (0x00),
NONE (0x08),
SUB (0x10),
UP (0x20),
AVG (0x40),
PAETH (0x80),
ALL (0x08 | 0x10 | 0x20 | 0x40 | 0x80);

@ApiStatus.Internal public final int _value;

FilterFlag(int value) {
this._value = value;
}
};

public static final EncodePNGOptions DEFAULT = new EncodePNGOptions(FilterFlag.ALL._value, 6);
public static final EncodePNGOptions DEFAULT = new EncodePNGOptions(EncodePNGFilterFlag.ALL._value, 6);

@ApiStatus.Internal
public final int _flags;
Expand All @@ -33,9 +17,9 @@ public EncodePNGOptions(int flags, int level) {
_zlibLevel = level;
}

public EncodePNGOptions withFlags(FilterFlag... flags) {
public EncodePNGOptions withFlags(EncodePNGFilterFlag... flags) {
int _flags = 0;
for (FilterFlag flag: flags) {
for (EncodePNGFilterFlag flag: flags) {
_flags = _flags | flag._value;
}
return new EncodePNGOptions(_flags, _zlibLevel);
Expand Down
8 changes: 8 additions & 0 deletions shared/java/EncodeWEBPCompressionMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.humbleui.skija;

import org.jetbrains.annotations.*;

public enum EncodeWEBPCompressionMode {
LOSSY,
LOSSLESS
}
11 changes: 3 additions & 8 deletions shared/java/EncodeWEBPOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@

@lombok.Data @With
public class EncodeWEBPOptions {
public enum CompressionMode {
LOSSY,
LOSSLESS
}

public static final EncodeWEBPOptions DEFAULT = new EncodeWEBPOptions(CompressionMode.LOSSY, 100f);
public static final EncodeWEBPOptions DEFAULT = new EncodeWEBPOptions(EncodeWEBPCompressionMode.LOSSY, 100f);

@ApiStatus.Internal
public final CompressionMode _compressionMode;
public final EncodeWEBPCompressionMode _compressionMode;

@ApiStatus.Internal
public final float _quality;

@ApiStatus.Internal
public EncodeWEBPOptions(CompressionMode compression, float quality) {
public EncodeWEBPOptions(EncodeWEBPCompressionMode compression, float quality) {
_compressionMode = compression;
_quality = quality;
}
Expand Down
4 changes: 4 additions & 0 deletions shared/java/EncodedImageFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.jetbrains.annotations.*;

/**
* @deprecated - use {@link EncoderJPEG}, {@link EncoderPNG} or {@link EncoderWEBP} directly
*/
@Deprecated
public enum EncodedImageFormat {
BMP,
GIF,
Expand Down
Loading

0 comments on commit a4a6ee8

Please sign in to comment.