0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-14 07:51:35 -05:00

wip: c++ initial version

This commit is contained in:
AzazelN28 2024-09-27 11:24:34 +02:00
parent 864088eecd
commit 472c769c9a
20 changed files with 6567 additions and 468 deletions

View file

@ -1,5 +1 @@
target/
debug/
**/*.rs.bk
out

View file

@ -1,324 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bumpalo"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "cc"
version = "1.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]]
name = "js-sys"
version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "log"
version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "minicov"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c71e683cd655513b99affab7d317deb690528255a0d5f717f1024093c12b169"
dependencies = [
"cc",
"walkdir",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "renderer"
version = "0.1.0"
dependencies = [
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
dependencies = [
"cfg-if",
"once_cell",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68497a05fb21143a08a7d24fc81763384a3072ee43c44e86aad1744d6adef9d9"
dependencies = [
"console_error_panic_hook",
"js-sys",
"minicov",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8220be1fa9e4c889b30fd207d4906657e7e90b12e0e6b0c8b8d8709f5de021"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "web-sys"
version = "0.3.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View file

@ -1,19 +0,0 @@
[package]
name = "renderer"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/penpot/penpot"
license-file = "../../../../LICENSE"
description = "Wasm-based canvas renderer for Penpot"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.93"
[profile.release]
opt-level = "s"
[dev-dependencies]
wasm-bindgen-test = "0.3.43"

View file

@ -0,0 +1,108 @@
FROM debian:11
RUN apt update && apt dist-upgrade -y && apt install -y \
git \
clang \
python \
curl \
build-essential \
libfontconfig-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
procps \
vim \
binaryen \
wabt \
&& groupadd -g 2000 skia \
&& useradd -u 2000 -g 2000 skia
# TODO(kjlubick): Try a shallow clone of depot_tools
RUN cd /tmp \
&& git clone 'https://chromium.googlesource.com/chromium/tools/depot_tools.git'
RUN cd /tmp \
&& git clone 'https://gn.googlesource.com/gn'
RUN cd /tmp \
&& git clone 'https://skia.googlesource.com/skia' \
&& cd skia \
&& git checkout 'chrome/m127'
ENV PATH=${PATH}:/tmp/depot_tools
ENV PATH=${PATH}:/tmp/gn
ADD --chown=skia:skia https://storage.googleapis.com/skia-swiftshader/libGLESv2.so /usr/local/lib/libGLESv2.so
ADD --chown=skia:skia https://storage.googleapis.com/skia-swiftshader/libEGL.so /usr/local/lib/libEGL.so
# FIXME: I don't like this approach because it implies that
# git-sync-deps is going to fail and we need to run it two
# times. The weird thing is that git-sync-deps fails consistently
# the first time.
RUN cd /tmp/skia; \
tools/git-sync-deps; \
tools/git-sync-deps; \
exit 0
RUN cd /tmp/skia && python3 bin/fetch-ninja
RUN . "tmp/skia/third_party/externals/emsdk/emsdk_env.sh"
RUN cd /tmp/skia && ./bin/gn gen out/wasm \
--args="is_debug=false \
is_official_build=true \
is_component_build=false \
is_trivial_abi=true \
werror=true \
target_cpu=\"wasm\" \
skia_use_angle=false \
skia_use_dng_sdk=false \
skia_use_dawn=false \
skia_use_webgl=true \
skia_use_webgpu=false \
skia_use_expat=false \
skia_use_fontconfig=false \
skia_use_freetype=true \
skia_use_libheif=false \
skia_use_libjpeg_turbo_decode=true \
skia_use_libjpeg_turbo_encode=true \
skia_use_no_jpeg_encode=false \
skia_use_libpng_decode=true \
skia_use_libpng_encode=true \
skia_use_no_png_encode=false \
skia_use_libwebp_decode=true \
skia_use_libwebp_encode=true \
skia_use_no_webp_encode=false \
skia_use_lua=false \
skia_use_piex=false \
skia_use_system_freetype2=false \
skia_use_system_libjpeg_turbo=false \
skia_use_system_libpng=false \
skia_use_system_libwebp=false \
skia_use_system_zlib=false \
skia_use_vulkan=false \
skia_use_wuffs=true \
skia_use_zlib=true \
skia_enable_ganesh=true \
skia_enable_sksl=true \
skia_build_for_debugger=false \
skia_enable_sksl_tracing=true \
skia_use_icu=true \
skia_use_client_icu=false \
skia_use_libgrapheme=false \
skia_use_system_icu=false \
skia_use_harfbuzz=true \
skia_use_system_harfbuzz=false \
skia_enable_fontmgr_custom_directory=false \
skia_enable_fontmgr_custom_embedded=true \
skia_enable_fontmgr_custom_empty=false \
skia_fontmgr_factory=\":fontmgr_custom_embedded_factory\" \
skia_use_freetype_woff2=true \
skia_enable_skshaper=true \
skia_enable_skparagraph=true \
skia_enable_pdf=false"
RUN cd /tmp/skia; ninja -C out/wasm
RUN echo "source '/tmp/skia/third_party/externals/emsdk/emsdk_env.sh'" >> /root/.bashrc
COPY docker/entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -0,0 +1,66 @@
all:
# -fno-rtti: Removes C++ Run-Time Type Info support.
# --no-entry: Disables the necessity of an entry point.
# -sALLOW_MEMORY_GROWTH: Creates a resizable memory ArrayBuffer.
# -sMODULARIZE: Exports emscripten as a CommonJS/AMD module.
# -sENVIRONMENT: Removes unnecessary environments (node,worker,etc).
# -sUSE_PTHREADS: Disables pthreads.
# -sMAX_WEBGL_VERSION: Max WebGL set to 2
# -sUSE_WEBGL2: Uses WebGL2 by default.
emcc \
-std=c++20 \
-lembind \
-fno-rtti \
--no-entry \
-sALLOW_MEMORY_GROWTH \
-sUSE_PTHREADS=0 \
-sMODULARIZE=1 \
-sDISABLE_EXCEPTION_CATCHING \
-sNODEJS_CATCH_EXIT=0 \
-sMAX_WEBGL_VERSION=2 \
-sUSE_WEBGL2=1 \
-sFORCE_FILESYSTEM=0 \
-sDYNAMIC_EXECUTION=0 \
-sFILESYSTEM=0 \
-sENVIRONMENT='web' \
-sINITIAL_MEMORY=128MB \
-DCK_ENABLE_WEBGL \
-DCK_NO_FONTS \
-DSK_RELEASE \
-DSK_DISABLE_TRACING \
-DSK_FORCE_AAA \
-DSK_FORCE_8_BYTE_ALIGNMENT \
-DSK_SHAPER_HARFBUZZ_AVAILABLE \
-DCK_INCLUDE_PARAGRAPH \
-DCK_SERIALIZE_SKP \
-DSK_GANESH \
-DSK_DISABLE_LEGACY_SHADERCONTEXT \
-DCK_INCLUDE_PATHOPS \
-DCK_INCLUDE_RUNTIME_EFFECT \
-DSKSL_ENABLE_TRACING \
-DNDEBUG \
-DSK_TRIVIAL_ABI="[[clang::trivial_abi]]" \
-DSK_TYPEFACE_FACTORY_FREETYPE \
-DSK_GL \
-DSK_CODEC_DECODES_JPEG \
-DSK_CODEC_DECODES_PNG \
-DSK_CODEC_DECODES_WEBP \
-DSK_HAS_WUFFS_LIBRARY \
-DSK_ENABLE_SKSL \
-DSK_ENABLE_PRECOMPILE \
-DSKNX_NO_SIMD \
-DSK_ASSUME_WEBGL=1 \
-DSK_USE_WEBGL \
-DSK_ENABLE_PARAGRAPH \
-DSK_UNICODE_AVAILABLE \
-DSK_UNICODE_ICU_IMPLEMENTATION \
-DSK_ENABLE_SKOTTIE \
-DSK_ENABLE_SKOTTIE_SKSLEFFECT \
-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 \
--pre-js js/preamble.js \
--pre-js js/postamble.js \
-I/tmp/skia \
-o out/renderer.js \
/tmp/skia/out/wasm/modules/canvaskit/fonts/NotoMono-Regular.ttf.ninja.cpp \
/tmp/skia/out/wasm/libskia.a \
src/main.cpp

View file

@ -0,0 +1,49 @@
# Renderer
## How this works?
First of all we need a proper environment to build Skia, this
environment is heavily based on the [Skia docker image](https://github.com/google/skia/blob/main/docker/skia-release/Dockerfile) but with some tweaks to support building
a C++ WebAssembly module using [Emscripten](https://emscripten.org/index.html).
## Building everything
From the root directory of `frontend/renderer` just run:
```sh
./scripts/build
```
This is going to build the docker image and run the container to build
the artifacts and then copy them to the necessary directories.
> :smile_cat: Be patient, the first time the docker image is built usually takes
> a few minutes.
## Building the Skia build tools Docker image
To build just the Skia build tools image:
```sh
cd frontend/renderer
docker build . -t skia-build-tools
```
## Building the renderer WebAssembly module
Just run the container and it will generate all the necessary
artifacts in the `out` directory.
```sh
cd frontend/renderer
docker run -t -v ${PWD}:/tmp/renderer skia-build-tools
```
Once the `renderer.js` and `renderer.wasm` are created in the `out` directory
we need to move them where Penpot can have access to them, so we need to execute
`./scripts/copy-artifacts`.
## C++ <-> JS
To add some extra functionality to the exported `Module` by the Emscripten
compiler, we use a series of javascript scripts that exist on the `js` directory.

View file

@ -0,0 +1,11 @@
# TO DO
- [x] Compile Skia.
- [x] Compile simple `renderer.wasm` with a exported function.
- [ ] Compile a `renderer.wasm` that uses a WebGL context.
## Notes
- I've used the Skia `main` branch and it looks that there's something missing from the last release (`chrome/m117`) so I tried to switch to that branch but now I have different issues.
- It is necessary to use the GL emscripten module to deal with WebGL contexts. See `js/preamble.js` and

View file

@ -0,0 +1,4 @@
#!/bin/bash
source /root/.bashrc
cd /tmp/renderer
emmake make

View file

@ -0,0 +1,2 @@
console.log("postamble");
}(Module));

View file

@ -0,0 +1,13 @@
// Adds compile-time JS functions to augment Renderer interface.
(function (Renderer) {
console.log("preamble", Renderer);
Renderer.setCanvas = function setCanvas(canvas, attrs) {
console.log("GL", GL);
const context = GL.createContext(canvas, attrs);
if (!context) {
throw new Error('Could not create a new WebGL context')
}
console.log("setCanvas", canvas, attrs);
const gr = this._MakeGrContext();
console.log("gr", gr);
};

View file

@ -0,0 +1,4 @@
#!/bin/bash
docker build . -t skia-build-tools
docker run -t -v $PWD:/tmp/renderer skia-build-tools
./scripts/copy-artifacts

View file

@ -0,0 +1,11 @@
#!/bin/bash
mkdir -p ../../resources/public/js/renderer/cpp
mkdir -p ../../src/app/renderer/
# FIXME: This is a VERY HACKY way to set the correct `scriptDirectory` but
# I didn't find a better way yet.
PREAMBLE_LINES=`wc -l js/preamble.js | egrep -o [0-9]+`
POSTAMBLE_LINES=`wc -l js/postamble.js | egrep -o [0-9]+`
LINE_NUMBER=`echo "200 + ${PREAMBLE_LINES} + ${POSTAMBLE_LINES}" | bc | egrep -o [0-9]+`
sed "${LINE_NUMBER} i \ \ scriptDirectory += 'renderer/cpp/';" out/renderer.js > ../../src/app/renderer/cpp.js
cp out/renderer.wasm ../../resources/public/js/renderer/cpp

View file

@ -0,0 +1,260 @@
#include "include/private/base/SkMalloc.h"
#include "include/android/SkAnimatedImage.h"
#include "include/codec/SkAndroidCodec.h"
#include "include/codec/SkCodec.h"
#include "include/codec/SkEncodedImageFormat.h"
#include "include/core/SkBBHFactory.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkBlender.h"
#include "include/core/SkBlurTypes.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageGenerator.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkM44.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPathEffect.h"
#include "include/core/SkPathMeasure.h"
#include "include/core/SkPathUtils.h"
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkPoint3.h"
#include "include/core/SkRRect.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSerialProcs.h"
#include "include/core/SkShader.h"
#include "include/core/SkSpan.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkStrokeRec.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTextBlob.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/core/SkVertices.h"
#include "include/effects/Sk1DPathEffect.h"
#include "include/effects/Sk2DPathEffect.h"
#include "include/effects/SkCornerPathEffect.h"
#include "include/effects/SkDashPathEffect.h"
#include "include/effects/SkDiscretePathEffect.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkLumaColorFilter.h"
#include "include/effects/SkPerlinNoiseShader.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/effects/SkTrimPathEffect.h"
#include "include/encode/SkJpegEncoder.h"
#include "include/encode/SkPngEncoder.h"
#include "include/encode/SkWebpEncoder.h"
#include "include/private/base/SkOnce.h"
#include "include/utils/SkParsePath.h"
#include "include/utils/SkShadowUtils.h"
#include "src/core/SkPathPriv.h"
#include "src/core/SkResourceCache.h"
#include "src/image/SkImage_Base.h"
#include "src/sksl/SkSLCompiler.h"
#include "modules/canvaskit/WasmCommon.h"
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/html5.h>
#if defined(CK_ENABLE_WEBGL) || defined(CK_ENABLE_WEBGPU)
#define ENABLE_GPU
#endif
#ifdef ENABLE_GPU
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/ganesh/GrExternalTextureGenerator.h"
#include "include/gpu/ganesh/SkImageGanesh.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "src/gpu/ganesh/GrCaps.h"
#endif // ENABLE_GPU
#ifdef CK_ENABLE_WEBGL
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrTypes.h"
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "include/gpu/gl/GrGLInterface.h"
#include "include/gpu/gl/GrGLTypes.h"
#include "src/gpu/RefCntedCallback.h"
#include "src/gpu/ganesh/GrProxyProvider.h"
#include "src/gpu/ganesh/GrRecordingContextPriv.h"
#include "src/gpu/ganesh/gl/GrGLDefines.h"
#include <webgl/webgl1.h>
#endif // CK_ENABLE_WEBGL
#ifdef CK_INCLUDE_PATHOPS
#include "include/pathops/SkPathOps.h"
#endif
// Necessary to prevent C++ name mangling.
extern "C" {
EMSCRIPTEN_KEEPALIVE int add(int a, int b) {
return a + b;
}
}
struct OptionalMatrix : SkMatrix {
OptionalMatrix(WASMPointerF32 mPtr) {
if (mPtr) {
const SkScalar* nineMatrixValues = reinterpret_cast<const SkScalar*>(mPtr);
this->set9(nineMatrixValues);
}
}
};
SkColor4f ptrToSkColor4f(WASMPointerF32 cPtr) {
float* fourFloats = reinterpret_cast<float*>(cPtr);
SkColor4f color;
memcpy(&color, fourFloats, 4 * sizeof(float));
return color;
}
SkRRect ptrToSkRRect(WASMPointerF32 fPtr) {
// In order, these floats should be 4 floats for the rectangle
// (left, top, right, bottom) and then 8 floats for the radii
// (upper left, upper right, lower right, lower left).
const SkScalar* twelveFloats = reinterpret_cast<const SkScalar*>(fPtr);
const SkRect rect = reinterpret_cast<const SkRect*>(twelveFloats)[0];
const SkVector* radiiValues = reinterpret_cast<const SkVector*>(twelveFloats + 4);
SkRRect rr;
rr.setRectRadii(rect, radiiValues);
return rr;
}
// Surface creation structs and helpers
struct SimpleImageInfo {
int width;
int height;
SkColorType colorType;
SkAlphaType alphaType;
sk_sp<SkColorSpace> colorSpace;
};
SkImageInfo toSkImageInfo(const SimpleImageInfo& sii) {
return SkImageInfo::Make(sii.width, sii.height, sii.colorType, sii.alphaType,
sii.colorSpace ? sii.colorSpace : SkColorSpace::MakeSRGB());
}
#ifdef CK_ENABLE_WEBGL
// Set the pixel format based on the colortype.
// These degrees of freedom are removed from canvaskit only to keep the interface simpler.
struct ColorSettings {
ColorSettings(sk_sp<SkColorSpace> colorSpace) {
if (colorSpace == nullptr || colorSpace->isSRGB()) {
colorType = kRGBA_8888_SkColorType;
pixFormat = GR_GL_RGBA8;
} else {
colorType = kRGBA_F16_SkColorType;
pixFormat = GR_GL_RGBA16F;
}
}
SkColorType colorType;
GrGLenum pixFormat;
};
sk_sp<GrDirectContext> MakeGrContext()
{
// We assume that any calls we make to GL for the remainder of this function will go to the
// desired WebGL Context.
// setup interface.
auto interface = GrGLMakeNativeInterface();
// setup context
return GrDirectContext::MakeGL(interface);
}
sk_sp<SkSurface> MakeOnScreenGLSurface(sk_sp<GrDirectContext> dContext, int width, int height,
sk_sp<SkColorSpace> colorSpace, int sampleCnt, int stencil) {
// WebGL should already be clearing the color and stencil buffers, but do it again here to
// ensure Skia receives them in the expected state.
emscripten_glBindFramebuffer(GL_FRAMEBUFFER, 0);
emscripten_glClearColor(0, 0, 0, 0);
emscripten_glClearStencil(0);
emscripten_glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
dContext->resetContext(kRenderTarget_GrGLBackendState | kMisc_GrGLBackendState);
// The on-screen canvas is FBO 0. Wrap it in a Skia render target so Skia can render to it.
GrGLFramebufferInfo info;
info.fFBOID = 0;
if (!colorSpace) {
colorSpace = SkColorSpace::MakeSRGB();
}
const auto colorSettings = ColorSettings(colorSpace);
info.fFormat = colorSettings.pixFormat;
auto target = GrBackendRenderTargets::MakeGL(width, height, sampleCnt, stencil, info);
sk_sp<SkSurface> surface(SkSurfaces::WrapBackendRenderTarget(dContext.get(),
target,
kBottomLeft_GrSurfaceOrigin,
colorSettings.colorType,
colorSpace,
nullptr));
return surface;
}
sk_sp<SkSurface> MakeOnScreenGLSurface(sk_sp<GrDirectContext> dContext, int width, int height,
sk_sp<SkColorSpace> colorSpace) {
GrGLint sampleCnt;
emscripten_glGetIntegerv(GL_SAMPLES, &sampleCnt);
GrGLint stencil;
emscripten_glGetIntegerv(GL_STENCIL_BITS, &stencil);
return MakeOnScreenGLSurface(dContext, width, height, colorSpace, sampleCnt, stencil);
}
sk_sp<SkSurface> MakeRenderTarget(sk_sp<GrDirectContext> dContext, int width, int height) {
SkImageInfo info = SkImageInfo::MakeN32(
width, height, SkAlphaType::kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
sk_sp<SkSurface> surface(SkSurfaces::RenderTarget(dContext.get(),
skgpu::Budgeted::kYes,
info,
0,
kBottomLeft_GrSurfaceOrigin,
nullptr,
true));
return surface;
}
sk_sp<SkSurface> MakeRenderTarget(sk_sp<GrDirectContext> dContext, SimpleImageInfo sii) {
sk_sp<SkSurface> surface(SkSurfaces::RenderTarget(dContext.get(),
skgpu::Budgeted::kYes,
toSkImageInfo(sii),
0,
kBottomLeft_GrSurfaceOrigin,
nullptr,
true));
return surface;
}
#endif // CK_ENABLE_WEBGL
EMSCRIPTEN_BINDINGS(Renderer) {
#ifdef ENABLE_GPU
constant("gpu", true);
function("_MakeGrContext", &MakeGrContext);
#endif // ENABLE_GPU
#ifdef CK_ENABLE_WEBGL
constant("webgl", true);
function("_MakeOnScreenGLSurface", select_overload<sk_sp<SkSurface>(sk_sp<GrDirectContext>, int, int, sk_sp<SkColorSpace>)>(&MakeOnScreenGLSurface));
function("_MakeOnScreenGLSurface", select_overload<sk_sp<SkSurface>(sk_sp<GrDirectContext>, int, int, sk_sp<SkColorSpace>, int, int)>(&MakeOnScreenGLSurface));
function("_MakeRenderTargetWH", select_overload<sk_sp<SkSurface>(sk_sp<GrDirectContext>, int, int)>(&MakeRenderTarget));
function("_MakeRenderTargetII", select_overload<sk_sp<SkSurface>(sk_sp<GrDirectContext>, SimpleImageInfo)>(&MakeRenderTarget));
#endif // CK_ENABLE_WEBGL
}

View file

@ -1,36 +0,0 @@
use wasm_bindgen::prelude::*;
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn print(msg: &str) {
log(msg);
}
#[cfg(test)]
mod tests {
use super::*;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[wasm_bindgen_test]
fn it_works_in_wasm() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View file

@ -74,7 +74,7 @@
[app.main.repo :as rp]
[app.main.streams :as ms]
[app.main.worker :as uw]
[app.renderer-v2 :as renderer]
[app.renderer.cpp :as renderer-cpp]
[app.util.dom :as dom]
[app.util.globals :as ug]
[app.util.http :as http]

View file

@ -32,7 +32,7 @@
[app.main.ui.workspace.sidebar.collapsable-button :refer [collapsed-button]]
[app.main.ui.workspace.sidebar.history :refer [history-toolbox]]
[app.main.ui.workspace.viewport :refer [viewport]]
[app.renderer-v2 :as renderer]
[app.renderer.cpp :as renderer-cpp]
[app.util.debug :as dbg]
[app.util.dom :as dom]
[app.util.globals :as globals]
@ -200,10 +200,6 @@
(ntf/hide)
(dw/finalize-file project-id file-id))))
(mf/with-effect [file-ready?]
(when (and file-ready? (contains? cf/flags :renderer-v2))
(renderer/print-msg "hello from wasm fn!")))
[:& (mf/provider ctx/current-file-id) {:value file-id}
[:& (mf/provider ctx/current-project-id) {:value project-id}
[:& (mf/provider ctx/current-team-id) {:value team-id}

View file

@ -14,6 +14,7 @@
[app.common.geom.shapes :as gsh]
[app.common.types.shape-tree :as ctt]
[app.common.types.shape.layout :as ctl]
[app.config :as cf]
[app.main.data.workspace.modifiers :as dwm]
[app.main.refs :as refs]
[app.main.ui.context :as ctx]
@ -47,6 +48,7 @@
[app.main.ui.workspace.viewport.utils :as utils]
[app.main.ui.workspace.viewport.viewport-ref :refer [create-viewport-ref]]
[app.main.ui.workspace.viewport.widgets :as widgets]
[app.renderer.cpp :as renderer-cpp]
[app.util.debug :as dbg]
[beicon.v2.core :as rx]
[rumext.v2 :as mf]))
@ -111,6 +113,8 @@
selected-shapes (keep (d/getf objects-modified) selected)
canvas-ref (mf/use-ref nil)
;; STATE
alt? (mf/use-state false)
shift? (mf/use-state false)
@ -263,6 +267,12 @@
rule-area-size (/ rulers/ruler-area-size zoom)]
(mf/with-effect
[canvas-ref]
;; FIXME:
(let [canvas (mf/ref-val canvas-ref)]
(renderer/set-canvas canvas)))
(hooks/setup-dom-events zoom disable-paste in-viewport? workspace-read-only? drawing-tool drawing-path?)
(hooks/setup-viewport-size vport viewport-ref)
(hooks/setup-cursor cursor alt? mod? space? panning drawing-tool drawing-path? node-editing? z? workspace-read-only?)
@ -304,50 +314,59 @@
:layout layout
:viewport-ref viewport-ref}])]
[:svg
{:id "render"
:class (stl/css :render-shapes)
:xmlns "http://www.w3.org/2000/svg"
:xmlnsXlink "http://www.w3.org/1999/xlink"
:xmlns:penpot "https://penpot.app/xmlns"
:preserveAspectRatio "xMidYMid meet"
:key (str "render" page-id)
:width (:width vport 0)
:height (:height vport 0)
:view-box (utils/format-viewbox vbox)
:style {:background-color background
:pointer-events "none"}
:fill "none"}
(if (contains? cf/flags :renderer-v2)
[:canvas {:id "render"
:ref canvas-ref
:class (stl/css :render-shapes)
:key (str "render" page-id)
:width (:width vport 0)
:height (:height vport 0)
:style {:background-color background
:pointer-events "none"}}]
[:svg
{:id "render"
:class (stl/css :render-shapes)
:xmlns "http://www.w3.org/2000/svg"
:xmlnsXlink "http://www.w3.org/1999/xlink"
:xmlns:penpot "https://penpot.app/xmlns"
:preserveAspectRatio "xMidYMid meet"
:key (str "render" page-id)
:width (:width vport 0)
:height (:height vport 0)
:view-box (utils/format-viewbox vbox)
:style {:background-color background
:pointer-events "none"}
:fill "none"}
[:defs
[:linearGradient {:id "frame-placeholder-gradient"}
[:animateTransform
{:attributeName "gradientTransform"
:type "translate"
:from "-1 0"
:to "1 0"
:dur "2s"
:repeatCount "indefinite"}]
[:stop {:offset "0%" :stop-color (str "color-mix(in srgb-linear, " background " 90%, #777)") :stop-opacity 1}]
[:stop {:offset "50%" :stop-color (str "color-mix(in srgb-linear, " background " 80%, #777)") :stop-opacity 1}]
[:stop {:offset "100%" :stop-color (str "color-mix(in srgb-linear, " background " 90%, #777)") :stop-opacity 1}]]]
[:defs
[:linearGradient {:id "frame-placeholder-gradient"}
[:animateTransform
{:attributeName "gradientTransform"
:type "translate"
:from "-1 0"
:to "1 0"
:dur "2s"
:repeatCount "indefinite"}]
[:stop {:offset "0%" :stop-color (str "color-mix(in srgb-linear, " background " 90%, #777)") :stop-opacity 1}]
[:stop {:offset "50%" :stop-color (str "color-mix(in srgb-linear, " background " 80%, #777)") :stop-opacity 1}]
[:stop {:offset "100%" :stop-color (str "color-mix(in srgb-linear, " background " 90%, #777)") :stop-opacity 1}]]]
(when (dbg/enabled? :show-export-metadata)
[:& use/export-page {:page page}])
(when (dbg/enabled? :show-export-metadata)
[:& use/export-page {:page page}])
;; We need a "real" background shape so layer transforms work properly in firefox
[:rect {:width (:width vbox 0)
:height (:height vbox 0)
:x (:x vbox 0)
:y (:y vbox 0)
:fill background}]
;; We need a "real" background shape so layer transforms work properly in firefox
[:rect {:width (:width vbox 0)
:height (:height vbox 0)
:x (:x vbox 0)
:y (:y vbox 0)
:fill background}]
[:& (mf/provider ctx/current-vbox) {:value vbox'}
[:& (mf/provider use/include-metadata-ctx) {:value (dbg/enabled? :show-export-metadata)}
;; Render root shape
[:& shapes/root-shape {:key page-id
:objects base-objects
:active-frames @active-frames}]]]]
[:& (mf/provider ctx/current-vbox) {:value vbox'}
[:& (mf/provider use/include-metadata-ctx) {:value (dbg/enabled? :show-export-metadata)}
;; Render root shape
[:& shapes/root-shape {:key page-id
:objects base-objects
:active-frames @active-frames}]]]])
[:svg.viewport-controls
{:xmlns "http://www.w3.org/2000/svg"

View file

@ -0,0 +1,33 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.renderer.cpp
(:require
["./cpp.js" :as renderer]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
(defonce ^:dynamic internal-module nil)
(defn set-canvas
[canvas]
(js/console.log "setting canvas" canvas)
(.setCanvas internal-module canvas #js {:antialias false}))
(defn on-init
[module']
(set! internal-module module')
(js/console.log "internal-module" internal-module module')
(js/console.log "add 2 + 2" (._add ^js module' 2 2)))
(defn init
[]
(ptk/reify ::init
ptk/WatchEvent
(watch [_ _ _]
(let [p (renderer)]
(->> (rx/from p)
(rx/map #(on-init %)))))))

File diff suppressed because it is too large Load diff

View file

@ -1,38 +0,0 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.renderer-v2
(:require
[app.config :as cf]
[beicon.v2.core :as rx]
[potok.v2.core :as ptk]))
(defonce internal-module #js {})
(defn on-module-loaded
[module']
(let [init-fn (.-default ^js module')]
(->> (rx/from (init-fn))
(rx/map (constantly module')))))
(defn- on-module-initialized
[module]
(set! internal-module module))
(defn print-msg [msg]
(let [print-fn (.-print internal-module)]
(print-fn msg)))
(defn init
[]
(ptk/reify ::init
ptk/WatchEvent
(watch [_ _ _]
(let [module-uri (assoc cf/public-uri :path "/js/renderer/renderer.js")]
(->> (rx/from (js/dynamicImport (str module-uri)))
(rx/mapcat on-module-loaded)
(rx/tap on-module-initialized)
(rx/ignore))))))