@shopify/react-native-skia 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +3 -3
- package/android/src/main/java/com/shopify/reactnative/skia/ViewScreenshotService.java +13 -8
- package/cpp/api/JsiNativeBuffer.h +43 -0
- package/cpp/api/JsiSkApi.h +3 -3
- package/cpp/api/JsiSkImageFactory.h +3 -3
- package/cpp/rnskia/RNSkPlatformContext.h +5 -5
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +3 -3
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +70 -93
- package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.h +119 -0
- package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +332 -0
- package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +2 -1
- package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +27 -6
- package/lib/commonjs/external/ModuleProxy.d.ts +10 -0
- package/lib/commonjs/external/ModuleProxy.js +36 -0
- package/lib/commonjs/external/ModuleProxy.js.map +1 -0
- package/lib/commonjs/external/reanimated/ReanimatedProxy.d.ts +3 -0
- package/lib/commonjs/external/reanimated/ReanimatedProxy.js +18 -0
- package/lib/commonjs/external/reanimated/ReanimatedProxy.js.map +1 -0
- package/lib/commonjs/external/reanimated/buffers.js +9 -6
- package/lib/commonjs/external/reanimated/buffers.js.map +1 -1
- package/lib/commonjs/external/reanimated/interpolators.js +8 -7
- package/lib/commonjs/external/reanimated/interpolators.js.map +1 -1
- package/lib/commonjs/external/reanimated/renderHelpers.js +27 -11
- package/lib/commonjs/external/reanimated/renderHelpers.js.map +1 -1
- package/lib/commonjs/external/reanimated/textures.js +4 -3
- package/lib/commonjs/external/reanimated/textures.js.map +1 -1
- package/lib/commonjs/external/reanimated/useAnimatedImageValue.js +6 -6
- package/lib/commonjs/external/reanimated/useAnimatedImageValue.js.map +1 -1
- package/lib/commonjs/external/reanimated/useDerivedValueOnJS.js +6 -5
- package/lib/commonjs/external/reanimated/useDerivedValueOnJS.js.map +1 -1
- package/lib/commonjs/renderer/Offscreen.js +1 -5
- package/lib/commonjs/renderer/Offscreen.js.map +1 -1
- package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +8 -15
- package/lib/commonjs/skia/types/Image/ImageFactory.js +1 -10
- package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
- package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.d.ts +18 -0
- package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.js +13 -0
- package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -0
- package/lib/commonjs/skia/types/NativeBuffer/index.d.ts +1 -0
- package/lib/commonjs/skia/types/NativeBuffer/index.js +17 -0
- package/lib/commonjs/skia/types/NativeBuffer/index.js.map +1 -0
- package/lib/commonjs/skia/types/Skia.d.ts +2 -2
- package/lib/commonjs/skia/types/Skia.js.map +1 -1
- package/lib/commonjs/skia/types/index.d.ts +1 -1
- package/lib/commonjs/skia/types/index.js +4 -4
- package/lib/commonjs/skia/types/index.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +3 -2
- package/lib/commonjs/skia/web/JsiSkImageFactory.js +18 -2
- package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkNativeBufferFactory.d.ts +8 -0
- package/lib/commonjs/skia/web/JsiSkNativeBufferFactory.js +29 -0
- package/lib/commonjs/skia/web/JsiSkNativeBufferFactory.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkia.js +2 -2
- package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
- package/lib/commonjs/specs/NativeSkiaModule.web.d.ts +0 -2
- package/lib/commonjs/specs/NativeSkiaModule.web.js +0 -8
- package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
- package/lib/module/external/ModuleProxy.d.ts +10 -0
- package/lib/module/external/ModuleProxy.js +28 -0
- package/lib/module/external/ModuleProxy.js.map +1 -0
- package/lib/module/external/reanimated/ReanimatedProxy.d.ts +3 -0
- package/lib/module/external/reanimated/ReanimatedProxy.js +12 -0
- package/lib/module/external/reanimated/ReanimatedProxy.js.map +1 -0
- package/lib/module/external/reanimated/buffers.js +8 -6
- package/lib/module/external/reanimated/buffers.js.map +1 -1
- package/lib/module/external/reanimated/interpolators.js +7 -7
- package/lib/module/external/reanimated/interpolators.js.map +1 -1
- package/lib/module/external/reanimated/renderHelpers.js +26 -11
- package/lib/module/external/reanimated/renderHelpers.js.map +1 -1
- package/lib/module/external/reanimated/textures.js +3 -3
- package/lib/module/external/reanimated/textures.js.map +1 -1
- package/lib/module/external/reanimated/useAnimatedImageValue.js +5 -6
- package/lib/module/external/reanimated/useAnimatedImageValue.js.map +1 -1
- package/lib/module/external/reanimated/useDerivedValueOnJS.js +5 -5
- package/lib/module/external/reanimated/useDerivedValueOnJS.js.map +1 -1
- package/lib/module/renderer/Offscreen.js +1 -5
- package/lib/module/renderer/Offscreen.js.map +1 -1
- package/lib/module/skia/types/Image/ImageFactory.d.ts +8 -15
- package/lib/module/skia/types/Image/ImageFactory.js +1 -10
- package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
- package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.d.ts +18 -0
- package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.js +4 -0
- package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -0
- package/lib/module/skia/types/NativeBuffer/index.d.ts +1 -0
- package/lib/module/skia/types/NativeBuffer/index.js +2 -0
- package/lib/module/skia/types/NativeBuffer/index.js.map +1 -0
- package/lib/module/skia/types/Skia.d.ts +2 -2
- package/lib/module/skia/types/Skia.js.map +1 -1
- package/lib/module/skia/types/index.d.ts +1 -1
- package/lib/module/skia/types/index.js +1 -1
- package/lib/module/skia/types/index.js.map +1 -1
- package/lib/module/skia/web/JsiSkImageFactory.d.ts +3 -2
- package/lib/module/skia/web/JsiSkImageFactory.js +18 -2
- package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
- package/lib/module/skia/web/JsiSkNativeBufferFactory.d.ts +8 -0
- package/lib/module/skia/web/JsiSkNativeBufferFactory.js +22 -0
- package/lib/module/skia/web/JsiSkNativeBufferFactory.js.map +1 -0
- package/lib/module/skia/web/JsiSkia.js +2 -2
- package/lib/module/skia/web/JsiSkia.js.map +1 -1
- package/lib/module/specs/NativeSkiaModule.web.d.ts +0 -2
- package/lib/module/specs/NativeSkiaModule.web.js +0 -3
- package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
- package/lib/typescript/src/external/ModuleProxy.d.ts +10 -0
- package/lib/typescript/src/external/reanimated/ReanimatedProxy.d.ts +3 -0
- package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +8 -15
- package/lib/typescript/src/skia/types/NativeBuffer/NativeBufferFactory.d.ts +18 -0
- package/lib/typescript/src/skia/types/NativeBuffer/index.d.ts +1 -0
- package/lib/typescript/src/skia/types/Skia.d.ts +2 -2
- package/lib/typescript/src/skia/types/index.d.ts +1 -1
- package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +3 -2
- package/lib/typescript/src/skia/web/JsiSkNativeBufferFactory.d.ts +8 -0
- package/lib/typescript/src/specs/NativeSkiaModule.web.d.ts +0 -2
- package/libs/ios/libskia.xcframework/Info.plist +5 -5
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
- package/libs/ios/libskottie.xcframework/Info.plist +5 -5
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
- package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e/libskparagraph.a +0 -0
- package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e_x86_64-simulator/libskparagraph.a +0 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
- package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e/libskunicode.a +0 -0
- package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode.a +0 -0
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
- package/package.json +1 -1
- package/src/external/ModuleProxy.ts +30 -0
- package/src/external/reanimated/ReanimatedProxy.ts +18 -0
- package/src/external/reanimated/buffers.ts +7 -6
- package/src/external/reanimated/interpolators.ts +7 -12
- package/src/external/reanimated/renderHelpers.ts +31 -18
- package/src/external/reanimated/textures.tsx +3 -3
- package/src/external/reanimated/useAnimatedImageValue.ts +5 -10
- package/src/external/reanimated/useDerivedValueOnJS.ts +5 -10
- package/src/renderer/Offscreen.tsx +1 -7
- package/src/skia/types/Image/ImageFactory.ts +7 -23
- package/src/skia/types/NativeBuffer/NativeBufferFactory.ts +38 -0
- package/src/skia/types/NativeBuffer/index.ts +1 -0
- package/src/skia/types/Skia.ts +2 -2
- package/src/skia/types/index.ts +1 -1
- package/src/skia/web/JsiSkImageFactory.ts +28 -6
- package/src/skia/web/JsiSkNativeBufferFactory.ts +35 -0
- package/src/skia/web/JsiSkia.ts +2 -2
- package/src/specs/NativeSkiaModule.web.ts +0 -4
- package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDrawViewManagerDelegate.java +0 -34
- package/cpp/api/JsiPlatformBuffer.h +0 -51
- package/cpp/skia/include/gpu/GrBackendDrawableInfo.h +0 -9
- package/lib/commonjs/external/reanimated/moduleWrapper.d.ts +0 -15
- package/lib/commonjs/external/reanimated/moduleWrapper.js +0 -46
- package/lib/commonjs/external/reanimated/moduleWrapper.js.map +0 -1
- package/lib/commonjs/skia/types/PlatformBuffer/PlatformBufferFactory.d.ts +0 -12
- package/lib/commonjs/skia/types/PlatformBuffer/PlatformBufferFactory.js +0 -6
- package/lib/commonjs/skia/types/PlatformBuffer/PlatformBufferFactory.js.map +0 -1
- package/lib/commonjs/skia/types/PlatformBuffer/index.d.ts +0 -1
- package/lib/commonjs/skia/types/PlatformBuffer/index.js +0 -17
- package/lib/commonjs/skia/types/PlatformBuffer/index.js.map +0 -1
- package/lib/commonjs/skia/web/JsiSkPlatformBufferFactory.d.ts +0 -8
- package/lib/commonjs/skia/web/JsiSkPlatformBufferFactory.js +0 -20
- package/lib/commonjs/skia/web/JsiSkPlatformBufferFactory.js.map +0 -1
- package/lib/module/external/reanimated/moduleWrapper.d.ts +0 -15
- package/lib/module/external/reanimated/moduleWrapper.js +0 -37
- package/lib/module/external/reanimated/moduleWrapper.js.map +0 -1
- package/lib/module/skia/types/PlatformBuffer/PlatformBufferFactory.d.ts +0 -12
- package/lib/module/skia/types/PlatformBuffer/PlatformBufferFactory.js +0 -2
- package/lib/module/skia/types/PlatformBuffer/PlatformBufferFactory.js.map +0 -1
- package/lib/module/skia/types/PlatformBuffer/index.d.ts +0 -1
- package/lib/module/skia/types/PlatformBuffer/index.js +0 -2
- package/lib/module/skia/types/PlatformBuffer/index.js.map +0 -1
- package/lib/module/skia/web/JsiSkPlatformBufferFactory.d.ts +0 -8
- package/lib/module/skia/web/JsiSkPlatformBufferFactory.js +0 -13
- package/lib/module/skia/web/JsiSkPlatformBufferFactory.js.map +0 -1
- package/lib/typescript/src/external/reanimated/moduleWrapper.d.ts +0 -15
- package/lib/typescript/src/skia/types/PlatformBuffer/PlatformBufferFactory.d.ts +0 -12
- package/lib/typescript/src/skia/types/PlatformBuffer/index.d.ts +0 -1
- package/lib/typescript/src/skia/web/JsiSkPlatformBufferFactory.d.ts +0 -8
- package/src/external/reanimated/moduleWrapper.ts +0 -83
- package/src/skia/types/PlatformBuffer/PlatformBufferFactory.ts +0 -14
- package/src/skia/types/PlatformBuffer/index.ts +0 -1
- package/src/skia/web/JsiSkPlatformBufferFactory.ts +0 -22
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
//
|
|
2
|
+
// SkiaCVPixelBufferUtils.mm
|
|
3
|
+
// react-native-skia
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 10.04.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import "SkiaCVPixelBufferUtils.h"
|
|
9
|
+
#import "SkiaMetalSurfaceFactory.h"
|
|
10
|
+
|
|
11
|
+
#pragma clang diagnostic push
|
|
12
|
+
#pragma clang diagnostic ignored "-Wdocumentation"
|
|
13
|
+
#import "include/core/SkColorSpace.h"
|
|
14
|
+
#import <include/gpu/GrBackendSurface.h>
|
|
15
|
+
#import <include/gpu/ganesh/SkImageGanesh.h>
|
|
16
|
+
#pragma clang diagnostic pop
|
|
17
|
+
|
|
18
|
+
#include <TargetConditionals.h>
|
|
19
|
+
#if TARGET_RT_BIG_ENDIAN
|
|
20
|
+
#define FourCC2Str(fourcc) \
|
|
21
|
+
(const char[]) { \
|
|
22
|
+
*((char *)&fourcc), *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 2), \
|
|
23
|
+
*(((char *)&fourcc) + 3), 0 \
|
|
24
|
+
}
|
|
25
|
+
#else
|
|
26
|
+
#define FourCC2Str(fourcc) \
|
|
27
|
+
(const char[]) { \
|
|
28
|
+
*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
|
|
29
|
+
*(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0 \
|
|
30
|
+
}
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
// pragma MARK: TextureHolder
|
|
34
|
+
|
|
35
|
+
TextureHolder::TextureHolder(CVMetalTextureRef texture) : _texture(texture) {}
|
|
36
|
+
TextureHolder::~TextureHolder() {
|
|
37
|
+
// ARC will now automatically release _texture.
|
|
38
|
+
CFRelease(_texture);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
GrBackendTexture TextureHolder::toGrBackendTexture() {
|
|
42
|
+
// Unwrap the underlying MTLTexture
|
|
43
|
+
id<MTLTexture> mtlTexture = CVMetalTextureGetTexture(_texture);
|
|
44
|
+
if (mtlTexture == nil) [[unlikely]] {
|
|
45
|
+
throw std::runtime_error(
|
|
46
|
+
"Failed to get MTLTexture from CVMetalTextureRef!");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Wrap MTLTexture in Skia's GrBackendTexture
|
|
50
|
+
GrMtlTextureInfo textureInfo;
|
|
51
|
+
textureInfo.fTexture.retain((__bridge void *)mtlTexture);
|
|
52
|
+
GrBackendTexture texture =
|
|
53
|
+
GrBackendTexture((int)mtlTexture.width, (int)mtlTexture.height,
|
|
54
|
+
skgpu::Mipmapped::kNo, textureInfo);
|
|
55
|
+
return texture;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
MultiTexturesHolder::~MultiTexturesHolder() {
|
|
59
|
+
for (TextureHolder *texture : _textures) {
|
|
60
|
+
delete texture;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void MultiTexturesHolder::addTexture(TextureHolder *texture) {
|
|
65
|
+
_textures.push_back(texture);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// pragma MARK: Base
|
|
69
|
+
|
|
70
|
+
SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat
|
|
71
|
+
SkiaCVPixelBufferUtils::getCVPixelBufferBaseFormat(
|
|
72
|
+
CVPixelBufferRef pixelBuffer) {
|
|
73
|
+
OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
|
|
74
|
+
|
|
75
|
+
switch (format) {
|
|
76
|
+
// 8-bit YUV formats
|
|
77
|
+
case kCVPixelFormatType_420YpCbCr8Planar:
|
|
78
|
+
case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
|
|
79
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
80
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
81
|
+
// 10-bit YUV formats
|
|
82
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
83
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
84
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange:
|
|
85
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarFullRange:
|
|
86
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange:
|
|
87
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarFullRange:
|
|
88
|
+
return CVPixelBufferBaseFormat::yuv;
|
|
89
|
+
case kCVPixelFormatType_24RGB:
|
|
90
|
+
case kCVPixelFormatType_24BGR:
|
|
91
|
+
case kCVPixelFormatType_32ARGB:
|
|
92
|
+
case kCVPixelFormatType_32BGRA:
|
|
93
|
+
case kCVPixelFormatType_32ABGR:
|
|
94
|
+
case kCVPixelFormatType_32RGBA:
|
|
95
|
+
return CVPixelBufferBaseFormat::rgb;
|
|
96
|
+
default:
|
|
97
|
+
[[unlikely]] throw std::runtime_error(
|
|
98
|
+
"CVPixelBuffer has unknown pixel format! " +
|
|
99
|
+
std::string("Expected: any RGB or YUV format, Received: ") +
|
|
100
|
+
std::string(FourCC2Str(format)));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// pragma MARK: RGB
|
|
105
|
+
|
|
106
|
+
SkColorType SkiaCVPixelBufferUtils::RGB::getCVPixelBufferColorType(
|
|
107
|
+
CVPixelBufferRef pixelBuffer) {
|
|
108
|
+
OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
|
|
109
|
+
|
|
110
|
+
switch (format) {
|
|
111
|
+
case kCVPixelFormatType_32BGRA:
|
|
112
|
+
[[likely]] return kBGRA_8888_SkColorType;
|
|
113
|
+
case kCVPixelFormatType_32RGBA:
|
|
114
|
+
return kRGBA_8888_SkColorType;
|
|
115
|
+
// This can be extended with branches for specific RGB formats if Apple
|
|
116
|
+
// uses new formats.
|
|
117
|
+
default:
|
|
118
|
+
[[unlikely]] throw std::runtime_error(
|
|
119
|
+
"CVPixelBuffer has unknown RGB format! " +
|
|
120
|
+
std::string(FourCC2Str(format)));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
sk_sp<SkImage> SkiaCVPixelBufferUtils::RGB::makeSkImageFromCVPixelBuffer(
|
|
125
|
+
GrDirectContext *context, CVPixelBufferRef pixelBuffer) {
|
|
126
|
+
// 1. Get Skia color type for RGB buffer
|
|
127
|
+
SkColorType colorType = getCVPixelBufferColorType(pixelBuffer);
|
|
128
|
+
|
|
129
|
+
// 2. Get texture, RGB buffers only have one plane
|
|
130
|
+
TextureHolder *texture =
|
|
131
|
+
getSkiaTextureForCVPixelBufferPlane(pixelBuffer, /* planeIndex */ 0);
|
|
132
|
+
|
|
133
|
+
// 3. Convert to image with manual memory cleanup
|
|
134
|
+
return SkImages::BorrowTextureFrom(
|
|
135
|
+
context, texture->toGrBackendTexture(), kTopLeft_GrSurfaceOrigin,
|
|
136
|
+
colorType, kOpaque_SkAlphaType, nullptr,
|
|
137
|
+
[](void *texture) { delete (TextureHolder *)texture; }, (void *)texture);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// pragma MARK: YUV
|
|
141
|
+
|
|
142
|
+
sk_sp<SkImage> SkiaCVPixelBufferUtils::YUV::makeSkImageFromCVPixelBuffer(
|
|
143
|
+
GrDirectContext *context, CVPixelBufferRef pixelBuffer) {
|
|
144
|
+
// 1. Get all planes (YUV, Y_UV, Y_U_V or Y_U_V_A)
|
|
145
|
+
size_t planesCount = CVPixelBufferGetPlaneCount(pixelBuffer);
|
|
146
|
+
MultiTexturesHolder *texturesHolder = new MultiTexturesHolder();
|
|
147
|
+
GrBackendTexture textures[planesCount];
|
|
148
|
+
|
|
149
|
+
for (size_t planeIndex = 0; planeIndex < planesCount; planeIndex++) {
|
|
150
|
+
TextureHolder *textureHolder =
|
|
151
|
+
getSkiaTextureForCVPixelBufferPlane(pixelBuffer, planeIndex);
|
|
152
|
+
textures[planeIndex] = textureHolder->toGrBackendTexture();
|
|
153
|
+
texturesHolder->addTexture(textureHolder);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// 2. Wrap info about buffer
|
|
157
|
+
SkYUVAInfo info = getYUVAInfoForCVPixelBuffer(pixelBuffer);
|
|
158
|
+
|
|
159
|
+
// 3. Wrap as YUVA textures type
|
|
160
|
+
GrYUVABackendTextures yuvaTextures(info, textures, kTopLeft_GrSurfaceOrigin);
|
|
161
|
+
|
|
162
|
+
// 4. Wrap into SkImage type with manualy memory cleanup
|
|
163
|
+
return SkImages::TextureFromYUVATextures(
|
|
164
|
+
context, yuvaTextures, nullptr,
|
|
165
|
+
[](void *textureHolders) {
|
|
166
|
+
delete (MultiTexturesHolder *)textureHolders;
|
|
167
|
+
},
|
|
168
|
+
(void *)texturesHolder);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
SkYUVAInfo SkiaCVPixelBufferUtils::YUV::getYUVAInfoForCVPixelBuffer(
|
|
172
|
+
CVPixelBufferRef pixelBuffer) {
|
|
173
|
+
size_t width = CVPixelBufferGetWidth(pixelBuffer);
|
|
174
|
+
size_t height = CVPixelBufferGetHeight(pixelBuffer);
|
|
175
|
+
SkISize size =
|
|
176
|
+
SkISize::Make(static_cast<int>(width), static_cast<int>(height));
|
|
177
|
+
|
|
178
|
+
OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
|
|
179
|
+
SkYUVAInfo::PlaneConfig planeConfig = getPlaneConfig(format);
|
|
180
|
+
SkYUVAInfo::Subsampling subsampling = getSubsampling(format);
|
|
181
|
+
SkYUVColorSpace colorspace = getColorspace(format);
|
|
182
|
+
|
|
183
|
+
return SkYUVAInfo(size, planeConfig, subsampling, colorspace);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// pragma MARK: YUV getPlaneConfig()
|
|
187
|
+
|
|
188
|
+
SkYUVAInfo::PlaneConfig
|
|
189
|
+
SkiaCVPixelBufferUtils::YUV::getPlaneConfig(OSType pixelFormat) {
|
|
190
|
+
switch (pixelFormat) {
|
|
191
|
+
case kCVPixelFormatType_420YpCbCr8Planar:
|
|
192
|
+
case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
|
|
193
|
+
return SkYUVAInfo::PlaneConfig::kYUV;
|
|
194
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
195
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
196
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
197
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
198
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange:
|
|
199
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarFullRange:
|
|
200
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange:
|
|
201
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarFullRange:
|
|
202
|
+
[[likely]] return SkYUVAInfo::PlaneConfig::kY_UV;
|
|
203
|
+
|
|
204
|
+
// This can be extended with branches for specific YUV formats if Apple
|
|
205
|
+
// uses new formats.
|
|
206
|
+
default:
|
|
207
|
+
[[unlikely]] throw std::runtime_error("Invalid pixel format! " +
|
|
208
|
+
std::string(FourCC2Str(pixelFormat)));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// pragma MARK: YUV getSubsampling()
|
|
213
|
+
|
|
214
|
+
SkYUVAInfo::Subsampling
|
|
215
|
+
SkiaCVPixelBufferUtils::YUV::getSubsampling(OSType pixelFormat) {
|
|
216
|
+
switch (pixelFormat) {
|
|
217
|
+
case kCVPixelFormatType_420YpCbCr8Planar:
|
|
218
|
+
case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
|
|
219
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
220
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
221
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
222
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
223
|
+
[[likely]] return SkYUVAInfo::Subsampling::k420;
|
|
224
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange:
|
|
225
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarFullRange:
|
|
226
|
+
return SkYUVAInfo::Subsampling::k422;
|
|
227
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange:
|
|
228
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarFullRange:
|
|
229
|
+
return SkYUVAInfo::Subsampling::k444;
|
|
230
|
+
// This can be extended with branches for specific YUV formats if Apple
|
|
231
|
+
// uses new formats.
|
|
232
|
+
default:
|
|
233
|
+
[[unlikely]] throw std::runtime_error("Invalid pixel format! " +
|
|
234
|
+
std::string(FourCC2Str(pixelFormat)));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// pragma MARK: YUV getColorspace()
|
|
239
|
+
|
|
240
|
+
SkYUVColorSpace SkiaCVPixelBufferUtils::YUV::getColorspace(OSType pixelFormat) {
|
|
241
|
+
switch (pixelFormat) {
|
|
242
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
243
|
+
[[likely]]
|
|
244
|
+
// 8-bit limited
|
|
245
|
+
return SkYUVColorSpace::kRec709_Limited_SkYUVColorSpace;
|
|
246
|
+
case kCVPixelFormatType_420YpCbCr8Planar:
|
|
247
|
+
case kCVPixelFormatType_420YpCbCr8PlanarFullRange:
|
|
248
|
+
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
249
|
+
[[likely]]
|
|
250
|
+
// 8-bit full
|
|
251
|
+
return SkYUVColorSpace::kRec709_Full_SkYUVColorSpace;
|
|
252
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
253
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange:
|
|
254
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange:
|
|
255
|
+
// 10-bit limited
|
|
256
|
+
return SkYUVColorSpace::kBT2020_10bit_Limited_SkYUVColorSpace;
|
|
257
|
+
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
258
|
+
case kCVPixelFormatType_422YpCbCr10BiPlanarFullRange:
|
|
259
|
+
case kCVPixelFormatType_444YpCbCr10BiPlanarFullRange:
|
|
260
|
+
// 10-bit full
|
|
261
|
+
return SkYUVColorSpace::kBT2020_10bit_Full_SkYUVColorSpace;
|
|
262
|
+
// This can be extended with branches for specific YUV formats if Apple
|
|
263
|
+
// uses new formats.
|
|
264
|
+
default:
|
|
265
|
+
[[unlikely]] throw std::runtime_error("Invalid pixel format! " +
|
|
266
|
+
std::string(FourCC2Str(pixelFormat)));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// pragma MARK: CVPixelBuffer -> Skia Texture
|
|
271
|
+
|
|
272
|
+
TextureHolder *SkiaCVPixelBufferUtils::getSkiaTextureForCVPixelBufferPlane(
|
|
273
|
+
CVPixelBufferRef pixelBuffer, size_t planeIndex) {
|
|
274
|
+
// 1. Get cache
|
|
275
|
+
CVMetalTextureCacheRef textureCache = getTextureCache();
|
|
276
|
+
|
|
277
|
+
// 2. Get MetalTexture from CMSampleBuffer
|
|
278
|
+
size_t width = CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex);
|
|
279
|
+
size_t height = CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex);
|
|
280
|
+
MTLPixelFormat pixelFormat =
|
|
281
|
+
getMTLPixelFormatForCVPixelBufferPlane(pixelBuffer, planeIndex);
|
|
282
|
+
|
|
283
|
+
CVMetalTextureRef textureHolder;
|
|
284
|
+
CVReturn result = CVMetalTextureCacheCreateTextureFromImage(
|
|
285
|
+
kCFAllocatorDefault, textureCache, pixelBuffer, nil, pixelFormat, width,
|
|
286
|
+
height, planeIndex, &textureHolder);
|
|
287
|
+
|
|
288
|
+
if (result != kCVReturnSuccess) [[unlikely]] {
|
|
289
|
+
throw std::runtime_error(
|
|
290
|
+
"Failed to create Metal Texture from CMSampleBuffer! Result: " +
|
|
291
|
+
std::to_string(result));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return new TextureHolder(textureHolder);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// pragma MARK: getTextureCache()
|
|
298
|
+
|
|
299
|
+
CVMetalTextureCacheRef SkiaCVPixelBufferUtils::getTextureCache() {
|
|
300
|
+
static CVMetalTextureCacheRef textureCache = nil;
|
|
301
|
+
if (textureCache == nil) {
|
|
302
|
+
// Create a new Texture Cache
|
|
303
|
+
auto result = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil,
|
|
304
|
+
MTLCreateSystemDefaultDevice(), nil,
|
|
305
|
+
&textureCache);
|
|
306
|
+
if (result != kCVReturnSuccess || textureCache == nil) {
|
|
307
|
+
throw std::runtime_error("Failed to create Metal Texture Cache!");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return textureCache;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// pragma MARK: Get CVPixelBuffer MTLPixelFormat
|
|
314
|
+
|
|
315
|
+
MTLPixelFormat SkiaCVPixelBufferUtils::getMTLPixelFormatForCVPixelBufferPlane(
|
|
316
|
+
CVPixelBufferRef pixelBuffer, size_t planeIndex) {
|
|
317
|
+
size_t width = CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex);
|
|
318
|
+
size_t bytesPerRow =
|
|
319
|
+
CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, planeIndex);
|
|
320
|
+
double bytesPerPixel = round(static_cast<double>(bytesPerRow) / width);
|
|
321
|
+
if (bytesPerPixel == 1) {
|
|
322
|
+
return MTLPixelFormatR8Unorm;
|
|
323
|
+
} else if (bytesPerPixel == 2) {
|
|
324
|
+
return MTLPixelFormatRG8Unorm;
|
|
325
|
+
} else if (bytesPerPixel == 4) {
|
|
326
|
+
return MTLPixelFormatBGRA8Unorm;
|
|
327
|
+
} else [[unlikely]] {
|
|
328
|
+
throw std::runtime_error("Invalid bytes per row! Expected 1 (R), 2 (RG) or "
|
|
329
|
+
"4 (RGBA), but received " +
|
|
330
|
+
std::to_string(bytesPerPixel));
|
|
331
|
+
}
|
|
332
|
+
}
|
|
@@ -26,7 +26,8 @@ public:
|
|
|
26
26
|
int height);
|
|
27
27
|
static sk_sp<SkSurface> makeOffscreenSurface(int width, int height);
|
|
28
28
|
|
|
29
|
-
static sk_sp<SkImage>
|
|
29
|
+
static sk_sp<SkImage>
|
|
30
|
+
makeTextureFromCVPixelBuffer(CVPixelBufferRef pixelBuffer);
|
|
30
31
|
|
|
31
32
|
private:
|
|
32
33
|
static id<MTLDevice> device;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#import "RNSkLog.h"
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
#import "SkiaCVPixelBufferUtils.h"
|
|
4
|
+
#import "SkiaMetalSurfaceFactory.h"
|
|
4
5
|
|
|
5
6
|
#pragma clang diagnostic push
|
|
6
7
|
#pragma clang diagnostic ignored "-Wdocumentation"
|
|
@@ -106,12 +107,32 @@ sk_sp<SkSurface> SkiaMetalSurfaceFactory::makeOffscreenSurface(int width,
|
|
|
106
107
|
return surface;
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
sk_sp<SkImage>
|
|
110
|
-
|
|
110
|
+
sk_sp<SkImage> SkiaMetalSurfaceFactory::makeTextureFromCVPixelBuffer(
|
|
111
|
+
CVPixelBufferRef pixelBuffer) {
|
|
111
112
|
if (!SkiaMetalSurfaceFactory::createSkiaDirectContextIfNecessary(
|
|
112
|
-
&ThreadContextHolder::ThreadSkiaMetalContext)) {
|
|
113
|
+
&ThreadContextHolder::ThreadSkiaMetalContext)) [[unlikely]] {
|
|
113
114
|
throw std::runtime_error("Failed to create Skia Context for this Thread!");
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
const SkiaMetalContext &context = ThreadContextHolder::ThreadSkiaMetalContext;
|
|
117
|
+
|
|
118
|
+
SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat format =
|
|
119
|
+
SkiaCVPixelBufferUtils::getCVPixelBufferBaseFormat(pixelBuffer);
|
|
120
|
+
switch (format) {
|
|
121
|
+
case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::rgb: {
|
|
122
|
+
// CVPixelBuffer is in any RGB format, single-plane
|
|
123
|
+
return SkiaCVPixelBufferUtils::RGB::makeSkImageFromCVPixelBuffer(
|
|
124
|
+
context.skContext.get(), pixelBuffer);
|
|
125
|
+
}
|
|
126
|
+
case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::yuv: {
|
|
127
|
+
// CVPixelBuffer is in any YUV format, multi-plane
|
|
128
|
+
return SkiaCVPixelBufferUtils::YUV::makeSkImageFromCVPixelBuffer(
|
|
129
|
+
context.skContext.get(), pixelBuffer);
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
[[unlikely]] {
|
|
133
|
+
throw std::runtime_error("Failed to convert PlatformBuffer to SkImage - "
|
|
134
|
+
"PlatformBuffer has unsupported PixelFormat! " +
|
|
135
|
+
std::to_string(static_cast<int>(format)));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
117
138
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type ImportType = ReturnType<typeof require>;
|
|
2
|
+
/**
|
|
3
|
+
* Create a lazily-imported module proxy.
|
|
4
|
+
* This is useful for lazily requiring optional dependencies.
|
|
5
|
+
*/
|
|
6
|
+
export declare const createModuleProxy: <TModule>(getModule: () => ImportType) => TModule;
|
|
7
|
+
export declare class OptionalDependencyNotInstalledError extends Error {
|
|
8
|
+
constructor(name: string);
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createModuleProxy = exports.OptionalDependencyNotInstalledError = void 0;
|
|
7
|
+
// https://github.com/mrousavy/react-native-vision-camera/blob/main/package/src/dependencies/ModuleProxy.ts
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create a lazily-imported module proxy.
|
|
11
|
+
* This is useful for lazily requiring optional dependencies.
|
|
12
|
+
*/
|
|
13
|
+
const createModuleProxy = getModule => {
|
|
14
|
+
const holder = {
|
|
15
|
+
module: undefined
|
|
16
|
+
};
|
|
17
|
+
const proxy = new Proxy(holder, {
|
|
18
|
+
get: (target, property) => {
|
|
19
|
+
if (target.module == null) {
|
|
20
|
+
// lazy initialize module via require()
|
|
21
|
+
// caller needs to make sure the require() call is wrapped in a try/catch
|
|
22
|
+
target.module = getModule();
|
|
23
|
+
}
|
|
24
|
+
return target.module[property];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return proxy;
|
|
28
|
+
};
|
|
29
|
+
exports.createModuleProxy = createModuleProxy;
|
|
30
|
+
class OptionalDependencyNotInstalledError extends Error {
|
|
31
|
+
constructor(name) {
|
|
32
|
+
super(`${name} is not installed!`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.OptionalDependencyNotInstalledError = OptionalDependencyNotInstalledError;
|
|
36
|
+
//# sourceMappingURL=ModuleProxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createModuleProxy","getModule","holder","module","undefined","proxy","Proxy","get","target","property","exports","OptionalDependencyNotInstalledError","Error","constructor","name"],"sources":["ModuleProxy.ts"],"sourcesContent":["// https://github.com/mrousavy/react-native-vision-camera/blob/main/package/src/dependencies/ModuleProxy.ts\ntype ImportType = ReturnType<typeof require>;\n\n/**\n * Create a lazily-imported module proxy.\n * This is useful for lazily requiring optional dependencies.\n */\nexport const createModuleProxy = <TModule>(\n getModule: () => ImportType\n): TModule => {\n const holder: { module: TModule | undefined } = { module: undefined };\n\n const proxy = new Proxy(holder, {\n get: (target, property) => {\n if (target.module == null) {\n // lazy initialize module via require()\n // caller needs to make sure the require() call is wrapped in a try/catch\n target.module = getModule() as TModule;\n }\n return target.module[property as keyof typeof holder.module];\n },\n });\n return proxy as unknown as TModule;\n};\n\nexport class OptionalDependencyNotInstalledError extends Error {\n constructor(name: string) {\n super(`${name} is not installed!`);\n }\n}\n"],"mappings":";;;;;;AAAA;;AAGA;AACA;AACA;AACA;AACO,MAAMA,iBAAiB,GAC5BC,SAA2B,IACf;EACZ,MAAMC,MAAuC,GAAG;IAAEC,MAAM,EAAEC;EAAU,CAAC;EAErE,MAAMC,KAAK,GAAG,IAAIC,KAAK,CAACJ,MAAM,EAAE;IAC9BK,GAAG,EAAEA,CAACC,MAAM,EAAEC,QAAQ,KAAK;MACzB,IAAID,MAAM,CAACL,MAAM,IAAI,IAAI,EAAE;QACzB;QACA;QACAK,MAAM,CAACL,MAAM,GAAGF,SAAS,CAAC,CAAY;MACxC;MACA,OAAOO,MAAM,CAACL,MAAM,CAACM,QAAQ,CAA+B;IAC9D;EACF,CAAC,CAAC;EACF,OAAOJ,KAAK;AACd,CAAC;AAACK,OAAA,CAAAV,iBAAA,GAAAA,iBAAA;AAEK,MAAMW,mCAAmC,SAASC,KAAK,CAAC;EAC7DC,WAAWA,CAACC,IAAY,EAAE;IACxB,KAAK,CAAE,GAAEA,IAAK,oBAAmB,CAAC;EACpC;AACF;AAACJ,OAAA,CAAAC,mCAAA,GAAAA,mCAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _ModuleProxy = require("../ModuleProxy");
|
|
8
|
+
const Reanimated = (0, _ModuleProxy.createModuleProxy)(() => {
|
|
9
|
+
try {
|
|
10
|
+
return require("react-native-reanimated");
|
|
11
|
+
} catch (e) {
|
|
12
|
+
throw new _ModuleProxy.OptionalDependencyNotInstalledError("react-native-reanimated");
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line import/no-default-export
|
|
17
|
+
var _default = exports.default = Reanimated;
|
|
18
|
+
//# sourceMappingURL=ReanimatedProxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_ModuleProxy","require","Reanimated","createModuleProxy","e","OptionalDependencyNotInstalledError","_default","exports","default"],"sources":["ReanimatedProxy.ts"],"sourcesContent":["import type * as ReanimatedT from \"react-native-reanimated\";\n\nimport {\n OptionalDependencyNotInstalledError,\n createModuleProxy,\n} from \"../ModuleProxy\";\ntype TReanimated = typeof ReanimatedT;\n\nconst Reanimated = createModuleProxy<TReanimated>(() => {\n try {\n return require(\"react-native-reanimated\");\n } catch (e) {\n throw new OptionalDependencyNotInstalledError(\"react-native-reanimated\");\n }\n});\n\n// eslint-disable-next-line import/no-default-export\nexport default Reanimated;\n"],"mappings":";;;;;;AAEA,IAAAA,YAAA,GAAAC,OAAA;AAMA,MAAMC,UAAU,GAAG,IAAAC,8BAAiB,EAAc,MAAM;EACtD,IAAI;IACF,OAAOF,OAAO,CAAC,yBAAyB,CAAC;EAC3C,CAAC,CAAC,OAAOG,CAAC,EAAE;IACV,MAAM,IAAIC,gDAAmC,CAAC,yBAAyB,CAAC;EAC1E;AACF,CAAC,CAAC;;AAEF;AAAA,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GACeN,UAAU"}
|
|
@@ -6,17 +6,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.useRectBuffer = exports.useRSXformBuffer = exports.usePointBuffer = exports.useColorBuffer = void 0;
|
|
7
7
|
var _react = require("react");
|
|
8
8
|
var _skia = require("../../skia");
|
|
9
|
-
var _moduleWrapper = require("./moduleWrapper");
|
|
10
9
|
var _interpolators = require("./interpolators");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
var _ReanimatedProxy = _interopRequireDefault(require("./ReanimatedProxy"));
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
const useBufferValue = (size, bufferInitializer) => {
|
|
13
|
+
return (0, _react.useMemo)(() => _ReanimatedProxy.default.makeMutable(new Array(size).fill(0).map(bufferInitializer)),
|
|
14
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
15
|
+
[size]);
|
|
16
|
+
};
|
|
14
17
|
const useBuffer = (size, bufferInitializer, modifier) => {
|
|
15
18
|
var _mod$__closure;
|
|
16
19
|
const values = useBufferValue(size, bufferInitializer);
|
|
17
20
|
const mod = modifier;
|
|
18
21
|
const deps = [size, ...Object.values((_mod$__closure = mod.__closure) !== null && _mod$__closure !== void 0 ? _mod$__closure : {})];
|
|
19
|
-
const mapperId =
|
|
22
|
+
const mapperId = _ReanimatedProxy.default.startMapper(() => {
|
|
20
23
|
"worklet";
|
|
21
24
|
|
|
22
25
|
values.value.forEach((val, index) => {
|
|
@@ -26,7 +29,7 @@ const useBuffer = (size, bufferInitializer, modifier) => {
|
|
|
26
29
|
}, deps);
|
|
27
30
|
(0, _react.useEffect)(() => {
|
|
28
31
|
return () => {
|
|
29
|
-
|
|
32
|
+
_ReanimatedProxy.default.stopMapper(mapperId);
|
|
30
33
|
};
|
|
31
34
|
}, [mapperId]);
|
|
32
35
|
return values;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_skia","
|
|
1
|
+
{"version":3,"names":["_react","require","_skia","_interpolators","_ReanimatedProxy","_interopRequireDefault","obj","__esModule","default","useBufferValue","size","bufferInitializer","useMemo","Rea","makeMutable","Array","fill","map","useBuffer","modifier","_mod$__closure","values","mod","deps","Object","__closure","mapperId","startMapper","value","forEach","val","index","notifyChange","useEffect","stopMapper","useRectBuffer","Skia","XYWHRect","exports","useRSXformBuffer","RSXform","usePointBuffer","Point","useColorBuffer","Color"],"sources":["buffers.ts"],"sourcesContent":["import { useEffect, useMemo } from \"react\";\nimport type { WorkletFunction } from \"react-native-reanimated/lib/typescript/reanimated2/commonTypes\";\n\nimport type { SkColor, SkHostRect, SkPoint, SkRSXform } from \"../../skia/types\";\nimport { Skia } from \"../../skia\";\n\nimport { notifyChange } from \"./interpolators\";\nimport Rea from \"./ReanimatedProxy\";\n\ntype Modifier<T> = (input: T, index: number) => void;\n\nconst useBufferValue = <T>(size: number, bufferInitializer: () => T) => {\n return useMemo(\n () => Rea.makeMutable(new Array(size).fill(0).map(bufferInitializer)),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [size]\n );\n};\n\nconst useBuffer = <T>(\n size: number,\n bufferInitializer: () => T,\n modifier: Modifier<T>\n) => {\n const values = useBufferValue(size, bufferInitializer);\n const mod = modifier as WorkletFunction;\n const deps = [size, ...Object.values(mod.__closure ?? {})];\n const mapperId = Rea.startMapper(() => {\n \"worklet\";\n values.value.forEach((val, index) => {\n modifier(val, index);\n });\n notifyChange(values);\n }, deps);\n\n useEffect(() => {\n return () => {\n Rea.stopMapper(mapperId);\n };\n }, [mapperId]);\n\n return values;\n};\n\nexport const useRectBuffer = (size: number, modifier: Modifier<SkHostRect>) =>\n useBuffer(size, () => Skia.XYWHRect(0, 0, 0, 0), modifier);\n\n// Usage for RSXform Buffer\nexport const useRSXformBuffer = (size: number, modifier: Modifier<SkRSXform>) =>\n useBuffer(size, () => Skia.RSXform(1, 0, 0, 0), modifier);\n\n// Usage for Point Buffer\nexport const usePointBuffer = (size: number, modifier: Modifier<SkPoint>) =>\n useBuffer(size, () => Skia.Point(0, 0), modifier);\n\n// Usage for Color Buffer\nexport const useColorBuffer = (size: number, modifier: Modifier<SkColor>) =>\n useBuffer(size, () => Skia.Color(\"black\"), modifier);\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,IAAAC,KAAA,GAAAD,OAAA;AAEA,IAAAE,cAAA,GAAAF,OAAA;AACA,IAAAG,gBAAA,GAAAC,sBAAA,CAAAJ,OAAA;AAAoC,SAAAI,uBAAAC,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAIpC,MAAMG,cAAc,GAAGA,CAAIC,IAAY,EAAEC,iBAA0B,KAAK;EACtE,OAAO,IAAAC,cAAO,EACZ,MAAMC,wBAAG,CAACC,WAAW,CAAC,IAAIC,KAAK,CAACL,IAAI,CAAC,CAACM,IAAI,CAAC,CAAC,CAAC,CAACC,GAAG,CAACN,iBAAiB,CAAC,CAAC;EACrE;EACA,CAACD,IAAI,CACP,CAAC;AACH,CAAC;AAED,MAAMQ,SAAS,GAAGA,CAChBR,IAAY,EACZC,iBAA0B,EAC1BQ,QAAqB,KAClB;EAAA,IAAAC,cAAA;EACH,MAAMC,MAAM,GAAGZ,cAAc,CAACC,IAAI,EAAEC,iBAAiB,CAAC;EACtD,MAAMW,GAAG,GAAGH,QAA2B;EACvC,MAAMI,IAAI,GAAG,CAACb,IAAI,EAAE,GAAGc,MAAM,CAACH,MAAM,EAAAD,cAAA,GAACE,GAAG,CAACG,SAAS,cAAAL,cAAA,cAAAA,cAAA,GAAI,CAAC,CAAC,CAAC,CAAC;EAC1D,MAAMM,QAAQ,GAAGb,wBAAG,CAACc,WAAW,CAAC,MAAM;IACrC,SAAS;;IACTN,MAAM,CAACO,KAAK,CAACC,OAAO,CAAC,CAACC,GAAG,EAAEC,KAAK,KAAK;MACnCZ,QAAQ,CAACW,GAAG,EAAEC,KAAK,CAAC;IACtB,CAAC,CAAC;IACF,IAAAC,2BAAY,EAACX,MAAM,CAAC;EACtB,CAAC,EAAEE,IAAI,CAAC;EAER,IAAAU,gBAAS,EAAC,MAAM;IACd,OAAO,MAAM;MACXpB,wBAAG,CAACqB,UAAU,CAACR,QAAQ,CAAC;IAC1B,CAAC;EACH,CAAC,EAAE,CAACA,QAAQ,CAAC,CAAC;EAEd,OAAOL,MAAM;AACf,CAAC;AAEM,MAAMc,aAAa,GAAGA,CAACzB,IAAY,EAAES,QAA8B,KACxED,SAAS,CAACR,IAAI,EAAE,MAAM0B,UAAI,CAACC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAElB,QAAQ,CAAC;;AAE5D;AAAAmB,OAAA,CAAAH,aAAA,GAAAA,aAAA;AACO,MAAMI,gBAAgB,GAAGA,CAAC7B,IAAY,EAAES,QAA6B,KAC1ED,SAAS,CAACR,IAAI,EAAE,MAAM0B,UAAI,CAACI,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAErB,QAAQ,CAAC;;AAE3D;AAAAmB,OAAA,CAAAC,gBAAA,GAAAA,gBAAA;AACO,MAAME,cAAc,GAAGA,CAAC/B,IAAY,EAAES,QAA2B,KACtED,SAAS,CAACR,IAAI,EAAE,MAAM0B,UAAI,CAACM,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAEvB,QAAQ,CAAC;;AAEnD;AAAAmB,OAAA,CAAAG,cAAA,GAAAA,cAAA;AACO,MAAME,cAAc,GAAGA,CAACjC,IAAY,EAAES,QAA2B,KACtED,SAAS,CAACR,IAAI,EAAE,MAAM0B,UAAI,CAACQ,KAAK,CAAC,OAAO,CAAC,EAAEzB,QAAQ,CAAC;AAACmB,OAAA,CAAAK,cAAA,GAAAA,cAAA"}
|
|
@@ -8,7 +8,8 @@ var _react = require("react");
|
|
|
8
8
|
var _animation = require("../../animation");
|
|
9
9
|
var _skia = require("../../skia");
|
|
10
10
|
var _Offscreen = require("../../renderer/Offscreen");
|
|
11
|
-
var
|
|
11
|
+
var _ReanimatedProxy = _interopRequireDefault(require("./ReanimatedProxy"));
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
13
|
const notifyChange = value => {
|
|
13
14
|
"worklet";
|
|
14
15
|
|
|
@@ -20,8 +21,8 @@ const notifyChange = value => {
|
|
|
20
21
|
exports.notifyChange = notifyChange;
|
|
21
22
|
const usePathValue = (cb, init) => {
|
|
22
23
|
const pathInit = (0, _react.useMemo)(() => _skia.Skia.Path.Make(), []);
|
|
23
|
-
const path =
|
|
24
|
-
|
|
24
|
+
const path = _ReanimatedProxy.default.useSharedValue(pathInit);
|
|
25
|
+
_ReanimatedProxy.default.useDerivedValue(() => {
|
|
25
26
|
path.value.reset();
|
|
26
27
|
if (init !== undefined) {
|
|
27
28
|
path.value.addPath(init);
|
|
@@ -33,13 +34,13 @@ const usePathValue = (cb, init) => {
|
|
|
33
34
|
};
|
|
34
35
|
exports.usePathValue = usePathValue;
|
|
35
36
|
const useClock = () => {
|
|
36
|
-
const clock =
|
|
37
|
+
const clock = _ReanimatedProxy.default.useSharedValue(0);
|
|
37
38
|
const callback = (0, _react.useCallback)(info => {
|
|
38
39
|
"worklet";
|
|
39
40
|
|
|
40
41
|
clock.value = info.timeSinceFirstFrame;
|
|
41
42
|
}, [clock]);
|
|
42
|
-
|
|
43
|
+
_ReanimatedProxy.default.useFrameCallback(callback);
|
|
43
44
|
return clock;
|
|
44
45
|
};
|
|
45
46
|
|
|
@@ -50,8 +51,8 @@ exports.useClock = useClock;
|
|
|
50
51
|
const useInterpolator = (factory, value, interpolator, input, output, options) => {
|
|
51
52
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
53
|
const init = (0, _react.useMemo)(() => factory(), []);
|
|
53
|
-
const result =
|
|
54
|
-
|
|
54
|
+
const result = _ReanimatedProxy.default.useSharedValue(init);
|
|
55
|
+
_ReanimatedProxy.default.useAnimatedReaction(() => value.value, val => {
|
|
55
56
|
result.value = interpolator(val, input, output, options, result.value);
|
|
56
57
|
notifyChange(result);
|
|
57
58
|
}, [input, output, options]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_animation","_skia","_Offscreen","
|
|
1
|
+
{"version":3,"names":["_react","require","_animation","_skia","_Offscreen","_ReanimatedProxy","_interopRequireDefault","obj","__esModule","default","notifyChange","value","isOnMainThread","_value","exports","usePathValue","cb","init","pathInit","useMemo","Skia","Path","Make","path","Rea","useSharedValue","useDerivedValue","reset","undefined","addPath","useClock","clock","callback","useCallback","info","timeSinceFirstFrame","useFrameCallback","useInterpolator","factory","interpolator","input","output","options","result","useAnimatedReaction","val","usePathInterpolation","outputRange","allPathsInterpolable","slice","every","isInterpolatable","Error","interpolatePaths","useVectorInterpolation","Point","interpolateVector"],"sources":["interpolators.ts"],"sourcesContent":["import type {\n ExtrapolationType,\n FrameInfo,\n SharedValue,\n} from \"react-native-reanimated\";\nimport { useCallback, useMemo } from \"react\";\n\nimport type { SkPath, SkPoint } from \"../../skia/types\";\nimport { interpolatePaths, interpolateVector } from \"../../animation\";\nimport { Skia } from \"../../skia\";\nimport { isOnMainThread } from \"../../renderer/Offscreen\";\n\nimport Rea from \"./ReanimatedProxy\";\n\nexport const notifyChange = (value: SharedValue<unknown>) => {\n \"worklet\";\n if (isOnMainThread()) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (value as any)._value = value.value;\n }\n};\n\nexport const usePathValue = (cb: (path: SkPath) => void, init?: SkPath) => {\n const pathInit = useMemo(() => Skia.Path.Make(), []);\n const path = Rea.useSharedValue(pathInit);\n Rea.useDerivedValue(() => {\n path.value.reset();\n if (init !== undefined) {\n path.value.addPath(init);\n }\n cb(path.value);\n notifyChange(path);\n });\n return path;\n};\n\nexport const useClock = () => {\n const clock = Rea.useSharedValue(0);\n const callback = useCallback(\n (info: FrameInfo) => {\n \"worklet\";\n clock.value = info.timeSinceFirstFrame;\n },\n [clock]\n );\n Rea.useFrameCallback(callback);\n return clock;\n};\n\n/**\n * @worklet\n */\ntype Interpolator<T> = (\n value: number,\n input: number[],\n output: T[],\n options: ExtrapolationType,\n result: T\n) => T;\n\nconst useInterpolator = <T>(\n factory: () => T,\n value: SharedValue<number>,\n interpolator: Interpolator<T>,\n input: number[],\n output: T[],\n options?: ExtrapolationType\n) => {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const init = useMemo(() => factory(), []);\n const result = Rea.useSharedValue(init);\n Rea.useAnimatedReaction(\n () => value.value,\n (val) => {\n result.value = interpolator(val, input, output, options, result.value);\n notifyChange(result);\n },\n [input, output, options]\n );\n return result;\n};\n\nexport const usePathInterpolation = (\n value: SharedValue<number>,\n input: number[],\n outputRange: SkPath[],\n options?: ExtrapolationType\n) => {\n // Check if all paths in outputRange are interpolable\n const allPathsInterpolable = outputRange\n .slice(1)\n .every((path) => outputRange[0].isInterpolatable(path));\n if (!allPathsInterpolable) {\n // Handle the case where not all paths are interpolable\n // For example, throw an error or return early\n throw new Error(\n `Not all paths in the output range are interpolable.\nSee: https://shopify.github.io/react-native-skia/docs/animations/hooks#usepathinterpolation`\n );\n }\n return useInterpolator(\n () => Skia.Path.Make(),\n value,\n interpolatePaths,\n input,\n outputRange,\n options\n );\n};\n\nexport const useVectorInterpolation = (\n value: SharedValue<number>,\n input: number[],\n outputRange: SkPoint[],\n options?: ExtrapolationType\n) =>\n useInterpolator(\n () => Skia.Point(0, 0),\n value,\n interpolateVector,\n input,\n outputRange,\n options\n );\n"],"mappings":"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"}
|