@shopify/react-native-skia 1.11.18 → 1.12.0
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/cpp/rnskia/DawnContext.h +1 -1
- package/lib/commonjs/sksg/Recorder/Core.d.ts +1 -5
- package/lib/commonjs/sksg/Recorder/Core.js +12 -4
- package/lib/commonjs/sksg/Recorder/Core.js.map +1 -1
- package/lib/commonjs/sksg/Recorder/Player.js +4 -5
- package/lib/commonjs/sksg/Recorder/Player.js.map +1 -1
- package/lib/commonjs/sksg/Recorder/commands/Box.js +2 -4
- package/lib/commonjs/sksg/Recorder/commands/Box.js.map +1 -1
- package/lib/module/sksg/Recorder/Core.d.ts +1 -5
- package/lib/module/sksg/Recorder/Core.js +10 -2
- package/lib/module/sksg/Recorder/Core.js.map +1 -1
- package/lib/module/sksg/Recorder/Player.js +5 -6
- package/lib/module/sksg/Recorder/Player.js.map +1 -1
- package/lib/module/sksg/Recorder/commands/Box.js +3 -5
- package/lib/module/sksg/Recorder/commands/Box.js.map +1 -1
- package/lib/typescript/lib/commonjs/sksg/Recorder/Core.d.ts +1 -1
- package/lib/typescript/lib/module/sksg/Recorder/Core.d.ts +1 -1
- package/lib/typescript/src/sksg/Recorder/Core.d.ts +1 -5
- package/package.json +1 -1
- package/react-native-skia.podspec +3 -3
- package/src/sksg/Recorder/Core.ts +5 -7
- package/src/sksg/Recorder/Player.ts +5 -6
- package/src/sksg/Recorder/commands/Box.ts +3 -5
- package/ios/RNSkia-iOS/MetalContext.h +0 -134
- package/ios/RNSkia-iOS/MetalContext.mm +0 -34
- package/ios/RNSkia-iOS/MetalWindowContext.h +0 -39
- package/ios/RNSkia-iOS/MetalWindowContext.mm +0 -60
- package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +0 -38
- package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.mm +0 -103
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +0 -75
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +0 -303
- package/ios/RNSkia-iOS/RNSkiOSVideo.h +0 -51
- package/ios/RNSkia-iOS/RNSkiOSVideo.mm +0 -137
- package/ios/RNSkia-iOS/RNSkiOSView.h +0 -37
- package/ios/RNSkia-iOS/RNSkiOSView.mm +0 -35
- package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.h +0 -119
- package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +0 -344
- package/ios/RNSkia-iOS/SkiaManager.h +0 -25
- package/ios/RNSkia-iOS/SkiaManager.mm +0 -62
- package/ios/RNSkia-iOS/SkiaPictureView.h +0 -7
- package/ios/RNSkia-iOS/SkiaPictureView.mm +0 -66
- package/ios/RNSkia-iOS/SkiaPictureViewManager.h +0 -8
- package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +0 -51
- package/ios/RNSkia-iOS/SkiaUIView.h +0 -37
- package/ios/RNSkia-iOS/SkiaUIView.mm +0 -168
- package/ios/RNSkia-iOS/ViewScreenshotService.h +0 -21
- package/ios/RNSkia-iOS/ViewScreenshotService.mm +0 -85
- package/ios/RNSkiaModule.h +0 -20
- package/ios/RNSkiaModule.mm +0 -55
- package/ios/Rnskia.xcodeproj/project.pbxproj +0 -281
- package/ios/Rnskia.xcworkspace/contents.xcworkspacedata +0 -7
@@ -1,303 +0,0 @@
|
|
1
|
-
#import "RNSkiOSPlatformContext.h"
|
2
|
-
|
3
|
-
#import <CoreMedia/CMSampleBuffer.h>
|
4
|
-
#include <Metal/Metal.h>
|
5
|
-
#import <React/RCTUtils.h>
|
6
|
-
#include <thread>
|
7
|
-
#include <utility>
|
8
|
-
|
9
|
-
#if defined(SK_GRAPHITE)
|
10
|
-
#include "DawnContext.h"
|
11
|
-
#else
|
12
|
-
#include "MetalContext.h"
|
13
|
-
#endif
|
14
|
-
#include "RNSkiOSVideo.h"
|
15
|
-
|
16
|
-
#pragma clang diagnostic push
|
17
|
-
#pragma clang diagnostic ignored "-Wdocumentation"
|
18
|
-
|
19
|
-
#import "include/core/SkColorSpace.h"
|
20
|
-
#include "include/core/SkFontMgr.h"
|
21
|
-
#include "include/core/SkSurface.h"
|
22
|
-
|
23
|
-
#include "include/ports/SkFontMgr_mac_ct.h"
|
24
|
-
|
25
|
-
#pragma clang diagnostic pop
|
26
|
-
|
27
|
-
namespace RNSkia {
|
28
|
-
|
29
|
-
void RNSkiOSPlatformContext::performStreamOperation(
|
30
|
-
const std::string &sourceUri,
|
31
|
-
const std::function<void(std::unique_ptr<SkStreamAsset>)> &op) {
|
32
|
-
|
33
|
-
auto loader = [=]() {
|
34
|
-
NSURL *url = [[NSURL alloc]
|
35
|
-
initWithString:[NSString stringWithUTF8String:sourceUri.c_str()]];
|
36
|
-
|
37
|
-
NSData *data = nullptr;
|
38
|
-
auto scheme = url.scheme;
|
39
|
-
auto extension = url.pathExtension;
|
40
|
-
|
41
|
-
if (scheme == nullptr &&
|
42
|
-
(extension == nullptr || [extension isEqualToString:@""])) {
|
43
|
-
// If the extension and scheme is nil, we assume that we're trying to
|
44
|
-
// load from the embedded iOS app bundle and will try to load image
|
45
|
-
// and get data from the image directly. imageNamed will return the
|
46
|
-
// best version of the requested image:
|
47
|
-
auto image = [UIImage imageNamed:[url absoluteString]];
|
48
|
-
// We don't know the image format (png, jpg, etc) but
|
49
|
-
// UIImagePNGRepresentation will support all of them
|
50
|
-
data = UIImagePNGRepresentation(image);
|
51
|
-
} else {
|
52
|
-
// Load from metro / node
|
53
|
-
data = [NSData dataWithContentsOfURL:url];
|
54
|
-
}
|
55
|
-
|
56
|
-
auto bytes = [data bytes];
|
57
|
-
auto skData = SkData::MakeWithCopy(bytes, [data length]);
|
58
|
-
auto stream = SkMemoryStream::Make(skData);
|
59
|
-
|
60
|
-
op(std::move(stream));
|
61
|
-
};
|
62
|
-
|
63
|
-
// Fire and forget the thread - will be resolved on completion
|
64
|
-
std::thread(loader).detach();
|
65
|
-
}
|
66
|
-
|
67
|
-
void RNSkiOSPlatformContext::releaseNativeBuffer(uint64_t pointer) {
|
68
|
-
CVPixelBufferRef pixelBuffer = reinterpret_cast<CVPixelBufferRef>(pointer);
|
69
|
-
if (pixelBuffer) {
|
70
|
-
CFRelease(pixelBuffer);
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
uint64_t RNSkiOSPlatformContext::makeNativeBuffer(sk_sp<SkImage> image) {
|
75
|
-
// 0. If Image is not in BGRA, convert to BGRA as only BGRA is supported.
|
76
|
-
if (image->colorType() != kBGRA_8888_SkColorType) {
|
77
|
-
#if defined(SK_GRAPHITE)
|
78
|
-
SkImage::RequiredProperties requiredProps;
|
79
|
-
image = image->makeColorTypeAndColorSpace(
|
80
|
-
DawnContext::getInstance().getRecorder(), kBGRA_8888_SkColorType,
|
81
|
-
SkColorSpace::MakeSRGB(), requiredProps);
|
82
|
-
#else
|
83
|
-
// on iOS, 32_BGRA is the only supported RGB format for CVPixelBuffers.
|
84
|
-
image = image->makeColorTypeAndColorSpace(
|
85
|
-
MetalContext::getInstance().getDirectContext(), kBGRA_8888_SkColorType,
|
86
|
-
SkColorSpace::MakeSRGB());
|
87
|
-
#endif
|
88
|
-
if (image == nullptr) {
|
89
|
-
throw std::runtime_error(
|
90
|
-
"Failed to convert image to BGRA_8888 colortype! Only BGRA_8888 "
|
91
|
-
"NativeBuffers are supported.");
|
92
|
-
}
|
93
|
-
}
|
94
|
-
|
95
|
-
// 1. Get image info
|
96
|
-
auto bytesPerPixel = image->imageInfo().bytesPerPixel();
|
97
|
-
int bytesPerRow = image->width() * bytesPerPixel;
|
98
|
-
auto buf = SkData::MakeUninitialized(image->width() * image->height() *
|
99
|
-
bytesPerPixel);
|
100
|
-
SkImageInfo info = SkImageInfo::Make(image->width(), image->height(),
|
101
|
-
image->colorType(), image->alphaType());
|
102
|
-
// 2. Copy pixels into our buffer
|
103
|
-
image->readPixels(nullptr, info, const_cast<void *>(buf->data()), bytesPerRow,
|
104
|
-
0, 0);
|
105
|
-
|
106
|
-
// 3. Create an IOSurface (GPU + CPU memory)
|
107
|
-
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(
|
108
|
-
kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
|
109
|
-
&kCFTypeDictionaryValueCallBacks);
|
110
|
-
int width = image->width();
|
111
|
-
int height = image->height();
|
112
|
-
int pitch = width * bytesPerPixel;
|
113
|
-
int size = width * height * bytesPerPixel;
|
114
|
-
OSType pixelFormat = kCVPixelFormatType_32BGRA;
|
115
|
-
CFDictionarySetValue(
|
116
|
-
dict, kIOSurfaceBytesPerRow,
|
117
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pitch));
|
118
|
-
CFDictionarySetValue(
|
119
|
-
dict, kIOSurfaceBytesPerElement,
|
120
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bytesPerPixel));
|
121
|
-
CFDictionarySetValue(
|
122
|
-
dict, kIOSurfaceWidth,
|
123
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width));
|
124
|
-
CFDictionarySetValue(
|
125
|
-
dict, kIOSurfaceHeight,
|
126
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height));
|
127
|
-
CFDictionarySetValue(
|
128
|
-
dict, kIOSurfacePixelFormat,
|
129
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pixelFormat));
|
130
|
-
CFDictionarySetValue(
|
131
|
-
dict, kIOSurfaceAllocSize,
|
132
|
-
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &size));
|
133
|
-
IOSurfaceRef surface = IOSurfaceCreate(dict);
|
134
|
-
if (surface == nil) {
|
135
|
-
throw std::runtime_error("Failed to create " + std::to_string(width) + "x" +
|
136
|
-
std::to_string(height) + " IOSurface!");
|
137
|
-
}
|
138
|
-
|
139
|
-
// 4. Copy over the memory from the pixels into the IOSurface
|
140
|
-
IOSurfaceLock(surface, 0, nil);
|
141
|
-
void *base = IOSurfaceGetBaseAddress(surface);
|
142
|
-
memcpy(base, buf->data(), buf->size());
|
143
|
-
IOSurfaceUnlock(surface, 0, nil);
|
144
|
-
|
145
|
-
// 5. Create a CVPixelBuffer from the IOSurface
|
146
|
-
CVPixelBufferRef pixelBuffer = nullptr;
|
147
|
-
CVReturn result =
|
148
|
-
CVPixelBufferCreateWithIOSurface(nil, surface, nil, &pixelBuffer);
|
149
|
-
if (result != kCVReturnSuccess) {
|
150
|
-
throw std::runtime_error(
|
151
|
-
"Failed to create CVPixelBuffer from SkImage! Return value: " +
|
152
|
-
std::to_string(result));
|
153
|
-
}
|
154
|
-
|
155
|
-
// 8. Return CVPixelBuffer casted to uint64_t
|
156
|
-
return reinterpret_cast<uint64_t>(pixelBuffer);
|
157
|
-
}
|
158
|
-
|
159
|
-
const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp<SkImage> image) {
|
160
|
-
GrBackendTexture texture;
|
161
|
-
TextureInfo result;
|
162
|
-
if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
|
163
|
-
throw std::runtime_error("Couldn't get backend texture");
|
164
|
-
}
|
165
|
-
if (!texture.isValid()) {
|
166
|
-
throw std::runtime_error("Invalid backend texture");
|
167
|
-
}
|
168
|
-
GrMtlTextureInfo textureInfo;
|
169
|
-
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
|
170
|
-
throw std::runtime_error("Couldn't get Metal texture info");
|
171
|
-
}
|
172
|
-
result.mtlTexture = textureInfo.fTexture.get();
|
173
|
-
return result;
|
174
|
-
}
|
175
|
-
|
176
|
-
const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp<SkSurface> surface) {
|
177
|
-
GrBackendTexture texture = SkSurfaces::GetBackendTexture(
|
178
|
-
surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
|
179
|
-
TextureInfo result;
|
180
|
-
if (!texture.isValid()) {
|
181
|
-
throw std::runtime_error("Invalid backend texture");
|
182
|
-
}
|
183
|
-
GrMtlTextureInfo textureInfo;
|
184
|
-
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
|
185
|
-
throw std::runtime_error("Couldn't get Metal texture info");
|
186
|
-
}
|
187
|
-
result.mtlTexture = textureInfo.fTexture.get();
|
188
|
-
return result;
|
189
|
-
}
|
190
|
-
|
191
|
-
std::shared_ptr<RNSkVideo>
|
192
|
-
RNSkiOSPlatformContext::createVideo(const std::string &url) {
|
193
|
-
return std::make_shared<RNSkiOSVideo>(url, this);
|
194
|
-
}
|
195
|
-
|
196
|
-
std::shared_ptr<WindowContext>
|
197
|
-
RNSkiOSPlatformContext::makeContextFromNativeSurface(void *surface, int width,
|
198
|
-
int height) {
|
199
|
-
#if defined(SK_GRAPHITE)
|
200
|
-
return DawnContext::getInstance().MakeWindow(surface, width, height);
|
201
|
-
#else
|
202
|
-
return MetalContext::getInstance().MakeWindow((__bridge CALayer *)surface,
|
203
|
-
width, height);
|
204
|
-
#endif
|
205
|
-
}
|
206
|
-
|
207
|
-
void RNSkiOSPlatformContext::raiseError(const std::exception &err) {
|
208
|
-
RCTFatal(RCTErrorWithMessage([NSString stringWithUTF8String:err.what()]));
|
209
|
-
}
|
210
|
-
|
211
|
-
sk_sp<SkSurface> RNSkiOSPlatformContext::makeOffscreenSurface(int width,
|
212
|
-
int height) {
|
213
|
-
#if defined(SK_GRAPHITE)
|
214
|
-
return DawnContext::getInstance().MakeOffscreen(width, height);
|
215
|
-
#else
|
216
|
-
return MetalContext::getInstance().MakeOffscreen(width, height);
|
217
|
-
#endif
|
218
|
-
}
|
219
|
-
|
220
|
-
sk_sp<SkImage> RNSkiOSPlatformContext::makeImageFromNativeBuffer(void *buffer) {
|
221
|
-
#if defined(SK_GRAPHITE)
|
222
|
-
return DawnContext::getInstance().MakeImageFromBuffer(buffer);
|
223
|
-
#else
|
224
|
-
return MetalContext::getInstance().MakeImageFromBuffer(buffer);
|
225
|
-
#endif
|
226
|
-
}
|
227
|
-
|
228
|
-
sk_sp<SkImage> RNSkiOSPlatformContext::makeImageFromNativeTexture(
|
229
|
-
const TextureInfo &texInfo, int width, int height, bool mipMapped) {
|
230
|
-
id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(texInfo.mtlTexture);
|
231
|
-
|
232
|
-
SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat);
|
233
|
-
if (colorType == SkColorType::kUnknown_SkColorType) {
|
234
|
-
throw std::runtime_error("Unsupported pixelFormat");
|
235
|
-
}
|
236
|
-
|
237
|
-
GrMtlTextureInfo textureInfo;
|
238
|
-
textureInfo.fTexture.retain((__bridge const void *)mtlTexture);
|
239
|
-
|
240
|
-
GrBackendTexture texture = GrBackendTextures::MakeMtl(
|
241
|
-
width, height, mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
|
242
|
-
textureInfo);
|
243
|
-
|
244
|
-
return SkImages::BorrowTextureFrom(getDirectContext(), texture,
|
245
|
-
kTopLeft_GrSurfaceOrigin, colorType,
|
246
|
-
kPremul_SkAlphaType, nullptr);
|
247
|
-
}
|
248
|
-
|
249
|
-
SkColorType RNSkiOSPlatformContext::mtlPixelFormatToSkColorType(
|
250
|
-
MTLPixelFormat pixelFormat) {
|
251
|
-
switch (pixelFormat) {
|
252
|
-
case MTLPixelFormatRGBA8Unorm:
|
253
|
-
return kRGBA_8888_SkColorType;
|
254
|
-
case MTLPixelFormatBGRA8Unorm:
|
255
|
-
return kBGRA_8888_SkColorType;
|
256
|
-
case MTLPixelFormatRGB10A2Unorm:
|
257
|
-
return kRGBA_1010102_SkColorType;
|
258
|
-
case MTLPixelFormatR8Unorm:
|
259
|
-
return kGray_8_SkColorType;
|
260
|
-
case MTLPixelFormatRGBA16Float:
|
261
|
-
return kRGBA_F16_SkColorType;
|
262
|
-
case MTLPixelFormatRG8Unorm:
|
263
|
-
return kR8G8_unorm_SkColorType;
|
264
|
-
case MTLPixelFormatR16Float:
|
265
|
-
return kA16_float_SkColorType;
|
266
|
-
case MTLPixelFormatRG16Float:
|
267
|
-
return kR16G16_float_SkColorType;
|
268
|
-
case MTLPixelFormatR16Unorm:
|
269
|
-
return kA16_unorm_SkColorType;
|
270
|
-
case MTLPixelFormatRG16Unorm:
|
271
|
-
return kR16G16_unorm_SkColorType;
|
272
|
-
case MTLPixelFormatRGBA16Unorm:
|
273
|
-
return kR16G16B16A16_unorm_SkColorType;
|
274
|
-
case MTLPixelFormatRGBA8Unorm_sRGB:
|
275
|
-
return kSRGBA_8888_SkColorType;
|
276
|
-
default:
|
277
|
-
return kUnknown_SkColorType;
|
278
|
-
}
|
279
|
-
}
|
280
|
-
|
281
|
-
#if !defined(SK_GRAPHITE)
|
282
|
-
GrDirectContext *RNSkiOSPlatformContext::getDirectContext() {
|
283
|
-
return MetalContext::getInstance().getDirectContext();
|
284
|
-
}
|
285
|
-
#endif
|
286
|
-
|
287
|
-
sk_sp<SkFontMgr> RNSkiOSPlatformContext::createFontMgr() {
|
288
|
-
return SkFontMgr_New_CoreText(nullptr);
|
289
|
-
}
|
290
|
-
|
291
|
-
void RNSkiOSPlatformContext::runOnMainThread(std::function<void()> func) {
|
292
|
-
dispatch_async(dispatch_get_main_queue(), ^{
|
293
|
-
func();
|
294
|
-
});
|
295
|
-
}
|
296
|
-
|
297
|
-
sk_sp<SkImage>
|
298
|
-
RNSkiOSPlatformContext::takeScreenshotFromViewTag(size_t viewTag) {
|
299
|
-
return [_screenshotService
|
300
|
-
screenshotOfViewWithTag:[NSNumber numberWithLong:viewTag]];
|
301
|
-
}
|
302
|
-
|
303
|
-
} // namespace RNSkia
|
@@ -1,51 +0,0 @@
|
|
1
|
-
#pragma once
|
2
|
-
|
3
|
-
#include <string>
|
4
|
-
|
5
|
-
#include <AVFoundation/AVFoundation.h>
|
6
|
-
#include <CoreVideo/CoreVideo.h>
|
7
|
-
|
8
|
-
#pragma clang diagnostic push
|
9
|
-
#pragma clang diagnostic ignored "-Wdocumentation"
|
10
|
-
|
11
|
-
#include "include/core/SkImage.h"
|
12
|
-
#include "include/core/SkSize.h"
|
13
|
-
|
14
|
-
#pragma clang diagnostic pop
|
15
|
-
|
16
|
-
#include "RNSkPlatformContext.h"
|
17
|
-
#include "RNSkVideo.h"
|
18
|
-
|
19
|
-
namespace RNSkia {
|
20
|
-
|
21
|
-
class RNSkiOSVideo : public RNSkVideo {
|
22
|
-
private:
|
23
|
-
std::string _url;
|
24
|
-
AVPlayer *_player = nullptr;
|
25
|
-
AVPlayerItem *_playerItem = nullptr;
|
26
|
-
AVPlayerItemVideoOutput *_videoOutput = nullptr;
|
27
|
-
RNSkPlatformContext *_context;
|
28
|
-
double _duration = 0;
|
29
|
-
double _framerate = 0;
|
30
|
-
float _videoWidth = 0;
|
31
|
-
float _videoHeight = 0;
|
32
|
-
CGAffineTransform _preferredTransform;
|
33
|
-
bool _isPlaying = false;
|
34
|
-
void setupPlayer();
|
35
|
-
NSDictionary *getOutputSettings();
|
36
|
-
|
37
|
-
public:
|
38
|
-
RNSkiOSVideo(std::string url, RNSkPlatformContext *context);
|
39
|
-
~RNSkiOSVideo();
|
40
|
-
sk_sp<SkImage> nextImage(double *timeStamp = nullptr) override;
|
41
|
-
double duration() override;
|
42
|
-
double framerate() override;
|
43
|
-
void seek(double timestamp) override;
|
44
|
-
void play();
|
45
|
-
void pause();
|
46
|
-
float getRotationInDegrees() override;
|
47
|
-
SkISize getSize() override;
|
48
|
-
void setVolume(float volume);
|
49
|
-
};
|
50
|
-
|
51
|
-
} // namespace RNSkia
|
@@ -1,137 +0,0 @@
|
|
1
|
-
#include <memory>
|
2
|
-
#include <string>
|
3
|
-
|
4
|
-
#pragma clang diagnostic push
|
5
|
-
#pragma clang diagnostic ignored "-Wdocumentation"
|
6
|
-
|
7
|
-
#include "include/core/SkImage.h"
|
8
|
-
|
9
|
-
#pragma clang diagnostic pop
|
10
|
-
|
11
|
-
#include "RNSkiOSVideo.h"
|
12
|
-
#include <AVFoundation/AVFoundation.h>
|
13
|
-
#include <CoreVideo/CoreVideo.h>
|
14
|
-
|
15
|
-
namespace RNSkia {
|
16
|
-
|
17
|
-
RNSkiOSVideo::RNSkiOSVideo(std::string url, RNSkPlatformContext *context)
|
18
|
-
: _url(std::move(url)), _context(context) {
|
19
|
-
setupPlayer();
|
20
|
-
}
|
21
|
-
|
22
|
-
RNSkiOSVideo::~RNSkiOSVideo() {
|
23
|
-
if (_player) {
|
24
|
-
[_player pause];
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
void RNSkiOSVideo::setupPlayer() {
|
29
|
-
NSURL *videoURL = [NSURL URLWithString:@(_url.c_str())];
|
30
|
-
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoURL];
|
31
|
-
_player = [AVPlayer playerWithPlayerItem:playerItem];
|
32
|
-
_playerItem = playerItem;
|
33
|
-
|
34
|
-
NSDictionary *outputSettings = getOutputSettings();
|
35
|
-
_videoOutput =
|
36
|
-
[[AVPlayerItemVideoOutput alloc] initWithOutputSettings:outputSettings];
|
37
|
-
[playerItem addOutput:_videoOutput];
|
38
|
-
|
39
|
-
CMTime time = playerItem.asset.duration;
|
40
|
-
if (time.timescale != 0) {
|
41
|
-
_duration = CMTimeGetSeconds(time) * 1000; // Store duration in milliseconds
|
42
|
-
}
|
43
|
-
|
44
|
-
AVAssetTrack *videoTrack =
|
45
|
-
[[playerItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
|
46
|
-
if (videoTrack) {
|
47
|
-
_framerate = videoTrack.nominalFrameRate;
|
48
|
-
_preferredTransform = videoTrack.preferredTransform;
|
49
|
-
CGSize videoSize = videoTrack.naturalSize;
|
50
|
-
_videoWidth = videoSize.width;
|
51
|
-
_videoHeight = videoSize.height;
|
52
|
-
}
|
53
|
-
play();
|
54
|
-
}
|
55
|
-
|
56
|
-
sk_sp<SkImage> RNSkiOSVideo::nextImage(double *timeStamp) {
|
57
|
-
CMTime currentTime = [_player currentTime];
|
58
|
-
CVPixelBufferRef pixelBuffer =
|
59
|
-
[_videoOutput copyPixelBufferForItemTime:currentTime
|
60
|
-
itemTimeForDisplay:nullptr];
|
61
|
-
if (!pixelBuffer) {
|
62
|
-
NSLog(@"No pixel buffer.");
|
63
|
-
return nullptr;
|
64
|
-
}
|
65
|
-
|
66
|
-
auto skImage = _context->makeImageFromNativeBuffer((void *)pixelBuffer);
|
67
|
-
|
68
|
-
if (timeStamp) {
|
69
|
-
*timeStamp = CMTimeGetSeconds(currentTime);
|
70
|
-
}
|
71
|
-
|
72
|
-
CVPixelBufferRelease(pixelBuffer);
|
73
|
-
return skImage;
|
74
|
-
}
|
75
|
-
|
76
|
-
NSDictionary *RNSkiOSVideo::getOutputSettings() {
|
77
|
-
return @{
|
78
|
-
(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
|
79
|
-
(id)kCVPixelBufferMetalCompatibilityKey : @YES
|
80
|
-
};
|
81
|
-
}
|
82
|
-
|
83
|
-
float RNSkiOSVideo::getRotationInDegrees() {
|
84
|
-
CGFloat rotationAngle = 0.0;
|
85
|
-
auto transform = _preferredTransform;
|
86
|
-
// Determine the rotation angle in radians
|
87
|
-
if (transform.a == 0 && transform.b == 1 && transform.c == -1 &&
|
88
|
-
transform.d == 0) {
|
89
|
-
rotationAngle = 90;
|
90
|
-
} else if (transform.a == 0 && transform.b == -1 && transform.c == 1 &&
|
91
|
-
transform.d == 0) {
|
92
|
-
rotationAngle = 270;
|
93
|
-
} else if (transform.a == -1 && transform.b == 0 && transform.c == 0 &&
|
94
|
-
transform.d == -1) {
|
95
|
-
rotationAngle = 180;
|
96
|
-
}
|
97
|
-
return rotationAngle;
|
98
|
-
}
|
99
|
-
|
100
|
-
void RNSkiOSVideo::seek(double timeInMilliseconds) {
|
101
|
-
CMTime seekTime =
|
102
|
-
CMTimeMakeWithSeconds(timeInMilliseconds / 1000.0, NSEC_PER_SEC);
|
103
|
-
[_player seekToTime:seekTime
|
104
|
-
toleranceBefore:kCMTimeZero
|
105
|
-
toleranceAfter:kCMTimeZero
|
106
|
-
completionHandler:^(BOOL finished) {
|
107
|
-
if (!finished) {
|
108
|
-
NSLog(@"Seek failed or was interrupted.");
|
109
|
-
}
|
110
|
-
}];
|
111
|
-
}
|
112
|
-
|
113
|
-
void RNSkiOSVideo::play() {
|
114
|
-
if (_player) {
|
115
|
-
[_player play];
|
116
|
-
_isPlaying = true;
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
void RNSkiOSVideo::pause() {
|
121
|
-
if (_player) {
|
122
|
-
[_player pause];
|
123
|
-
_isPlaying = false;
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
double RNSkiOSVideo::duration() { return _duration; }
|
128
|
-
|
129
|
-
double RNSkiOSVideo::framerate() { return _framerate; }
|
130
|
-
|
131
|
-
SkISize RNSkiOSVideo::getSize() {
|
132
|
-
return SkISize::Make(_videoWidth, _videoHeight);
|
133
|
-
}
|
134
|
-
|
135
|
-
void RNSkiOSVideo::setVolume(float volume) { _player.volume = volume; }
|
136
|
-
|
137
|
-
} // namespace RNSkia
|
@@ -1,37 +0,0 @@
|
|
1
|
-
#pragma once
|
2
|
-
|
3
|
-
#import <memory>
|
4
|
-
|
5
|
-
#import "RNSkMetalCanvasProvider.h"
|
6
|
-
#import "RNSkView.h"
|
7
|
-
#import "RNSkiOSPlatformContext.h"
|
8
|
-
|
9
|
-
class RNSkBaseiOSView {
|
10
|
-
public:
|
11
|
-
virtual CALayer *getLayer() = 0;
|
12
|
-
virtual void setSize(int width, int height) = 0;
|
13
|
-
virtual std::shared_ptr<RNSkia::RNSkView> getDrawView() = 0;
|
14
|
-
};
|
15
|
-
|
16
|
-
template <class T> class RNSkiOSView : public RNSkBaseiOSView, public T {
|
17
|
-
public:
|
18
|
-
RNSkiOSView(std::shared_ptr<RNSkia::RNSkPlatformContext> context)
|
19
|
-
: T(context,
|
20
|
-
std::make_shared<RNSkMetalCanvasProvider>(
|
21
|
-
std::bind(&RNSkia::RNSkView::requestRedraw, this), context)) {}
|
22
|
-
|
23
|
-
CALayer *getLayer() override {
|
24
|
-
return std::static_pointer_cast<RNSkMetalCanvasProvider>(
|
25
|
-
this->getCanvasProvider())
|
26
|
-
->getLayer();
|
27
|
-
}
|
28
|
-
|
29
|
-
void setSize(int width, int height) override {
|
30
|
-
std::static_pointer_cast<RNSkMetalCanvasProvider>(this->getCanvasProvider())
|
31
|
-
->setSize(width, height);
|
32
|
-
}
|
33
|
-
|
34
|
-
std::shared_ptr<RNSkia::RNSkView> getDrawView() override {
|
35
|
-
return this->shared_from_this();
|
36
|
-
}
|
37
|
-
};
|
@@ -1,35 +0,0 @@
|
|
1
|
-
#import <memory>
|
2
|
-
|
3
|
-
#import "RNSkMetalCanvasProvider.h"
|
4
|
-
#import "RNSkView.h"
|
5
|
-
#import "RNSkiOSPlatformContext.h"
|
6
|
-
|
7
|
-
class RNSkBaseiOSView {
|
8
|
-
public:
|
9
|
-
virtual CALayer *getLayer() = 0;
|
10
|
-
virtual void setSize(int width, int height) = 0;
|
11
|
-
virtual std::shared_ptr<RNSkia::RNSkView> getDrawView() = 0;
|
12
|
-
};
|
13
|
-
|
14
|
-
template <class T> class RNSkiOSView : public RNSkBaseiOSView, public T {
|
15
|
-
public:
|
16
|
-
RNSkiOSView(std::shared_ptr<RNSkia::RNSkPlatformContext> context)
|
17
|
-
: T(context,
|
18
|
-
std::make_shared<RNSkMetalCanvasProvider>(
|
19
|
-
std::bind(&RNSkia::RNSkView::requestRedraw, this), context)) {}
|
20
|
-
|
21
|
-
CALayer *getLayer() override {
|
22
|
-
return std::static_pointer_cast<RNSkMetalCanvasProvider>(
|
23
|
-
this->getCanvasProvider())
|
24
|
-
->getLayer();
|
25
|
-
}
|
26
|
-
|
27
|
-
void setSize(int width, int height) override {
|
28
|
-
std::static_pointer_cast<RNSkMetalCanvasProvider>(this->getCanvasProvider())
|
29
|
-
->setSize(width, height);
|
30
|
-
}
|
31
|
-
|
32
|
-
std::shared_ptr<RNSkia::RNSkView> getDrawView() override {
|
33
|
-
return this->shared_from_this();
|
34
|
-
}
|
35
|
-
};
|
@@ -1,119 +0,0 @@
|
|
1
|
-
//
|
2
|
-
// SkiaCVPixelBufferUtils.h
|
3
|
-
// react-native-skia
|
4
|
-
//
|
5
|
-
// Created by Marc Rousavy on 10.04.24.
|
6
|
-
//
|
7
|
-
|
8
|
-
#pragma once
|
9
|
-
|
10
|
-
#import <vector>
|
11
|
-
|
12
|
-
#import <CoreMedia/CMSampleBuffer.h>
|
13
|
-
#import <CoreVideo/CVMetalTextureCache.h>
|
14
|
-
#import <MetalKit/MetalKit.h>
|
15
|
-
|
16
|
-
#pragma clang diagnostic push
|
17
|
-
#pragma clang diagnostic ignored "-Wdocumentation"
|
18
|
-
#import "include/core/SkColorSpace.h"
|
19
|
-
#import "include/core/SkImage.h"
|
20
|
-
#import "include/gpu/ganesh/GrBackendSurface.h"
|
21
|
-
#import "include/gpu/ganesh/GrYUVABackendTextures.h"
|
22
|
-
#pragma clang diagnostic pop
|
23
|
-
|
24
|
-
/**
|
25
|
-
Holds a Metal Texture.
|
26
|
-
When the `TextureHolder` is destroyed, the underlying Metal Texture
|
27
|
-
is marked as invalid and might be overwritten by the Texture Cache,
|
28
|
-
so make sure to delete the `TextureHolder` only when the `SkImage`
|
29
|
-
has been deleted.
|
30
|
-
|
31
|
-
For example, use the `releaseProc` parameter in `BorrowImageFromTexture`
|
32
|
-
to delete the `TextureHolder`.
|
33
|
-
*/
|
34
|
-
class TextureHolder {
|
35
|
-
public:
|
36
|
-
/**
|
37
|
-
Create a new instance of TextureHolder which holds
|
38
|
-
the given `CVMetalTextureRef`.
|
39
|
-
|
40
|
-
The given `CVMetalTextureRef` is assumed to already be
|
41
|
-
retained with `CFRetain`, and will later be manually
|
42
|
-
released with `CFRelease`.
|
43
|
-
*/
|
44
|
-
TextureHolder(CVMetalTextureRef texture);
|
45
|
-
~TextureHolder();
|
46
|
-
|
47
|
-
/**
|
48
|
-
Converts this Texture to a Skia GrBackendTexture.
|
49
|
-
*/
|
50
|
-
GrBackendTexture toGrBackendTexture();
|
51
|
-
|
52
|
-
private:
|
53
|
-
CVMetalTextureRef _texture;
|
54
|
-
};
|
55
|
-
|
56
|
-
/**
|
57
|
-
Same as `TextureHolder`, but for multiple planar textures (e.g. YUVA)
|
58
|
-
*/
|
59
|
-
class MultiTexturesHolder {
|
60
|
-
public:
|
61
|
-
~MultiTexturesHolder();
|
62
|
-
void addTexture(TextureHolder *texture);
|
63
|
-
|
64
|
-
private:
|
65
|
-
std::vector<TextureHolder *> _textures;
|
66
|
-
};
|
67
|
-
|
68
|
-
class SkiaCVPixelBufferUtils {
|
69
|
-
public:
|
70
|
-
enum class CVPixelBufferBaseFormat { rgb, yuv };
|
71
|
-
|
72
|
-
/**
|
73
|
-
Get the base format (currently only RGB) of the PixelBuffer.
|
74
|
-
Depending on the base-format, different methods have to be used to create
|
75
|
-
Skia buffers.
|
76
|
-
*/
|
77
|
-
static CVPixelBufferBaseFormat
|
78
|
-
getCVPixelBufferBaseFormat(CVPixelBufferRef pixelBuffer);
|
79
|
-
|
80
|
-
class RGB {
|
81
|
-
public:
|
82
|
-
/**
|
83
|
-
Creates a GPU-backed Skia Texture (SkImage) with the given RGB
|
84
|
-
CVPixelBuffer.
|
85
|
-
*/
|
86
|
-
static sk_sp<SkImage>
|
87
|
-
makeSkImageFromCVPixelBuffer(GrDirectContext *context,
|
88
|
-
CVPixelBufferRef pixelBuffer);
|
89
|
-
|
90
|
-
private:
|
91
|
-
static SkColorType getCVPixelBufferColorType(CVPixelBufferRef pixelBuffer);
|
92
|
-
};
|
93
|
-
|
94
|
-
class YUV {
|
95
|
-
public:
|
96
|
-
/**
|
97
|
-
Creates a GPU-backed Skia Texture (SkImage) with the given YUV
|
98
|
-
CVPixelBuffer.
|
99
|
-
*/
|
100
|
-
static sk_sp<SkImage>
|
101
|
-
makeSkImageFromCVPixelBuffer(GrDirectContext *context,
|
102
|
-
CVPixelBufferRef pixelBuffer);
|
103
|
-
|
104
|
-
private:
|
105
|
-
static SkYUVAInfo::PlaneConfig getPlaneConfig(OSType pixelFormat);
|
106
|
-
static SkYUVAInfo::Subsampling getSubsampling(OSType pixelFormat);
|
107
|
-
static SkYUVColorSpace getColorspace(OSType pixelFormat);
|
108
|
-
static SkYUVAInfo getYUVAInfoForCVPixelBuffer(CVPixelBufferRef pixelBuffer);
|
109
|
-
};
|
110
|
-
|
111
|
-
private:
|
112
|
-
static CVMetalTextureCacheRef getTextureCache();
|
113
|
-
static TextureHolder *
|
114
|
-
getSkiaTextureForCVPixelBufferPlane(CVPixelBufferRef pixelBuffer,
|
115
|
-
size_t planeIndex);
|
116
|
-
static MTLPixelFormat
|
117
|
-
getMTLPixelFormatForCVPixelBufferPlane(CVPixelBufferRef pixelBuffer,
|
118
|
-
size_t planeIndex);
|
119
|
-
};
|