Skip to content

Commit

Permalink
chore: fixes for c-api build issues (#239)
Browse files Browse the repository at this point in the history
* chore: fixes for c-api build issues

---------

Co-authored-by: Muhmud Ahmad <[email protected]>
  • Loading branch information
muhmud and Muhmud Ahmad authored Oct 4, 2024
1 parent 453e0e0 commit 964931e
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 17 deletions.
24 changes: 24 additions & 0 deletions Makefile.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RUNTIME_FFI := dotlottie-ffi

RELEASE := release
NATIVE := native

DOTLOTTIE_PLAYER_NATIVE_RELEASE_DIR := $(RELEASE)\$(NATIVE)\dotlottie-player
DOTLOTTIE_PLAYER_NATIVE_RELEASE_INCLUDE_DIR := $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_DIR)\include
DOTLOTTIE_PLAYER_NATIVE_RELEASE_LIB_DIR := $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_DIR)\lib

RUNTIME_FFI_HEADER := dotlottie_player.h

define NATIVE_RELEASE
if exist "$(RELEASE)\$(NATIVE)" rmdir /s /q $(RELEASE)\$(NATIVE)
mkdir $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_INCLUDE_DIR)
mkdir $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_LIB_DIR)
copy $(RUNTIME_FFI)\bindings.h $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_INCLUDE_DIR)\$(RUNTIME_FFI_HEADER)
copy $(RUNTIME_FFI)\target\release\dotlottie_player.dll $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_LIB_DIR)\dotlottie_player.dll
copy $(RUNTIME_FFI)\target\release\dotlottie_player.lib $(DOTLOTTIE_PLAYER_NATIVE_RELEASE_LIB_DIR)\dotlottie_player.lib
cd $(RELEASE)\$(NATIVE) && tar.exe -a -c -f "dotlottie_player.native.zip" dotlottie-player
endef

native:
cargo build --manifest-path $(RUNTIME_FFI)/Cargo.toml --release
$(NATIVE_RELEASE)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ files in `release/native`:
make native
```

On Windows, you should use the `Makefile.win` make file:

```bash
make -f Makefile.win native
```

Examples for using the native interface can be found in the `examples` directory, which also contains a
[README](./examples/README.md) with information on getting started.

Expand Down
2 changes: 2 additions & 0 deletions dotlottie-ffi/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ int32_t dotlottie_config(struct DotLottiePlayer *ptr, struct DotLottieConfig *re

int32_t dotlottie_current_frame(struct DotLottiePlayer *ptr, float *result);

int32_t dotlottie_destroy(struct DotLottiePlayer *ptr);

int32_t dotlottie_duration(struct DotLottiePlayer *ptr, float *result);

int32_t dotlottie_init_config(struct DotLottieConfig *config);
Expand Down
8 changes: 8 additions & 0 deletions dotlottie-ffi/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub unsafe extern "C" fn dotlottie_new_player(ptr: *const DotLottieConfig) -> *m
std::ptr::null_mut()
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_destroy(ptr: *mut DotLottiePlayer) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
std::mem::drop(std::ptr::read(dotlottie_player));
DOTLOTTIE_SUCCESS
})
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_init_config(config: *mut DotLottieConfig) -> i32 {
if config.is_null() {
Expand Down
22 changes: 20 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
### Running the examples

In order to try out the `demo-player-c` and `skia-demo-player-cpp` examples, you must first ensure that
`dotlottie-ffi` has been built, which can be done by executing `cargo build` in the `dotlottie-ffi`
directory.
`dotlottie-ffi` has been built, which can be done by executing `cargo build --release` in the `dotlottie-ffi`
directory, or by running `make native` from the root of the repo.

You can then build each of the demos by running `make` in their respective directories. You will
need the `SDL2` libraries and headers available on your system to perform the build. On Windows, you should
use a WSL2 environment, or build the sources manually.

For the Skia demo, you should run `make skia-build` in the `skia-demo-player-cpp` directory first.

On a Mac, the SDL2 dependencies can be installed through `brew`:

```Bash
brew install sdl2 sdl2_image
```

You can then run the examples using a command such as the following:

```Bash
Expand Down Expand Up @@ -56,5 +64,15 @@ if (ret != DOTLOTTIE_SUCCESS) {
}
```

Finally, the player can be destroyed as follows:

```C
ret = dotlottie_destroy(player);
if (ret != DOTLOTTIE_SUCCESS) {
fprintf(stderr, "Could not destroy dotlottie player\n");
return 1;
}
```

Every API call will return `DOTLOTTIE_SUCCESS` on success, and some other value on error. The set of
possible error return values can be found in the `dotlottie_player.h` header file.
2 changes: 2 additions & 0 deletions examples/demo-player-c/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
demo-player
demo-player.dSYM/

7 changes: 5 additions & 2 deletions examples/demo-player-c/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
.PHONY: all run debug

DOTLOTTIE_PLAYER_LIB_PATH=../../dotlottie-ffi/target/debug
DOTLOTTIE_PLAYER_LIB_PATH=../../dotlottie-ffi/target/release
DOTLOTTIE_PLAYER_LIB=dotlottie_player

SDL2_CFLAGS=$(shell sdl2-config --cflags)
SDL2_LIBS=$(shell sdl2-config --libs)

all: demo-player

demo-player: main.c
$(CC) -g main.c -L$(DOTLOTTIE_PLAYER_LIB_PATH) -lSDL2 -l$(DOTLOTTIE_PLAYER_LIB) -o $@
$(CC) -g main.c $(SDL2_CFLAGS) $(SDL2_LIBS) -L$(DOTLOTTIE_PLAYER_LIB_PATH) -l$(DOTLOTTIE_PLAYER_LIB) -o $@

run: export LD_LIBRARY_PATH = $(DOTLOTTIE_PLAYER_LIB_PATH)
run: demo-player
Expand Down
6 changes: 3 additions & 3 deletions examples/demo-player-c/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_pixels.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_pixels.h>
#include <libgen.h> // For dirname
#include <limits.h> // For PATH_MAX
#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions examples/skia-demo-player-cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
demo-player
skia
demo-player.dSYM
22 changes: 17 additions & 5 deletions examples/skia-demo-player-cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
.PHONY: all run debug clean

DOTLOTTIE_PLAYER_LIB_PATH=../../dotlottie-ffi/target/debug
DOTLOTTIE_PLAYER_LIB_PATH=../../dotlottie-ffi/target/release
DOTLOTTIE_PLAYER_LIB=dotlottie_player

SDL2_CFLAGS=$(shell sdl2-config --cflags)
SDL2_LIBS=$(shell sdl2-config --libs)

BUILD_PLATFORM := $(shell uname -s | tr '[:upper:]' '[:lower:]')
MAC_BUILD_PLATFORM := darwin
ifeq ($(BUILD_PLATFORM),$(MAC_BUILD_PLATFORM))
FRAMEWORKS := -framework CoreText -framework CoreGraphics -framework Foundation -framework CoreServices
endif

all: demo-player

demo-player: skia main.cpp
$(CXX) -g -Iskia -Lskia/out/Static -L$(DOTLOTTIE_PLAYER_LIB_PATH) main.cpp \
-lSDL2 -l$(DOTLOTTIE_PLAYER_LIB) -lskia -lpng -ljpeg -lz -lfreetype -lwebp -lwebpdemux \
-o $@
$(CXX) -g --std=c++20 -Iskia -Lskia/out/Static -L$(DOTLOTTIE_PLAYER_LIB_PATH) main.cpp \
$(SDL2_CFLAGS) $(SDL2_LIBS) -l$(DOTLOTTIE_PLAYER_LIB) -lskia -ldng_sdk \
$(FRAMEWORKS) -o $@

run: export LD_LIBRARY_PATH = $(DOTLOTTIE_PLAYER_LIB_PATH)
run: demo-player
Expand All @@ -20,10 +29,13 @@ debug: demo-player

skia:
git clone [email protected]:google/skia.git

skia-build: skia
cd skia && \
python3 tools/git-sync-deps && \
python3 bin/fetch-ninja && \
bin/gn gen out/Static --args='is_official_build=true' && \
bin/gn gen out/Static \
--args='is_official_build=true skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false skia_use_icu=false skia_use_icu=false skia_use_harfbuzz=false skia_use_freetype=false' && \
ninja -C out/Static

clean:
Expand Down
10 changes: 5 additions & 5 deletions examples/skia-demo-player-cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_pixels.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_pixels.h>
#include <libgen.h> // For dirname
#include <limits.h> // For PATH_MAX
#include <memory>
Expand Down Expand Up @@ -161,14 +161,14 @@ int main(int argc, char **argv) {
dotlottie_render(player);
// Use skia to render an image
SkImageInfo imageInfo =
SkImageInfo::Make(WIDTH, HEIGHT, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
SkImageInfo::Make(WIDTH, HEIGHT, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
sk_sp<SkData> imageData = SkData::MakeWithoutCopy(buffer, WIDTH * HEIGHT * 4);
sk_sp<SkImage> bitmapImage = SkImages::RasterFromData(imageInfo, imageData, WIDTH * 4);
// Draw the image
SkRect src = SkRect::MakeWH(bitmapImage->width(), bitmapImage->height());
SkRect dst = SkRect::MakeWH(WIDTH, HEIGHT);
canvas->drawImageRect(bitmapImage, src, dst, SkSamplingOptions(), nullptr,
SkCanvas::kStrict_SrcRectConstraint);
SkCanvas::kStrict_SrcRectConstraint);
// Render the image in the window
SDL_UpdateTexture(texture, NULL, pixels, WIDTH * sizeof(Uint32));
SDL_RenderCopy(renderer, texture, NULL, NULL);
Expand Down

0 comments on commit 964931e

Please sign in to comment.