Skip to content

Commit

Permalink
Added skresources module and ResourceProviders (closes #129)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Sep 18, 2021
1 parent 8057d11 commit e265802
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 6 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 0.93.3 - Sep 18, 2021

Added:

- org.jetbrains.skija.resources.*
- ResourceProvider
- FileResourceProvider
- DataURIResourceProviderProxy
- CachingResourceProvider
- AnimationBuilder::setResourceProvider #129

# 0.93.1 - Aug 11, 2021

Added:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ cd skija
./script/build.py
```

To codesign:

```sh
security find-identity
export APPLE_CODESIGN_IDENTITY="<...>"
./script/build.py
```

For building Skia itself, see https://github.com/JetBrains/skia-build/

## Running examples
Expand Down
1 change: 1 addition & 0 deletions examples/scenes/animations/Nyan Cat.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/scenes/src/Scenes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Scenes {
public static TreeMap<String, Scene> scenes;
public static String currentScene = "Run Handler";
public static String currentScene = "Skottie";
public static HUD hud = new HUD();
public static boolean stats = true;

Expand Down
7 changes: 6 additions & 1 deletion examples/scenes/src/SkottieScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.*;
import lombok.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.resources.*;
import org.jetbrains.skija.sksg.*;
import org.jetbrains.skija.skottie.*;

Expand All @@ -26,6 +27,7 @@ public void log(LogLevel level, String message, String json) {
@SneakyThrows
public SkottieScene() {
_variants = Files.list(Path.of(file("animations"))).map(Path::getFileName).map(Path::toString).sorted().toArray(String[]::new);
_variantIdx = 7;
}

@Override
Expand All @@ -37,9 +39,12 @@ public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int
error = null;

try {
var dir = file("animations");
var resourceProvider = CachingResourceProvider.make(DataURIResourceProviderProxy.make(FileResourceProvider.make(dir, false), false));
animation = new AnimationBuilder(AnimationBuilderFlag.DEFER_IMAGE_LOADING, AnimationBuilderFlag.PREFER_EMBEDDED_FONTS)
.setLogger(logger)
.buildFromFile(file("animations/" + _variants[_variantIdx]));
.setResourceProvider(resourceProvider)
.buildFromFile(dir + "/" + _variants[_variantIdx]);
} catch (IllegalArgumentException e) {
}
animationVariant = _variants[_variantIdx];
Expand Down
5 changes: 3 additions & 2 deletions platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ file(GLOB IMPL_SOURCES ${CMAKE_SOURCE_DIR}/cc/impl/*.cc)
file(GLOB PARAGRAPH_SOURCES ${CMAKE_SOURCE_DIR}/cc/paragraph/*.cc)
file(GLOB SHAPER_SOURCES ${CMAKE_SOURCE_DIR}/cc/shaper/*.cc)
file(GLOB SVG_SOURCES ${CMAKE_SOURCE_DIR}/cc/svg/*.cc)
file(GLOB RESOURCES_SOURCES ${CMAKE_SOURCE_DIR}/cc/resources/*.cc)
file(GLOB SKOTTIE_SOURCES ${CMAKE_SOURCE_DIR}/cc/skottie/*.cc)
file(GLOB SKSG_SOURCES ${CMAKE_SOURCE_DIR}/cc/sksg/*.cc)
add_library(skija SHARED ${SOURCES} ${IMPL_SOURCES} ${PARAGRAPH_SOURCES} ${SHAPER_SOURCES} ${SVG_SOURCES} ${SKOTTIE_SOURCES} ${SKSG_SOURCES})
add_library(skija SHARED ${SOURCES} ${IMPL_SOURCES} ${PARAGRAPH_SOURCES} ${SHAPER_SOURCES} ${SVG_SOURCES} ${RESOURCES_SOURCES} ${SKOTTIE_SOURCES} ${SKSG_SOURCES})

add_definitions(-DFT2_BUILD_LIBRARY
-DFT_CONFIG_MODULES_H=<include/freetype-android/ftmodule.h>
Expand Down Expand Up @@ -79,4 +80,4 @@ endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(FindSkia)
target_link_libraries(skija skottie sksg svg skparagraph skshaper skia ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES})
target_link_libraries(skija skottie sksg svg skparagraph skshaper skresources skia ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES})
10 changes: 10 additions & 0 deletions platform/cc/resources/CachingResourceProvider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <jni.h>
#include "../interop.hh"
#include "SkResources.h"

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_resources_CachingResourceProvider__1nMake
(JNIEnv* env, jclass jclass, jlong resourceProviderPtr) {
skresources::ResourceProvider* resourceProvider = reinterpret_cast<skresources::ResourceProvider*>(static_cast<uintptr_t>(resourceProviderPtr));
auto instance = skresources::CachingResourceProvider::Make(sk_ref_sp(resourceProvider));
return ptrToJlong(instance.release());
}
10 changes: 10 additions & 0 deletions platform/cc/resources/DataURIResourceProviderProxy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <jni.h>
#include "../interop.hh"
#include "SkResources.h"

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_resources_DataURIResourceProviderProxy__1nMake
(JNIEnv* env, jclass jclass, jlong resourceProviderPtr, jboolean predecode) {
skresources::ResourceProvider* resourceProvider = reinterpret_cast<skresources::ResourceProvider*>(static_cast<uintptr_t>(resourceProviderPtr));
auto instance = skresources::DataURIResourceProviderProxy::Make(sk_ref_sp(resourceProvider), predecode);
return ptrToJlong(instance.release());
}
10 changes: 10 additions & 0 deletions platform/cc/resources/FileResourceProvider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <jni.h>
#include "../interop.hh"
#include "SkResources.h"

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_resources_FileResourceProvider__1nMake
(JNIEnv* env, jclass jclass, jstring basePathStr, jboolean predecode) {
SkString basePath = skString(env, basePathStr);
auto instance = skresources::FileResourceProvider::Make(basePath, predecode);
return ptrToJlong(instance.release());
}
9 changes: 9 additions & 0 deletions platform/cc/skottie/AnimationBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "SkStream.h"
#include "Skottie.h"
#include "SkFontMgr.h"
#include "SkResources.h"
#include "src/utils/SkOSPath.h"

using namespace skottie;

Expand Down Expand Up @@ -34,6 +36,13 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_skottie_AnimationBuil
instance->setLogger(logger);
}

extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_skottie_AnimationBuilder__1nSetResourceProvider
(JNIEnv* env, jclass jclass, jlong ptr, jlong resourceProviderPtr) {
Animation::Builder* instance = reinterpret_cast<Animation::Builder*>(static_cast<uintptr_t>(ptr));
sk_sp<ResourceProvider> resourceProvider = sk_ref_sp(reinterpret_cast<ResourceProvider*>(static_cast<uintptr_t>(resourceProviderPtr)));
instance->setResourceProvider(resourceProvider);
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_skottie_AnimationBuilder__1nBuildFromString
(JNIEnv* env, jclass jclass, jlong ptr, jstring dataStr) {
Animation::Builder* instance = reinterpret_cast<Animation::Builder*>(static_cast<uintptr_t>(ptr));
Expand Down
7 changes: 7 additions & 0 deletions platform/cmake/FindSkia.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ add_library(svg INTERFACE)
target_link_libraries(svg INTERFACE ${SKIA_SVG_LIBRARY})
find_path(SKIA_SVG_INCLUDE_DIR SkSVGDOM.h HINTS "${SKIA_DIR}/modules/svg/include")

# SkResources
find_library(SKIA_SKRESOURCES_LIBRARY skresources PATH "${SKIA_LIBRARY_DIR}")
add_library(skresources INTERFACE)
target_link_libraries(skresources INTERFACE ${SKIA_SKRESOURCES_LIBRARY})
find_path(SKIA_SKRESOURCES_INCLUDE_DIR SkResources.h HINTS "${SKIA_DIR}/modules/skresources/include")

# SKOTTIE
find_library(SKIA_SKOTTIE_LIBRARY skottie PATH "${SKIA_LIBRARY_DIR}")
add_library(skottie INTERFACE)
Expand Down Expand Up @@ -115,6 +121,7 @@ target_include_directories(skia INTERFACE
${SKIA_SKSG_INCLUDE_DIR}
${SKIA_DIR}/third_party/externals/icu/source/common
${SKIA_DIR}/third_party/icu
${SKIA_SKRESOURCES_INCLUDE_DIR}
)

if(WIN32)
Expand Down
12 changes: 11 additions & 1 deletion script/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env python3
import argparse, glob, os, sys, zipfile
import argparse, glob, os, subprocess, sys, zipfile
sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__))))
import build_shared, common

Expand Down Expand Up @@ -45,6 +45,16 @@ def main():
# Ninja
common.check_call(["ninja"], cwd=os.path.abspath('build'))

# Codesign
if common.system == "macos" and os.getenv("APPLE_CODESIGN_IDENTITY"):
subprocess.call(["codesign",
# "--force",
# "-vvvvvv",
"--deep",
"--sign",
os.getenv("APPLE_CODESIGN_IDENTITY"),
"build/libskija_" + common.arch + ".dylib"])

# javac
modulepath = []
if args.skija_version:
Expand Down
2 changes: 2 additions & 0 deletions script/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ def replaced(filename, replacements):

def copy_newer(src, dst):
if not os.path.exists(dst) or os.path.getmtime(src) > os.path.getmtime(dst):
if os.path.exists(dst):
os.remove(dst)
shutil.copy2(src, dst)
24 changes: 24 additions & 0 deletions shared/java/resources/CachingResourceProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jetbrains.skija.resources;

import java.lang.ref.*;
import org.jetbrains.annotations.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.impl.*;

public class CachingResourceProvider extends ResourceProvider {
static { Library.staticLoad(); }

@ApiStatus.Internal
public CachingResourceProvider(long ptr) {
super(ptr);
}

@NotNull @Contract("_ -> new")
public static CachingResourceProvider make(@NotNull ResourceProvider resourceProvider) {
assert resourceProvider != null : "Can’t CachingResourceProvider::make with resourceProvider == null";
Stats.onNativeCall();
return new CachingResourceProvider(_nMake(Native.getPtr(resourceProvider)));
}

@ApiStatus.Internal public static native long _nMake(long resourceProviderPtr);
}
29 changes: 29 additions & 0 deletions shared/java/resources/DataURIResourceProviderProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jetbrains.skija.resources;

import java.lang.ref.*;
import org.jetbrains.annotations.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.impl.*;

public class DataURIResourceProviderProxy extends ResourceProvider {
static { Library.staticLoad(); }

@ApiStatus.Internal
public DataURIResourceProviderProxy(long ptr) {
super(ptr);
}

@NotNull @Contract("_ -> new")
public static DataURIResourceProviderProxy make(@NotNull ResourceProvider resourceProvider) {
return make(resourceProvider, false);
}

@NotNull @Contract("_, _ -> new")
public static DataURIResourceProviderProxy make(@NotNull ResourceProvider resourceProvider, boolean predecode) {
assert resourceProvider != null : "Can’t DataURIResourceProviderProxy::make with resourceProvider == null";
Stats.onNativeCall();
return new DataURIResourceProviderProxy(_nMake(Native.getPtr(resourceProvider), predecode));
}

@ApiStatus.Internal public static native long _nMake(long resourceProviderPtr, boolean predecode);
}
29 changes: 29 additions & 0 deletions shared/java/resources/FileResourceProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jetbrains.skija.resources;

import java.lang.ref.*;
import org.jetbrains.annotations.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.impl.*;

public class FileResourceProvider extends ResourceProvider {
static { Library.staticLoad(); }

@ApiStatus.Internal
public FileResourceProvider(long ptr) {
super(ptr);
}

@NotNull @Contract("_ -> new")
public static FileResourceProvider make(@NotNull String baseDir) {
return make(baseDir, false);
}

@NotNull @Contract("_, _ -> new")
public static FileResourceProvider make(@NotNull String baseDir, boolean predecode) {
assert baseDir != null : "Can’t FileResourceProvider::make with baseDir == null";
Stats.onNativeCall();
return new FileResourceProvider(_nMake(baseDir, predecode));
}

@ApiStatus.Internal public static native long _nMake(String baseDir, boolean predecode);
}
11 changes: 11 additions & 0 deletions shared/java/resources/ResourceProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.skija.resources;

import org.jetbrains.annotations.*;
import org.jetbrains.skija.impl.*;

public abstract class ResourceProvider extends RefCnt {
@ApiStatus.Internal
public ResourceProvider(long ptr) {
super(ptr);
}
}
15 changes: 14 additions & 1 deletion shared/java/skottie/AnimationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jetbrains.annotations.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.impl.*;
import org.jetbrains.skija.resources.*;

public class AnimationBuilder extends Managed {
static { Library.staticLoad(); }
Expand Down Expand Up @@ -52,7 +53,7 @@ public AnimationBuilder setFontManager(@Nullable FontMgr fontMgr) {
/**
* <p>Register a {@link Logger} with this builder.</p>
*/
@Contract("_ -> this")
@NotNull @Contract("_ -> this")
public AnimationBuilder setLogger(@Nullable Logger logger) {
try {
Stats.onNativeCall();
Expand All @@ -63,6 +64,17 @@ public AnimationBuilder setLogger(@Nullable Logger logger) {
}
}

@NotNull @Contract("_ -> this")
public AnimationBuilder setResourceProvider(@Nullable ResourceProvider resourceProvider) {
try {
Stats.onNativeCall();
_nSetResourceProvider(_ptr, Native.getPtr(resourceProvider));
return this;
} finally {
Reference.reachabilityFence(resourceProvider);
}
}

@NotNull @Contract("!null -> new; null -> fail")
public Animation buildFromString(@NotNull String data) {
try {
Expand Down Expand Up @@ -109,6 +121,7 @@ public Animation buildFromData(@NotNull Data data) {
@ApiStatus.Internal public static native long _nMake(int flags);
@ApiStatus.Internal public static native void _nSetFontManager(long ptr, long fontMgrPtr);
@ApiStatus.Internal public static native void _nSetLogger(long ptr, long loggerPtr);
@ApiStatus.Internal public static native void _nSetResourceProvider(long ptr, long resourceProviderPtr);
@ApiStatus.Internal public static native long _nBuildFromString(long ptr, String data);
@ApiStatus.Internal public static native long _nBuildFromFile(long ptr, String path);
@ApiStatus.Internal public static native long _nBuildFromData(long ptr, long dataPtr);
Expand Down

0 comments on commit e265802

Please sign in to comment.