Skip to content

Commit

Permalink
💄 minor refactoring (#2752)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Nov 20, 2024
1 parent ad6e7d0 commit 516bdb5
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 28 deletions.
1 change: 1 addition & 0 deletions apps/paper/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ react {

/* Hermes Commands */
// The hermes compiler command to run. By default it is 'hermesc'
hermesCommand = "$rootDir/../../../node_modules/react-native/sdks/hermesc/%OS-BIN%/hermesc"
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
//
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
Expand Down
85 changes: 77 additions & 8 deletions apps/paper/src/Examples/API/UseCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,96 @@
import type { SkSize } from "@shopify/react-native-skia";
import { Canvas, Fill, Group, Rect, rect } from "@shopify/react-native-skia";
import {
BlurMask,
Canvas,
Circle,
Fill,
Group,
mix,
polar2Canvas,
vec,
} from "@shopify/react-native-skia";
import React, { useEffect, useRef } from "react";
import { View, Animated } from "react-native";
import { useNavigation } from "@react-navigation/native";
import { useContextBridge } from "its-fine";
import type { SharedValue } from "react-native-reanimated";
import { useDerivedValue, useSharedValue } from "react-native-reanimated";

interface MyCompProps {
import { useLoop } from "../../components/Animations";

const c1 = "#61bea2";
const c2 = "#529ca0";

interface SizeProps {
size: SharedValue<SkSize>;
}

const MyComp = ({ size }: MyCompProps) => {
interface RingProps extends SizeProps {
index: number;
progress: SharedValue<number>;
}

const Ring = ({ index, progress, size }: RingProps) => {
const R = useDerivedValue(() => size.value.width / 4);
const center = useDerivedValue(() =>
vec(size.value.width / 2, size.value.height / 2 - 64)
);

const theta = (index * (2 * Math.PI)) / 6;
const transform = useDerivedValue(() => {
const { x, y } = polar2Canvas(
{ theta, radius: progress.value * R.value },
{ x: 0, y: 0 }
);
const scale = mix(progress.value, 0.3, 1);
return [{ translateX: x }, { translateY: y }, { scale }];
}, [progress, R]);

return (
<Circle
c={center}
r={R}
color={index % 2 ? c1 : c2}
origin={center}
transform={transform}
/>
);
};

const BreatheDemo = ({ size }: SizeProps) => {
const center = useDerivedValue(() =>
vec(size.value.width / 2, size.value.height / 2 - 64)
);

const progress = useLoop({ duration: 3000 });

const transform = useDerivedValue(
() => [{ rotate: mix(progress.value, -Math.PI, 0) }],
[progress]
);

return (
<>
<Fill color="rgb(36,43,56)" />
<Group origin={center} transform={transform} blendMode="screen">
<BlurMask style="solid" blur={40} />
{new Array(6).fill(0).map((_, index) => {
return (
<Ring size={size} key={index} index={index} progress={progress} />
);
})}
</Group>
</>
);
};

const MyComp = ({ size }: SizeProps) => {
const navigation = useNavigation();
const { routeNames } = navigation.getState()!;
console.log({ routeNames });
const rct = useDerivedValue(() => {
return rect(0, 0, size.value.width, size.value.height / 2);
}, [size]);
return (
<Group>
<Fill color="magenta" />
<Rect color="cyan" rect={rct} />
<BreatheDemo size={size} />
</Group>
);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/skia/android/cpp/rnskia-android/OpenGLContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OpenGLContext {

SkSurfaceProps props(0, kUnknown_SkPixelGeometry);

auto result = _glContext->makeCurrent(*_glSurface);
auto result = _glContext->makeCurrent(_glSurface.get());
if (!result) {
return nullptr;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ class OpenGLContext {
_glConfig = _glDisplay->chooseConfig();
_glContext = _glDisplay->makeContext(_glConfig, nullptr);
_glSurface = _glDisplay->makePixelBufferSurface(_glConfig, 1, 1);
_glContext->makeCurrent(*_glSurface);
_glContext->makeCurrent(_glSurface.get());
auto backendInterface = GrGLMakeNativeInterface();
_directContext = GrDirectContexts::MakeGL(backendInterface);

Expand Down
13 changes: 11 additions & 2 deletions packages/skia/android/cpp/rnskia-android/OpenGLWindowContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ sk_sp<SkSurface> OpenGLWindowContext::getSurface() {
std::unique_ptr<gl::Surface> surface = nullptr;
};

if (!_window) {
throw std::runtime_error("No native window provided");
}
auto releaseCtx = new ReleaseContext();
releaseCtx->surface =
_context->_glDisplay->makeWindowSurface(_context->_glConfig, _window);
if (!releaseCtx->surface) {
throw std::runtime_error("Failed to create window surface");
}
_glSurface = releaseCtx->surface.get();

// Now make this one current
_context->_glContext->makeCurrent(*releaseCtx->surface);
auto success = _context->_glContext->makeCurrent(releaseCtx->surface.get());
if (!success) {
throw std::runtime_error("Failed to make window surface current");
}

// Set up parameters for the render target so that it
// matches the underlying OpenGL context.
Expand Down Expand Up @@ -73,7 +82,7 @@ sk_sp<SkSurface> OpenGLWindowContext::getSurface() {
}

void OpenGLWindowContext::present() {
_context->_glContext->makeCurrent(*_glSurface);
_context->_glContext->makeCurrent(_glSurface);
_context->_directContext->flushAndSubmit();
_glSurface->present();
}
Expand Down
10 changes: 8 additions & 2 deletions packages/skia/android/cpp/rnskia-android/OpenGLWindowContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ class OpenGLWindowContext : public WindowContext {
public:
OpenGLWindowContext(OpenGLContext *context, ANativeWindow *window, int width,
int height)
: _context(context), _window(window), _width(width), _height(height) {}
: _context(context), _window(window), _width(width), _height(height) {
ANativeWindow_acquire(_window);
}

~OpenGLWindowContext() { ANativeWindow_release(_window); }
~OpenGLWindowContext() {
_skSurface = nullptr;
_glSurface = nullptr;
ANativeWindow_release(_window);
}

sk_sp<SkSurface> getSurface() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
#if defined(SK_GRAPHITE)
return DawnContext::getInstance().MakeWindow(surface, width, height);
#else
return OpenGLContext::getInstance().MakeWindow(
reinterpret_cast<ANativeWindow *>(surface), width, height);
auto aWindow = reinterpret_cast<ANativeWindow *>(surface);
return OpenGLContext::getInstance().MakeWindow(aWindow, width, height);
#endif
}

Expand Down
8 changes: 4 additions & 4 deletions packages/skia/android/cpp/rnskia-android/gl/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ class Context {

const EGLContext &getHandle() const { return _context; }

bool makeCurrent(const Surface &surface) const {
bool makeCurrent(const Surface *surface) {
if (_context == EGL_NO_CONTEXT) {
return false;
}
const auto result =
eglMakeCurrentIfNecessary(_display, surface.getHandle(),
surface.getHandle(), _context) == EGL_TRUE;
eglMakeCurrentIfNecessary(_display, surface->getHandle(),
surface->getHandle(), _context) == EGL_TRUE;
if (!result) {
LOG_EGL_ERROR;
}
return result;
}

bool clearCurrent() const {
bool clearCurrent() {
const auto result =
eglMakeCurrentIfNecessary(_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT) == EGL_TRUE;
Expand Down
8 changes: 4 additions & 4 deletions packages/skia/android/cpp/rnskia-android/gl/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Display {

bool isValid() const { return _display != EGL_NO_DISPLAY; }

EGLConfig chooseConfig() const {
EGLConfig chooseConfig() {

EGLint att[] = {EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
Expand Down Expand Up @@ -82,14 +82,14 @@ class Display {
}

std::unique_ptr<Surface> makeWindowSurface(const EGLConfig &config,
EGLNativeWindowType window) {
ANativeWindow *window) {
const EGLint attribs[] = {EGL_NONE};
auto surface = eglCreateWindowSurface(_display, config, window, attribs);
if (surface == EGL_NO_SURFACE) {
LOG_EGL_ERROR;
return nullptr;
}
return std::unique_ptr<Surface>(new Surface(_display, surface));
return std::make_unique<Surface>(_display, surface);
}

std::unique_ptr<Surface> makePixelBufferSurface(const EGLConfig &config,
Expand All @@ -101,7 +101,7 @@ class Display {
LOG_EGL_ERROR;
return nullptr;
}
return std::unique_ptr<Surface>(new Surface(_display, surface));
return std::make_unique<Surface>(_display, surface);
}

const EGLDisplay &getHandle() const { return _display; }
Expand Down
8 changes: 4 additions & 4 deletions packages/skia/android/cpp/rnskia-android/gl/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace gl {

class Surface {
public:
Surface(EGLDisplay display, EGLSurface surface)
: _display(display), _surface(surface) {}

~Surface() {
if (_surface != EGL_NO_SURFACE) {
if (eglDestroySurface(_display, _surface) != EGL_TRUE) {
Expand All @@ -18,7 +21,7 @@ class Surface {

const EGLSurface &getHandle() const { return _surface; }

bool present() const {
bool present() {
const auto result = eglSwapBuffers(_display, _surface) == EGL_TRUE;
if (!result) {
LOG_EGL_ERROR;
Expand All @@ -32,9 +35,6 @@ class Surface {
EGLDisplay _display = EGL_NO_DISPLAY;
EGLSurface _surface = EGL_NO_SURFACE;

Surface(EGLDisplay display, EGLSurface surface)
: _display(display), _surface(surface) {}

Surface(const Surface &) = delete;

Surface &operator=(const Surface &) = delete;
Expand Down
3 changes: 3 additions & 0 deletions packages/skia/cpp/api/JsiSkiaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class JsiSkiaContext : public JsiSkWrappingSharedPtrHostObject<WindowContext> {
void *surface = reinterpret_cast<void *>(nativeBufferPointer);
auto width = static_cast<int>(arguments[1].asNumber());
auto height = static_cast<int>(arguments[2].asNumber());
if (surface == nullptr) {
throw std::runtime_error("Surface is null");
}
auto result =
context->makeContextFromNativeSurface(surface, width, height);
// Return the newly constructed object
Expand Down

0 comments on commit 516bdb5

Please sign in to comment.