Skip to content

Commit 516bdb5

Browse files
authored
💄 minor refactoring (#2752)
1 parent ad6e7d0 commit 516bdb5

File tree

10 files changed

+116
-28
lines changed

10 files changed

+116
-28
lines changed

‎apps/paper/android/app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ react {
4545

4646
/* Hermes Commands */
4747
// The hermes compiler command to run. By default it is 'hermesc'
48+
hermesCommand = "$rootDir/../../../node_modules/react-native/sdks/hermesc/%OS-BIN%/hermesc"
4849
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
4950
//
5051
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"

‎apps/paper/src/Examples/API/UseCanvas.tsx

+77-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,96 @@
11
import type { SkSize } from "@shopify/react-native-skia";
2-
import { Canvas, Fill, Group, Rect, rect } from "@shopify/react-native-skia";
2+
import {
3+
BlurMask,
4+
Canvas,
5+
Circle,
6+
Fill,
7+
Group,
8+
mix,
9+
polar2Canvas,
10+
vec,
11+
} from "@shopify/react-native-skia";
312
import React, { useEffect, useRef } from "react";
413
import { View, Animated } from "react-native";
514
import { useNavigation } from "@react-navigation/native";
615
import { useContextBridge } from "its-fine";
716
import type { SharedValue } from "react-native-reanimated";
817
import { useDerivedValue, useSharedValue } from "react-native-reanimated";
918

10-
interface MyCompProps {
19+
import { useLoop } from "../../components/Animations";
20+
21+
const c1 = "#61bea2";
22+
const c2 = "#529ca0";
23+
24+
interface SizeProps {
1125
size: SharedValue<SkSize>;
1226
}
1327

14-
const MyComp = ({ size }: MyCompProps) => {
28+
interface RingProps extends SizeProps {
29+
index: number;
30+
progress: SharedValue<number>;
31+
}
32+
33+
const Ring = ({ index, progress, size }: RingProps) => {
34+
const R = useDerivedValue(() => size.value.width / 4);
35+
const center = useDerivedValue(() =>
36+
vec(size.value.width / 2, size.value.height / 2 - 64)
37+
);
38+
39+
const theta = (index * (2 * Math.PI)) / 6;
40+
const transform = useDerivedValue(() => {
41+
const { x, y } = polar2Canvas(
42+
{ theta, radius: progress.value * R.value },
43+
{ x: 0, y: 0 }
44+
);
45+
const scale = mix(progress.value, 0.3, 1);
46+
return [{ translateX: x }, { translateY: y }, { scale }];
47+
}, [progress, R]);
48+
49+
return (
50+
<Circle
51+
c={center}
52+
r={R}
53+
color={index % 2 ? c1 : c2}
54+
origin={center}
55+
transform={transform}
56+
/>
57+
);
58+
};
59+
60+
const BreatheDemo = ({ size }: SizeProps) => {
61+
const center = useDerivedValue(() =>
62+
vec(size.value.width / 2, size.value.height / 2 - 64)
63+
);
64+
65+
const progress = useLoop({ duration: 3000 });
66+
67+
const transform = useDerivedValue(
68+
() => [{ rotate: mix(progress.value, -Math.PI, 0) }],
69+
[progress]
70+
);
71+
72+
return (
73+
<>
74+
<Fill color="rgb(36,43,56)" />
75+
<Group origin={center} transform={transform} blendMode="screen">
76+
<BlurMask style="solid" blur={40} />
77+
{new Array(6).fill(0).map((_, index) => {
78+
return (
79+
<Ring size={size} key={index} index={index} progress={progress} />
80+
);
81+
})}
82+
</Group>
83+
</>
84+
);
85+
};
86+
87+
const MyComp = ({ size }: SizeProps) => {
1588
const navigation = useNavigation();
1689
const { routeNames } = navigation.getState()!;
1790
console.log({ routeNames });
18-
const rct = useDerivedValue(() => {
19-
return rect(0, 0, size.value.width, size.value.height / 2);
20-
}, [size]);
2191
return (
2292
<Group>
23-
<Fill color="magenta" />
24-
<Rect color="cyan" rect={rct} />
93+
<BreatheDemo size={size} />
2594
</Group>
2695
);
2796
};

‎packages/skia/android/cpp/rnskia-android/OpenGLContext.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class OpenGLContext {
3333

3434
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
3535

36-
auto result = _glContext->makeCurrent(*_glSurface);
36+
auto result = _glContext->makeCurrent(_glSurface.get());
3737
if (!result) {
3838
return nullptr;
3939
}
@@ -142,7 +142,7 @@ class OpenGLContext {
142142
_glConfig = _glDisplay->chooseConfig();
143143
_glContext = _glDisplay->makeContext(_glConfig, nullptr);
144144
_glSurface = _glDisplay->makePixelBufferSurface(_glConfig, 1, 1);
145-
_glContext->makeCurrent(*_glSurface);
145+
_glContext->makeCurrent(_glSurface.get());
146146
auto backendInterface = GrGLMakeNativeInterface();
147147
_directContext = GrDirectContexts::MakeGL(backendInterface);
148148

‎packages/skia/android/cpp/rnskia-android/OpenGLWindowContext.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ sk_sp<SkSurface> OpenGLWindowContext::getSurface() {
2121
std::unique_ptr<gl::Surface> surface = nullptr;
2222
};
2323

24+
if (!_window) {
25+
throw std::runtime_error("No native window provided");
26+
}
2427
auto releaseCtx = new ReleaseContext();
2528
releaseCtx->surface =
2629
_context->_glDisplay->makeWindowSurface(_context->_glConfig, _window);
30+
if (!releaseCtx->surface) {
31+
throw std::runtime_error("Failed to create window surface");
32+
}
2733
_glSurface = releaseCtx->surface.get();
2834

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

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

7584
void OpenGLWindowContext::present() {
76-
_context->_glContext->makeCurrent(*_glSurface);
85+
_context->_glContext->makeCurrent(_glSurface);
7786
_context->_directContext->flushAndSubmit();
7887
_glSurface->present();
7988
}

‎packages/skia/android/cpp/rnskia-android/OpenGLWindowContext.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ class OpenGLWindowContext : public WindowContext {
3737
public:
3838
OpenGLWindowContext(OpenGLContext *context, ANativeWindow *window, int width,
3939
int height)
40-
: _context(context), _window(window), _width(width), _height(height) {}
40+
: _context(context), _window(window), _width(width), _height(height) {
41+
ANativeWindow_acquire(_window);
42+
}
4143

42-
~OpenGLWindowContext() { ANativeWindow_release(_window); }
44+
~OpenGLWindowContext() {
45+
_skSurface = nullptr;
46+
_glSurface = nullptr;
47+
ANativeWindow_release(_window);
48+
}
4349

4450
sk_sp<SkSurface> getSurface() override;
4551

‎packages/skia/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
6868
#if defined(SK_GRAPHITE)
6969
return DawnContext::getInstance().MakeWindow(surface, width, height);
7070
#else
71-
return OpenGLContext::getInstance().MakeWindow(
72-
reinterpret_cast<ANativeWindow *>(surface), width, height);
71+
auto aWindow = reinterpret_cast<ANativeWindow *>(surface);
72+
return OpenGLContext::getInstance().MakeWindow(aWindow, width, height);
7373
#endif
7474
}
7575

‎packages/skia/android/cpp/rnskia-android/gl/Context.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ class Context {
2222

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

25-
bool makeCurrent(const Surface &surface) const {
25+
bool makeCurrent(const Surface *surface) {
2626
if (_context == EGL_NO_CONTEXT) {
2727
return false;
2828
}
2929
const auto result =
30-
eglMakeCurrentIfNecessary(_display, surface.getHandle(),
31-
surface.getHandle(), _context) == EGL_TRUE;
30+
eglMakeCurrentIfNecessary(_display, surface->getHandle(),
31+
surface->getHandle(), _context) == EGL_TRUE;
3232
if (!result) {
3333
LOG_EGL_ERROR;
3434
}
3535
return result;
3636
}
3737

38-
bool clearCurrent() const {
38+
bool clearCurrent() {
3939
const auto result =
4040
eglMakeCurrentIfNecessary(_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
4141
EGL_NO_CONTEXT) == EGL_TRUE;

‎packages/skia/android/cpp/rnskia-android/gl/Display.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Display {
3535

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

38-
EGLConfig chooseConfig() const {
38+
EGLConfig chooseConfig() {
3939

4040
EGLint att[] = {EGL_RENDERABLE_TYPE,
4141
EGL_OPENGL_ES2_BIT,
@@ -82,14 +82,14 @@ class Display {
8282
}
8383

8484
std::unique_ptr<Surface> makeWindowSurface(const EGLConfig &config,
85-
EGLNativeWindowType window) {
85+
ANativeWindow *window) {
8686
const EGLint attribs[] = {EGL_NONE};
8787
auto surface = eglCreateWindowSurface(_display, config, window, attribs);
8888
if (surface == EGL_NO_SURFACE) {
8989
LOG_EGL_ERROR;
9090
return nullptr;
9191
}
92-
return std::unique_ptr<Surface>(new Surface(_display, surface));
92+
return std::make_unique<Surface>(_display, surface);
9393
}
9494

9595
std::unique_ptr<Surface> makePixelBufferSurface(const EGLConfig &config,
@@ -101,7 +101,7 @@ class Display {
101101
LOG_EGL_ERROR;
102102
return nullptr;
103103
}
104-
return std::unique_ptr<Surface>(new Surface(_display, surface));
104+
return std::make_unique<Surface>(_display, surface);
105105
}
106106

107107
const EGLDisplay &getHandle() const { return _display; }

‎packages/skia/android/cpp/rnskia-android/gl/Surface.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace gl {
66

77
class Surface {
88
public:
9+
Surface(EGLDisplay display, EGLSurface surface)
10+
: _display(display), _surface(surface) {}
11+
912
~Surface() {
1013
if (_surface != EGL_NO_SURFACE) {
1114
if (eglDestroySurface(_display, _surface) != EGL_TRUE) {
@@ -18,7 +21,7 @@ class Surface {
1821

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

21-
bool present() const {
24+
bool present() {
2225
const auto result = eglSwapBuffers(_display, _surface) == EGL_TRUE;
2326
if (!result) {
2427
LOG_EGL_ERROR;
@@ -32,9 +35,6 @@ class Surface {
3235
EGLDisplay _display = EGL_NO_DISPLAY;
3336
EGLSurface _surface = EGL_NO_SURFACE;
3437

35-
Surface(EGLDisplay display, EGLSurface surface)
36-
: _display(display), _surface(surface) {}
37-
3838
Surface(const Surface &) = delete;
3939

4040
Surface &operator=(const Surface &) = delete;

‎packages/skia/cpp/api/JsiSkiaContext.h

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class JsiSkiaContext : public JsiSkWrappingSharedPtrHostObject<WindowContext> {
6969
void *surface = reinterpret_cast<void *>(nativeBufferPointer);
7070
auto width = static_cast<int>(arguments[1].asNumber());
7171
auto height = static_cast<int>(arguments[2].asNumber());
72+
if (surface == nullptr) {
73+
throw std::runtime_error("Surface is null");
74+
}
7275
auto result =
7376
context->makeContextFromNativeSurface(surface, width, height);
7477
// Return the newly constructed object

0 commit comments

Comments
 (0)