@shopify/react-native-skia 1.6.0 → 1.7.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.
Files changed (93) hide show
  1. package/android/cpp/jni/include/JniSkiaBaseView.h +0 -1
  2. package/android/cpp/jni/include/JniSkiaManager.h +2 -2
  3. package/android/cpp/rnskia-android/MainThreadDispatcher.h +2 -4
  4. package/android/cpp/rnskia-android/OpenGLContext.h +13 -5
  5. package/android/cpp/rnskia-android/OpenGLWindowContext.h +2 -2
  6. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +61 -3
  7. package/android/cpp/rnskia-android/RNSkAndroidView.h +0 -4
  8. package/android/cpp/rnskia-android/gl/Display.h +2 -0
  9. package/cpp/api/JsiSkContourMeasure.h +1 -5
  10. package/cpp/api/JsiSkImage.h +11 -0
  11. package/cpp/api/JsiSkImageFactory.h +14 -0
  12. package/cpp/api/JsiSkPicture.h +2 -0
  13. package/cpp/api/JsiSkSurface.h +7 -0
  14. package/cpp/api/JsiTextureInfo.h +53 -0
  15. package/cpp/jsi/ViewProperty.h +48 -0
  16. package/cpp/rnskia/RNSkDomView.h +4 -7
  17. package/cpp/rnskia/RNSkJsiViewApi.h +3 -3
  18. package/cpp/rnskia/RNSkPictureView.h +8 -18
  19. package/cpp/rnskia/RNSkPlatformContext.h +18 -12
  20. package/cpp/rnskia/RNSkView.h +2 -8
  21. package/ios/RNSkia-iOS/MetalContext.h +101 -15
  22. package/ios/RNSkia-iOS/MetalContext.mm +9 -8
  23. package/ios/RNSkia-iOS/MetalWindowContext.h +39 -0
  24. package/ios/RNSkia-iOS/MetalWindowContext.mm +60 -0
  25. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +13 -25
  26. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +88 -2
  27. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +17 -6
  28. package/ios/RNSkia-iOS/SkiaManager.mm +1 -2
  29. package/ios/RNSkia-iOS/SkiaUIView.mm +17 -15
  30. package/lib/commonjs/skia/types/Image/Image.d.ts +10 -0
  31. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  32. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +18 -0
  33. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  34. package/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  35. package/lib/commonjs/skia/types/Matrix4.js +69 -1
  36. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  37. package/lib/commonjs/skia/types/Surface/Surface.d.ts +11 -0
  38. package/lib/commonjs/skia/types/Surface/Surface.js.map +1 -1
  39. package/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  40. package/lib/commonjs/skia/web/JsiSkImage.js +4 -0
  41. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  42. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  43. package/lib/commonjs/skia/web/JsiSkImageFactory.js +3 -0
  44. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  45. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  46. package/lib/commonjs/skia/web/JsiSkSurface.js +4 -0
  47. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  48. package/lib/module/skia/types/Image/Image.d.ts +10 -0
  49. package/lib/module/skia/types/Image/Image.js.map +1 -1
  50. package/lib/module/skia/types/Image/ImageFactory.d.ts +18 -0
  51. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  52. package/lib/module/skia/types/Matrix4.d.ts +6 -0
  53. package/lib/module/skia/types/Matrix4.js +67 -0
  54. package/lib/module/skia/types/Matrix4.js.map +1 -1
  55. package/lib/module/skia/types/Surface/Surface.d.ts +11 -0
  56. package/lib/module/skia/types/Surface/Surface.js.map +1 -1
  57. package/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  58. package/lib/module/skia/web/JsiSkImage.js +4 -0
  59. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  60. package/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  61. package/lib/module/skia/web/JsiSkImageFactory.js +3 -0
  62. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  63. package/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  64. package/lib/module/skia/web/JsiSkSurface.js +4 -0
  65. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  66. package/lib/typescript/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  67. package/lib/typescript/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  68. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  69. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  70. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  71. package/lib/typescript/lib/module/skia/types/Matrix4.d.ts +1 -0
  72. package/lib/typescript/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  73. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  74. package/lib/typescript/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  75. package/lib/typescript/src/skia/types/Image/Image.d.ts +10 -0
  76. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +18 -0
  77. package/lib/typescript/src/skia/types/Matrix4.d.ts +6 -0
  78. package/lib/typescript/src/skia/types/Surface/Surface.d.ts +11 -0
  79. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +1 -0
  80. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +1 -0
  81. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +1 -0
  82. package/package.json +1 -1
  83. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +93 -0
  84. package/src/skia/types/Image/Image.ts +11 -0
  85. package/src/skia/types/Image/ImageFactory.ts +24 -0
  86. package/src/skia/types/Matrix4.ts +101 -0
  87. package/src/skia/types/Surface/Surface.ts +12 -0
  88. package/src/skia/web/JsiSkImage.ts +5 -0
  89. package/src/skia/web/JsiSkImageFactory.ts +4 -0
  90. package/src/skia/web/JsiSkSurface.ts +5 -0
  91. package/cpp/jsi/JsiValueWrapper.h +0 -164
  92. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +0 -128
  93. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -92
@@ -1,13 +1,64 @@
1
1
  #pragma once
2
2
 
3
- #include "SkiaMetalSurfaceFactory.h"
4
- #include "WindowContext.h"
3
+ #include "MetalWindowContext.h"
4
+ #include "SkiaCVPixelBufferUtils.h"
5
5
 
6
6
  #include "include/core/SkSurface.h"
7
7
 
8
- namespace RNSkia {
9
- class RNSkiOSPlatformContext;
10
- }
8
+ #import <include/gpu/ganesh/GrBackendSurface.h>
9
+ #import <include/gpu/ganesh/GrDirectContext.h>
10
+ #import <include/gpu/ganesh/SkImageGanesh.h>
11
+ #import <include/gpu/ganesh/SkSurfaceGanesh.h>
12
+ #import <include/gpu/ganesh/mtl/GrMtlBackendContext.h>
13
+ #import <include/gpu/ganesh/mtl/GrMtlBackendSurface.h>
14
+ #import <include/gpu/ganesh/mtl/GrMtlDirectContext.h>
15
+ #import <include/gpu/ganesh/mtl/SkSurfaceMetal.h>
16
+
17
+ // namespace RNSkia {
18
+ // class RNSkiOSPlatformContext;
19
+ // }
20
+
21
+ class MetalSharedContext {
22
+ public:
23
+ static MetalSharedContext &getInstance() {
24
+ static MetalSharedContext instance;
25
+ return instance;
26
+ }
27
+
28
+ id<MTLDevice> getDevice() { return _device; }
29
+
30
+ private:
31
+ MetalSharedContext() {
32
+ _device = MTLCreateSystemDefaultDevice();
33
+ if (!_device) {
34
+ throw std::runtime_error("Failed to create Metal device");
35
+ }
36
+ }
37
+
38
+ MetalSharedContext(const MetalSharedContext &) = delete;
39
+ MetalSharedContext &operator=(const MetalSharedContext &) = delete;
40
+
41
+ id<MTLDevice> _device;
42
+ };
43
+
44
+ struct OffscreenRenderContext {
45
+ id<MTLTexture> texture;
46
+
47
+ OffscreenRenderContext(id<MTLDevice> device,
48
+ sk_sp<GrDirectContext> skiaContext,
49
+ id<MTLCommandQueue> commandQueue, int width,
50
+ int height) {
51
+ // Create a Metal texture descriptor
52
+ MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor
53
+ texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
54
+ width:width
55
+ height:height
56
+ mipmapped:NO];
57
+ textureDescriptor.usage =
58
+ MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
59
+ texture = [device newTextureWithDescriptor:textureDescriptor];
60
+ }
61
+ };
11
62
 
12
63
  class MetalContext {
13
64
 
@@ -21,28 +72,63 @@ public:
21
72
  }
22
73
 
23
74
  sk_sp<SkSurface> MakeOffscreen(int width, int height) {
24
- return SkiaMetalSurfaceFactory::makeOffscreenSurface(_device, &_context,
25
- width, height);
75
+ auto device = MetalSharedContext::getInstance().getDevice();
76
+ auto ctx = new OffscreenRenderContext(device, _directContext, _commandQueue,
77
+ width, height);
78
+
79
+ // Create a GrBackendTexture from the Metal texture
80
+ GrMtlTextureInfo info;
81
+ info.fTexture.retain((__bridge void *)ctx->texture);
82
+ GrBackendTexture backendTexture =
83
+ GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped::kNo, info);
84
+
85
+ // Create a SkSurface from the GrBackendTexture
86
+ auto surface = SkSurfaces::WrapBackendTexture(
87
+ _directContext.get(), backendTexture, kTopLeft_GrSurfaceOrigin, 0,
88
+ kBGRA_8888_SkColorType, nullptr, nullptr,
89
+ [](void *addr) { delete (OffscreenRenderContext *)addr; }, ctx);
90
+
91
+ return surface;
26
92
  }
27
93
 
28
94
  sk_sp<SkImage> MakeImageFromBuffer(void *buffer) {
95
+
29
96
  CVPixelBufferRef sampleBuffer = (CVPixelBufferRef)buffer;
30
- return SkiaMetalSurfaceFactory::makeTextureFromCVPixelBuffer(&_context,
31
- sampleBuffer);
97
+ SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat format =
98
+ SkiaCVPixelBufferUtils::getCVPixelBufferBaseFormat(sampleBuffer);
99
+ switch (format) {
100
+ case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::rgb: {
101
+ // CVPixelBuffer is in any RGB format, single-plane
102
+ return SkiaCVPixelBufferUtils::RGB::makeSkImageFromCVPixelBuffer(
103
+ _directContext.get(), sampleBuffer);
104
+ }
105
+ case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::yuv: {
106
+ // CVPixelBuffer is in any YUV format, multi-plane
107
+ return SkiaCVPixelBufferUtils::YUV::makeSkImageFromCVPixelBuffer(
108
+ _directContext.get(), sampleBuffer);
109
+ }
110
+ default:
111
+ [[unlikely]] {
112
+ throw std::runtime_error("Failed to convert NativeBuffer to SkImage - "
113
+ "NativeBuffer has unsupported PixelFormat! " +
114
+ std::to_string(static_cast<int>(format)));
115
+ }
116
+ }
32
117
  }
33
118
 
34
119
  std::unique_ptr<RNSkia::WindowContext> MakeWindow(CALayer *window, int width,
35
120
  int height) {
36
- return SkiaMetalSurfaceFactory::makeContext(&_context, window, width,
37
- height);
121
+ auto device = MetalSharedContext::getInstance().getDevice();
122
+ return std::make_unique<MetalWindowContext>(
123
+ _directContext.get(), device, _commandQueue, window, width, height);
38
124
  }
39
125
 
40
- GrDirectContext *getDirectContext() { return _context.skContext.get(); }
126
+ GrDirectContext *getDirectContext() { return _directContext.get(); }
41
127
 
42
128
  private:
43
- friend class RNSkia::RNSkiOSPlatformContext;
44
- id<MTLDevice> _device;
45
- SkiaMetalContext _context;
129
+ // friend class RNSkia::RNSkiOSPlatformContext;
130
+ id<MTLCommandQueue> _commandQueue = nullptr;
131
+ sk_sp<GrDirectContext> _directContext = nullptr;
46
132
 
47
133
  MetalContext();
48
134
  };
@@ -1,5 +1,7 @@
1
1
  #include "MetalContext.h"
2
2
 
3
+ #include "RNSkLog.h"
4
+
3
5
  #import <MetalKit/MetalKit.h>
4
6
 
5
7
  #pragma clang diagnostic push
@@ -16,18 +18,17 @@
16
18
  #pragma clang diagnostic pop
17
19
 
18
20
  MetalContext::MetalContext() {
19
- _device = MTLCreateSystemDefaultDevice();
20
- _context.commandQueue =
21
- id<MTLCommandQueue>(CFRetain((GrMTLHandle)[_device newCommandQueue]));
22
-
21
+ auto device = MetalSharedContext::getInstance().getDevice();
22
+ _commandQueue =
23
+ id<MTLCommandQueue>(CFRetain((GrMTLHandle)[device newCommandQueue]));
23
24
  GrMtlBackendContext backendContext = {};
24
- backendContext.fDevice.reset((__bridge void *)_device);
25
- backendContext.fQueue.reset((__bridge void *)_context.commandQueue);
25
+ backendContext.fDevice.reset((__bridge void *)device);
26
+ backendContext.fQueue.reset((__bridge void *)_commandQueue);
26
27
  GrContextOptions grContextOptions; // set different options here.
27
28
 
28
29
  // Create the Skia Direct Context
29
- _context.skContext = GrDirectContexts::MakeMetal(backendContext);
30
- if (_context.skContext == nullptr) {
30
+ _directContext = GrDirectContexts::MakeMetal(backendContext);
31
+ if (_directContext == nullptr) {
31
32
  RNSkia::RNSkLogger::logToConsole("Couldn't create a Skia Metal Context");
32
33
  }
33
34
  }
@@ -0,0 +1,39 @@
1
+ #pragma once
2
+
3
+ #import <MetalKit/MetalKit.h>
4
+
5
+ #include "WindowContext.h"
6
+
7
+ class SkiaMetalContext;
8
+
9
+ class MetalWindowContext : public RNSkia::WindowContext {
10
+ public:
11
+ MetalWindowContext(GrDirectContext *directContext, id<MTLDevice> device,
12
+ id<MTLCommandQueue> commandQueue, CALayer *layer,
13
+ int width, int height);
14
+ ~MetalWindowContext() = default;
15
+
16
+ sk_sp<SkSurface> getSurface() override;
17
+
18
+ void present() override;
19
+
20
+ int getWidth() override {
21
+ return _layer.frame.size.width * _layer.contentsScale;
22
+ };
23
+
24
+ int getHeight() override {
25
+ return _layer.frame.size.height * _layer.contentsScale;
26
+ };
27
+
28
+ void resize(int width, int height) override { _skSurface = nullptr; }
29
+
30
+ private:
31
+ GrDirectContext *_directContext;
32
+ id<MTLCommandQueue> _commandQueue;
33
+ sk_sp<SkSurface> _skSurface = nullptr;
34
+ #pragma clang diagnostic push
35
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
36
+ CAMetalLayer *_layer;
37
+ #pragma clang diagnostic pop
38
+ id<CAMetalDrawable> _currentDrawable = nil;
39
+ };
@@ -0,0 +1,60 @@
1
+ #include "MetalWindowContext.h"
2
+
3
+ #include "MetalContext.h"
4
+ #include "RNSkLog.h"
5
+
6
+ MetalWindowContext::MetalWindowContext(GrDirectContext *directContext,
7
+ id<MTLDevice> device,
8
+ id<MTLCommandQueue> commandQueue,
9
+ CALayer *layer, int width, int height)
10
+ : _directContext(directContext), _commandQueue(commandQueue) {
11
+ #pragma clang diagnostic push
12
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
13
+ _layer = (CAMetalLayer *)layer;
14
+ #pragma clang diagnostic pop
15
+ _layer.framebufferOnly = NO;
16
+ _layer.device = device;
17
+ _layer.opaque = false;
18
+ _layer.contentsScale = [UIScreen mainScreen].scale;
19
+ _layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
20
+ _layer.contentsGravity = kCAGravityBottomLeft;
21
+ _layer.drawableSize = CGSizeMake(width, height);
22
+ }
23
+
24
+ sk_sp<SkSurface> MetalWindowContext::getSurface() {
25
+ if (_skSurface) {
26
+ return _skSurface;
27
+ }
28
+
29
+ // Get the next drawable from the CAMetalLayer
30
+ _currentDrawable = [_layer nextDrawable];
31
+ if (!_currentDrawable) {
32
+ RNSkia::RNSkLogger::logToConsole(
33
+ "Could not retrieve drawable from CAMetalLayer");
34
+ return nullptr;
35
+ }
36
+
37
+ // Get the texture from the drawable
38
+ GrMtlTextureInfo fbInfo;
39
+ fbInfo.fTexture.retain((__bridge void *)_currentDrawable.texture);
40
+
41
+ GrBackendRenderTarget backendRT = GrBackendRenderTargets::MakeMtl(
42
+ _layer.drawableSize.width, _layer.drawableSize.height, fbInfo);
43
+
44
+ _skSurface = SkSurfaces::WrapBackendRenderTarget(
45
+ _directContext, backendRT, kTopLeft_GrSurfaceOrigin,
46
+ kBGRA_8888_SkColorType, nullptr, nullptr);
47
+
48
+ return _skSurface;
49
+ }
50
+
51
+ void MetalWindowContext::present() {
52
+ if (auto dContext = GrAsDirectContext(_skSurface->recordingContext())) {
53
+ dContext->flushAndSubmit();
54
+ }
55
+
56
+ id<MTLCommandBuffer> commandBuffer([_commandQueue commandBuffer]);
57
+ [commandBuffer presentDrawable:_currentDrawable];
58
+ [commandBuffer commit];
59
+ _skSurface = nullptr;
60
+ }
@@ -22,33 +22,19 @@ namespace RNSkia {
22
22
 
23
23
  namespace jsi = facebook::jsi;
24
24
 
25
- static void handleNotification(CFNotificationCenterRef center, void *observer,
26
- CFStringRef name, const void *object,
27
- CFDictionaryRef userInfo);
28
-
29
25
  class RNSkiOSPlatformContext : public RNSkPlatformContext {
30
26
  public:
31
27
  RNSkiOSPlatformContext(
32
- jsi::Runtime *runtime, RCTBridge *bridge,
28
+ RCTBridge *bridge,
33
29
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
34
- : RNSkPlatformContext(runtime, jsCallInvoker,
35
- [[UIScreen mainScreen] scale]) {
36
-
37
- // We need to make sure we invalidate when modules are freed
38
- CFNotificationCenterAddObserver(
39
- CFNotificationCenterGetLocalCenter(), this, &handleNotification,
40
- (__bridge CFStringRef)RCTBridgeWillInvalidateModulesNotification, NULL,
41
- CFNotificationSuspensionBehaviorDeliverImmediately);
30
+ : RNSkPlatformContext(jsCallInvoker, [[UIScreen mainScreen] scale]) {
42
31
 
43
32
  // Create screenshot manager
44
33
  _screenshotService =
45
34
  [[ViewScreenshotService alloc] initWithUiManager:bridge.uiManager];
46
35
  }
47
36
 
48
- ~RNSkiOSPlatformContext() {
49
- CFNotificationCenterRemoveEveryObserver(
50
- CFNotificationCenterGetLocalCenter(), this);
51
- }
37
+ ~RNSkiOSPlatformContext() = default;
52
38
 
53
39
  void runOnMainThread(std::function<void()>) override;
54
40
 
@@ -56,8 +42,16 @@ public:
56
42
 
57
43
  sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override;
58
44
 
45
+ sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &textureInfo,
46
+ int width, int height,
47
+ bool mipMapped) override;
48
+
59
49
  uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
60
50
 
51
+ const TextureInfo getTexture(sk_sp<SkSurface> image) override;
52
+
53
+ const TextureInfo getTexture(sk_sp<SkImage> image) override;
54
+
61
55
  void releaseNativeBuffer(uint64_t pointer) override;
62
56
 
63
57
  std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override;
@@ -76,16 +70,10 @@ public:
76
70
  #endif
77
71
  sk_sp<SkFontMgr> createFontMgr() override;
78
72
 
79
- void willInvalidateModules() {}
80
-
81
73
  private:
82
74
  ViewScreenshotService *_screenshotService;
83
- };
84
75
 
85
- static void handleNotification(CFNotificationCenterRef center, void *observer,
86
- CFStringRef name, const void *object,
87
- CFDictionaryRef userInfo) {
88
- (static_cast<RNSkiOSPlatformContext *>(observer))->willInvalidateModules();
89
- }
76
+ SkColorType mtlPixelFormatToSkColorType(MTLPixelFormat pixelFormat);
77
+ };
90
78
 
91
79
  } // namespace RNSkia
@@ -1,6 +1,7 @@
1
1
  #import "RNSkiOSPlatformContext.h"
2
2
 
3
3
  #import <CoreMedia/CMSampleBuffer.h>
4
+ #include <Metal/Metal.h>
4
5
  #import <React/RCTUtils.h>
5
6
  #include <thread>
6
7
  #include <utility>
@@ -81,8 +82,8 @@ uint64_t RNSkiOSPlatformContext::makeNativeBuffer(sk_sp<SkImage> image) {
81
82
  #else
82
83
  // on iOS, 32_BGRA is the only supported RGB format for CVPixelBuffers.
83
84
  image = image->makeColorTypeAndColorSpace(
84
- MetalContext::getInstance()._context.skContext.get(),
85
- kBGRA_8888_SkColorType, SkColorSpace::MakeSRGB());
85
+ MetalContext::getInstance().getDirectContext(), kBGRA_8888_SkColorType,
86
+ SkColorSpace::MakeSRGB());
86
87
  #endif
87
88
  if (image == nullptr) {
88
89
  throw std::runtime_error(
@@ -155,6 +156,38 @@ uint64_t RNSkiOSPlatformContext::makeNativeBuffer(sk_sp<SkImage> image) {
155
156
  return reinterpret_cast<uint64_t>(pixelBuffer);
156
157
  }
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
+
158
191
  std::shared_ptr<RNSkVideo>
159
192
  RNSkiOSPlatformContext::createVideo(const std::string &url) {
160
193
  return std::make_shared<RNSkiOSVideo>(url, this);
@@ -192,6 +225,59 @@ sk_sp<SkImage> RNSkiOSPlatformContext::makeImageFromNativeBuffer(void *buffer) {
192
225
  #endif
193
226
  }
194
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
+
195
281
  #if !defined(SK_GRAPHITE)
196
282
  GrDirectContext *RNSkiOSPlatformContext::getDirectContext() {
197
283
  return MetalContext::getInstance().getDirectContext();
@@ -6,29 +6,40 @@
6
6
  //
7
7
 
8
8
  #import "SkiaCVPixelBufferUtils.h"
9
- #import "SkiaMetalSurfaceFactory.h"
10
9
 
11
10
  #pragma clang diagnostic push
12
11
  #pragma clang diagnostic ignored "-Wdocumentation"
12
+
13
+ #import "include/core/SkCanvas.h"
13
14
  #import "include/core/SkColorSpace.h"
15
+
16
+ #import <CoreMedia/CMSampleBuffer.h>
17
+ #import <CoreVideo/CVMetalTextureCache.h>
18
+
14
19
  #import <include/gpu/ganesh/GrBackendSurface.h>
20
+ #import <include/gpu/ganesh/GrDirectContext.h>
15
21
  #import <include/gpu/ganesh/SkImageGanesh.h>
22
+ #import <include/gpu/ganesh/SkSurfaceGanesh.h>
16
23
  #import <include/gpu/ganesh/mtl/GrMtlBackendContext.h>
17
24
  #import <include/gpu/ganesh/mtl/GrMtlBackendSurface.h>
18
25
  #import <include/gpu/ganesh/mtl/GrMtlDirectContext.h>
19
- #import <include/gpu/ganesh/mtl/GrMtlTypes.h>
20
26
  #import <include/gpu/ganesh/mtl/SkSurfaceMetal.h>
27
+
21
28
  #pragma clang diagnostic pop
22
29
 
23
30
  #include <TargetConditionals.h>
24
31
  #if TARGET_RT_BIG_ENDIAN
25
32
  #define FourCC2Str(fourcc) \
26
- (const char[]){*((char *)&fourcc), *(((char *)&fourcc) + 1), \
27
- *(((char *)&fourcc) + 2), *(((char *)&fourcc) + 3), 0}
33
+ (const char[]) { \
34
+ *((char *)&fourcc), *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 2), \
35
+ *(((char *)&fourcc) + 3), 0 \
36
+ }
28
37
  #else
29
38
  #define FourCC2Str(fourcc) \
30
- (const char[]){*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
31
- *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0}
39
+ (const char[]) { \
40
+ *(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
41
+ *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0 \
42
+ }
32
43
  #endif
33
44
 
34
45
  // pragma MARK: TextureHolder
@@ -38,8 +38,7 @@
38
38
  // Create the RNSkiaManager (cross platform)
39
39
  _skManager = std::make_shared<RNSkia::RNSkManager>(
40
40
  jsRuntime, jsInvoker,
41
- std::make_shared<RNSkia::RNSkiOSPlatformContext>(jsRuntime, bridge,
42
- jsInvoker));
41
+ std::make_shared<RNSkia::RNSkiOSPlatformContext>(bridge, jsInvoker));
43
42
  }
44
43
  }
45
44
  return self;
@@ -62,18 +62,7 @@
62
62
  #pragma mark Lifecycle
63
63
 
64
64
  - (void)willMoveToSuperview:(UIView *)newWindow {
65
- if (newWindow == NULL) {
66
- // Remove implementation view when the parent view is not set
67
- if (_impl != nullptr) {
68
- [_impl->getLayer() removeFromSuperlayer];
69
-
70
- if (_nativeId != 0 && _manager != nullptr) {
71
- _manager->setSkiaView(_nativeId, nullptr);
72
- }
73
-
74
- _impl = nullptr;
75
- }
76
- } else {
65
+ if (newWindow != nullptr) {
77
66
  // Create implementation view when the parent view is set
78
67
  if (_impl == nullptr && _manager != nullptr) {
79
68
  _impl = _factory(_manager->getPlatformContext());
@@ -90,6 +79,21 @@
90
79
  }
91
80
  }
92
81
 
82
+ - (void)removeFromSuperview {
83
+ // Cleanup when removed from view hierarchy
84
+ if (_impl != nullptr) {
85
+ [_impl->getLayer() removeFromSuperlayer];
86
+
87
+ if (_nativeId != 0 && _manager != nullptr) {
88
+ _manager->setSkiaView(_nativeId, nullptr);
89
+ }
90
+
91
+ _impl = nullptr;
92
+ }
93
+
94
+ [super removeFromSuperview];
95
+ }
96
+
93
97
  - (void)dealloc {
94
98
  [self unregisterView];
95
99
  [[NSNotificationCenter defaultCenter]
@@ -119,8 +123,6 @@
119
123
  if (_manager != nullptr && _nativeId != 0) {
120
124
  _manager->unregisterSkiaView(_nativeId);
121
125
  }
122
-
123
- assert(_impl == nullptr);
124
126
  }
125
127
 
126
128
  #pragma Render
@@ -152,7 +154,7 @@
152
154
  }
153
155
 
154
156
  - (void)setOpaque:(bool)opaque {
155
- _opaque = opaque;
157
+ _opaque = opaque;
156
158
  }
157
159
 
158
160
  - (void)setNativeId:(size_t)nativeId {
@@ -38,6 +38,16 @@ export interface SkImage extends SkJSIInstance<"Image"> {
38
38
  * Returns the ImageInfo describing the image.
39
39
  */
40
40
  getImageInfo(): ImageInfo;
41
+ /**
42
+ * Returns the backend texture of the image.
43
+ * The returned object can be used to create a Skia Image object.
44
+ * The returned object is backend specific and should be used with caution.
45
+ * It is the caller's responsibility to ensure that the texture is not used after the image is deleted.
46
+ * The returned object may be null if the image does not have a backend texture.
47
+ *
48
+ * @return backend texture of the image or null
49
+ */
50
+ getNativeTextureUnstable(): unknown;
41
51
  /**
42
52
  * Returns this image as a shader with the specified tiling. It will use cubic sampling.
43
53
  * @param tx - tile mode in the x direction.
@@ -1 +1 @@
1
- {"version":3,"names":["FilterMode","exports","MipmapMode","ImageFormat"],"sources":["Image.ts"],"sourcesContent":["import type { SkMatrix } from \"../Matrix\";\nimport type { SkJSIInstance } from \"../JsiInstance\";\nimport type { TileMode } from \"../ImageFilter\";\nimport type { SkShader } from \"../Shader\";\n\nimport type { ImageInfo } from \"./ImageFactory\";\n\nexport interface CubicResampler {\n B: number;\n C: number;\n}\n\nexport interface FilterOptions {\n filter: FilterMode;\n mipmap?: MipmapMode;\n}\n\nexport enum FilterMode {\n Nearest,\n Linear,\n}\n\nexport enum MipmapMode {\n None,\n Nearest,\n Linear,\n}\n\nexport enum ImageFormat {\n JPEG = 3,\n PNG = 4,\n WEBP = 6,\n}\n\nexport interface SkImage extends SkJSIInstance<\"Image\"> {\n /**\n * Returns the possibly scaled height of the image.\n */\n height(): number;\n\n /**\n * Returns the possibly scaled width of the image.\n */\n width(): number;\n\n /**\n * Returns the ImageInfo describing the image.\n */\n getImageInfo(): ImageInfo;\n\n /**\n * Returns this image as a shader with the specified tiling. It will use cubic sampling.\n * @param tx - tile mode in the x direction.\n * @param ty - tile mode in the y direction.\n * @param fm - The filter mode. (default nearest)\n * @param mm - The mipmap mode. Note: for settings other than None, the image must have mipmaps (default none)\n * calculated with makeCopyWithDefaultMipmaps;\n * @param localMatrix\n */\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader;\n\n /**\n * Returns this image as a shader with the specified tiling. It will use cubic sampling.\n * @param tx - tile mode in the x direction.\n * @param ty - tile mode in the y direction.\n * @param B - See CubicResampler in SkSamplingOptions.h for more information\n * @param C - See CubicResampler in SkSamplingOptions.h for more information\n * @param localMatrix\n */\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader;\n\n /** Encodes Image pixels, returning result as UInt8Array. Returns existing\n encoded data if present; otherwise, SkImage is encoded with\n SkEncodedImageFormat::kPNG. Skia must be built with SK_ENCODE_PNG to encode\n SkImage.\n\n Returns nullptr if existing encoded data is missing or invalid, and\n encoding fails.\n\n @param fmt - PNG is the default value.\n @param quality - a value from 0 to 100; 100 is the least lossy. May be ignored.\n\n @return Uint8Array with data\n */\n encodeToBytes(fmt?: ImageFormat, quality?: number): Uint8Array;\n\n /** Encodes Image pixels, returning result as a base64 encoded string. Returns existing\n encoded data if present; otherwise, SkImage is encoded with\n SkEncodedImageFormat::kPNG. Skia must be built with SK_ENCODE_PNG to encode\n SkImage.\n\n Returns nullptr if existing encoded data is missing or invalid, and\n encoding fails.\n\n @param fmt - PNG is the default value.\n @param quality - a value from 0 to 100; 100 is the least lossy. May be ignored.\n\n @return base64 encoded string of data\n */\n encodeToBase64(fmt?: ImageFormat, quality?: number): string;\n\n /** Read Image pixels\n *\n * @param srcX - optional x-axis upper left corner of the rectangle to read from\n * @param srcY - optional y-axis upper left corner of the rectangle to read from\n * @param imageInfo - optional describes the pixel format and dimensions of the data to read into\n * @return Float32Array or Uint8Array with data or null if the read failed.\n */\n readPixels(\n srcX?: number,\n srcY?: number,\n imageInfo?: ImageInfo\n ): Float32Array | Uint8Array | null;\n\n /**\n * Returns raster image or lazy image. Copies SkImage backed by GPU texture\n * into CPU memory if needed. Returns original SkImage if decoded in raster\n * bitmap, or if encoded in a stream.\n */\n makeNonTextureImage(): SkImage;\n}\n"],"mappings":";;;;;;IAiBYA,UAAU,GAAAC,OAAA,CAAAD,UAAA,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAAA,IAKVE,UAAU,GAAAD,OAAA,CAAAC,UAAA,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAAA,IAMVC,WAAW,GAAAF,OAAA,CAAAE,WAAA,0BAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAA,OAAXA,WAAW;AAAA","ignoreList":[]}
1
+ {"version":3,"names":["FilterMode","exports","MipmapMode","ImageFormat"],"sources":["Image.ts"],"sourcesContent":["import type { SkMatrix } from \"../Matrix\";\nimport type { SkJSIInstance } from \"../JsiInstance\";\nimport type { TileMode } from \"../ImageFilter\";\nimport type { SkShader } from \"../Shader\";\n\nimport type { ImageInfo } from \"./ImageFactory\";\n\nexport interface CubicResampler {\n B: number;\n C: number;\n}\n\nexport interface FilterOptions {\n filter: FilterMode;\n mipmap?: MipmapMode;\n}\n\nexport enum FilterMode {\n Nearest,\n Linear,\n}\n\nexport enum MipmapMode {\n None,\n Nearest,\n Linear,\n}\n\nexport enum ImageFormat {\n JPEG = 3,\n PNG = 4,\n WEBP = 6,\n}\n\nexport interface SkImage extends SkJSIInstance<\"Image\"> {\n /**\n * Returns the possibly scaled height of the image.\n */\n height(): number;\n\n /**\n * Returns the possibly scaled width of the image.\n */\n width(): number;\n\n /**\n * Returns the ImageInfo describing the image.\n */\n getImageInfo(): ImageInfo;\n\n /**\n * Returns the backend texture of the image.\n * The returned object can be used to create a Skia Image object.\n * The returned object is backend specific and should be used with caution.\n * It is the caller's responsibility to ensure that the texture is not used after the image is deleted.\n * The returned object may be null if the image does not have a backend texture.\n *\n * @return backend texture of the image or null\n */\n getNativeTextureUnstable(): unknown;\n\n /**\n * Returns this image as a shader with the specified tiling. It will use cubic sampling.\n * @param tx - tile mode in the x direction.\n * @param ty - tile mode in the y direction.\n * @param fm - The filter mode. (default nearest)\n * @param mm - The mipmap mode. Note: for settings other than None, the image must have mipmaps (default none)\n * calculated with makeCopyWithDefaultMipmaps;\n * @param localMatrix\n */\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader;\n\n /**\n * Returns this image as a shader with the specified tiling. It will use cubic sampling.\n * @param tx - tile mode in the x direction.\n * @param ty - tile mode in the y direction.\n * @param B - See CubicResampler in SkSamplingOptions.h for more information\n * @param C - See CubicResampler in SkSamplingOptions.h for more information\n * @param localMatrix\n */\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader;\n\n /** Encodes Image pixels, returning result as UInt8Array. Returns existing\n encoded data if present; otherwise, SkImage is encoded with\n SkEncodedImageFormat::kPNG. Skia must be built with SK_ENCODE_PNG to encode\n SkImage.\n\n Returns nullptr if existing encoded data is missing or invalid, and\n encoding fails.\n\n @param fmt - PNG is the default value.\n @param quality - a value from 0 to 100; 100 is the least lossy. May be ignored.\n\n @return Uint8Array with data\n */\n encodeToBytes(fmt?: ImageFormat, quality?: number): Uint8Array;\n\n /** Encodes Image pixels, returning result as a base64 encoded string. Returns existing\n encoded data if present; otherwise, SkImage is encoded with\n SkEncodedImageFormat::kPNG. Skia must be built with SK_ENCODE_PNG to encode\n SkImage.\n\n Returns nullptr if existing encoded data is missing or invalid, and\n encoding fails.\n\n @param fmt - PNG is the default value.\n @param quality - a value from 0 to 100; 100 is the least lossy. May be ignored.\n\n @return base64 encoded string of data\n */\n encodeToBase64(fmt?: ImageFormat, quality?: number): string;\n\n /** Read Image pixels\n *\n * @param srcX - optional x-axis upper left corner of the rectangle to read from\n * @param srcY - optional y-axis upper left corner of the rectangle to read from\n * @param imageInfo - optional describes the pixel format and dimensions of the data to read into\n * @return Float32Array or Uint8Array with data or null if the read failed.\n */\n readPixels(\n srcX?: number,\n srcY?: number,\n imageInfo?: ImageInfo\n ): Float32Array | Uint8Array | null;\n\n /**\n * Returns raster image or lazy image. Copies SkImage backed by GPU texture\n * into CPU memory if needed. Returns original SkImage if decoded in raster\n * bitmap, or if encoded in a stream.\n */\n makeNonTextureImage(): SkImage;\n}\n"],"mappings":";;;;;;IAiBYA,UAAU,GAAAC,OAAA,CAAAD,UAAA,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAAA,IAKVE,UAAU,GAAAD,OAAA,CAAAC,UAAA,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAAA,IAMVC,WAAW,GAAAF,OAAA,CAAAE,WAAA,0BAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAXA,WAAW,CAAXA,WAAW;EAAA,OAAXA,WAAW;AAAA","ignoreList":[]}
@@ -41,6 +41,24 @@ export interface ImageFactory {
41
41
  * native buffer is invalid.
42
42
  */
43
43
  MakeImageFromNativeBuffer: (nativeBuffer: NativeBuffer) => SkImage;
44
+ /**
45
+ *
46
+ * Return an Image backed by a given native texture.
47
+ *
48
+ * The native texture must be a valid owning reference.
49
+ *
50
+ * This API might be used to integrate with other libraries using gpu textures,
51
+ * or to transfer images between different threads.
52
+ *
53
+ * @param texture A native texture handle
54
+ * @param width The width of the texture
55
+ * @param height The height of the texture
56
+ * @param mipmapped Whether the texture is mipmapped
57
+ * @throws Throws an error if the Image could not be created, for example when the given native texture is invalid.
58
+ *
59
+ * @returns Returns a valid SkImage, if the texture is invalid, an error is thrown.
60
+ */
61
+ MakeImageFromNativeTextureUnstable: (texture: unknown, width: number, height: number, mipmapped?: boolean) => SkImage;
44
62
  /**
45
63
  * Returns an image that will be a screenshot of the view represented by
46
64
  * the view tag
@@ -1 +1 @@
1
- {"version":3,"names":["AlphaType","exports"],"sources":["ImageFactory.ts"],"sourcesContent":["import type { SkData } from \"../Data\";\nimport type { NativeBuffer } from \"../NativeBuffer\";\n\nimport type { ColorType } from \"./ColorType\";\nimport type { SkImage } from \"./Image\";\n\nexport enum AlphaType {\n Unknown,\n Opaque,\n Premul,\n Unpremul,\n}\n\nexport interface ImageInfo {\n alphaType: AlphaType;\n // TODO: add support for color space\n // colorSpace: ColorSpace;\n colorType: ColorType;\n height: number;\n width: number;\n}\n\nexport interface ImageFactory {\n /**\n * Return an Image backed by the encoded data, but attempt to defer decoding until the image\n * is actually used/drawn. This deferral allows the system to cache the result, either on the\n * CPU or on the GPU, depending on where the image is drawn.\n * This decoding uses the codecs that have been compiled into CanvasKit. If the bytes are\n * invalid (or an unrecognized codec), null will be returned. See Image.h for more details.\n * @param data - Data object with bytes of data\n * @returns If the encoded format is not supported, or subset is outside of the bounds of the decoded\n * image, nullptr is returned.\n */\n MakeImageFromEncoded: (encoded: SkData) => SkImage | null;\n\n /**\n * Return an Image backed by a given native buffer.\n * The native buffer must be a valid owning reference.\n *\n * For instance, this API is used by\n * [react-native-vision-camera](https://github.com/mrousavy/react-native-vision-camera)\n * to render a Skia Camera preview.\n *\n * - On Android; This is an `AHardwareBuffer*`\n * - On iOS, this is a `CVPixelBufferRef`\n * @param nativeBuffer A strong `uintptr_t` pointer to the native buffer\n * @throws Throws an error if the Image could not be created, for example when the given\n * native buffer is invalid.\n */\n MakeImageFromNativeBuffer: (nativeBuffer: NativeBuffer) => SkImage;\n\n /**\n * Returns an image that will be a screenshot of the view represented by\n * the view tag\n * @param viewTag - The tag of the view to make an image from.\n * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.\n */\n MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;\n\n /**\n * Returns an image with the given pixel data and format.\n * Note that we will always make a copy of the pixel data, because of inconsistencies in\n * behavior between GPU and CPU (i.e. the pixel data will be turned into a GPU texture and\n * not modifiable after creation).\n *\n * @param info\n * @param data - bytes representing the pixel data.\n * @param bytesPerRow\n */\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): SkImage | null;\n}\n"],"mappings":";;;;;;IAMYA,SAAS,GAAAC,OAAA,CAAAD,SAAA,0BAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAAA,OAATA,SAAS;AAAA","ignoreList":[]}
1
+ {"version":3,"names":["AlphaType","exports"],"sources":["ImageFactory.ts"],"sourcesContent":["import type { SkData } from \"../Data\";\nimport type { NativeBuffer } from \"../NativeBuffer\";\n\nimport type { ColorType } from \"./ColorType\";\nimport type { SkImage } from \"./Image\";\n\nexport enum AlphaType {\n Unknown,\n Opaque,\n Premul,\n Unpremul,\n}\n\nexport interface ImageInfo {\n alphaType: AlphaType;\n // TODO: add support for color space\n // colorSpace: ColorSpace;\n colorType: ColorType;\n height: number;\n width: number;\n}\n\nexport interface ImageFactory {\n /**\n * Return an Image backed by the encoded data, but attempt to defer decoding until the image\n * is actually used/drawn. This deferral allows the system to cache the result, either on the\n * CPU or on the GPU, depending on where the image is drawn.\n * This decoding uses the codecs that have been compiled into CanvasKit. If the bytes are\n * invalid (or an unrecognized codec), null will be returned. See Image.h for more details.\n * @param data - Data object with bytes of data\n * @returns If the encoded format is not supported, or subset is outside of the bounds of the decoded\n * image, nullptr is returned.\n */\n MakeImageFromEncoded: (encoded: SkData) => SkImage | null;\n\n /**\n * Return an Image backed by a given native buffer.\n * The native buffer must be a valid owning reference.\n *\n * For instance, this API is used by\n * [react-native-vision-camera](https://github.com/mrousavy/react-native-vision-camera)\n * to render a Skia Camera preview.\n *\n * - On Android; This is an `AHardwareBuffer*`\n * - On iOS, this is a `CVPixelBufferRef`\n * @param nativeBuffer A strong `uintptr_t` pointer to the native buffer\n * @throws Throws an error if the Image could not be created, for example when the given\n * native buffer is invalid.\n */\n MakeImageFromNativeBuffer: (nativeBuffer: NativeBuffer) => SkImage;\n\n /**\n *\n * Return an Image backed by a given native texture.\n *\n * The native texture must be a valid owning reference.\n *\n * This API might be used to integrate with other libraries using gpu textures,\n * or to transfer images between different threads.\n *\n * @param texture A native texture handle\n * @param width The width of the texture\n * @param height The height of the texture\n * @param mipmapped Whether the texture is mipmapped\n * @throws Throws an error if the Image could not be created, for example when the given native texture is invalid.\n *\n * @returns Returns a valid SkImage, if the texture is invalid, an error is thrown.\n */\n MakeImageFromNativeTextureUnstable: (\n texture: unknown,\n width: number,\n height: number,\n mipmapped?: boolean\n ) => SkImage;\n\n /**\n * Returns an image that will be a screenshot of the view represented by\n * the view tag\n * @param viewTag - The tag of the view to make an image from.\n * @returns Returns a valid SkImage, if the view tag is invalid, nullptr is returned.\n */\n MakeImageFromViewTag: (viewTag: number) => Promise<SkImage | null>;\n\n /**\n * Returns an image with the given pixel data and format.\n * Note that we will always make a copy of the pixel data, because of inconsistencies in\n * behavior between GPU and CPU (i.e. the pixel data will be turned into a GPU texture and\n * not modifiable after creation).\n *\n * @param info\n * @param data - bytes representing the pixel data.\n * @param bytesPerRow\n */\n MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number): SkImage | null;\n}\n"],"mappings":";;;;;;IAMYA,SAAS,GAAAC,OAAA,CAAAD,SAAA,0BAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAATA,SAAS,CAATA,SAAS;EAAA,OAATA,SAAS;AAAA","ignoreList":[]}