1111#include " ImageBitmap.h"
1212#include " PlatformContext.h"
1313
14+ #include < ReactCommon/CallInvoker.h>
15+
1416#include " JSIConverter.h"
15- #include " rnwgpu/async/AsyncRunner.h"
16- #include " rnwgpu/async/AsyncTaskHandle.h"
17+ #include " Promise.h"
1718
1819namespace rnwgpu {
1920
@@ -27,15 +28,41 @@ struct Blob {
2728 std::string name;
2829};
2930
31+ // JSIConverter specialization must be declared before use
32+ template <> struct JSIConverter <std::shared_ptr<Blob>> {
33+ static std::shared_ptr<Blob>
34+ fromJSI (jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
35+ if (!outOfBounds && arg.isObject ()) {
36+ auto result = std::make_unique<Blob>();
37+ auto val = arg.asObject (runtime);
38+ if (val.hasProperty (runtime, " _data" )) {
39+ auto value = val.getPropertyAsObject (runtime, " _data" );
40+ result->blobId = JSIConverter<std::string>::fromJSI (
41+ runtime, value.getProperty (runtime, " blobId" ), false );
42+ result->size = JSIConverter<double >::fromJSI (
43+ runtime, value.getProperty (runtime, " size" ), false );
44+ result->offset = JSIConverter<double >::fromJSI (
45+ runtime, value.getProperty (runtime, " offset" ), false );
46+ }
47+ return result;
48+ } else {
49+ throw std::runtime_error (" Invalid Blob::fromJSI()" );
50+ }
51+ }
52+ static jsi::Value toJSI (jsi::Runtime &runtime, std::shared_ptr<Blob> arg) {
53+ throw std::runtime_error (" Invalid Blob::toJSI()" );
54+ }
55+ };
56+
3057class RNWebGPU : public NativeObject <RNWebGPU> {
3158public:
3259 static constexpr const char *CLASS_NAME = " RNWebGPU" ;
3360
3461 explicit RNWebGPU (std::shared_ptr<GPU > gpu,
3562 std::shared_ptr<PlatformContext> platformContext,
36- std::shared_ptr<async::AsyncRunner> asyncRunner )
63+ std::shared_ptr<facebook::react::CallInvoker> callInvoker )
3764 : NativeObject(CLASS_NAME ), _gpu(gpu), _platformContext(platformContext),
38- _async(asyncRunner ) {}
65+ _callInvoker(callInvoker ) {}
3966
4067 std::shared_ptr<GPU > getGPU () { return _gpu; }
4168
@@ -48,29 +75,41 @@ class RNWebGPU : public NativeObject<RNWebGPU> {
4875 return ctx;
4976 }
5077
51- async::AsyncTaskHandle createImageBitmap (std::shared_ptr<Blob> blob) {
78+ jsi::Value createImageBitmap (jsi::Runtime &runtime,
79+ const jsi::Value & /* thisVal*/ ,
80+ const jsi::Value *args, size_t count) {
81+ if (count < 1 ) {
82+ throw jsi::JSError (runtime, " createImageBitmap requires a Blob argument" );
83+ }
84+
85+ auto blob =
86+ JSIConverter<std::shared_ptr<Blob>>::fromJSI (runtime, args[0 ], false );
5287 auto platformContext = _platformContext;
88+ auto callInvoker = _callInvoker;
5389 std::string blobId = blob->blobId ;
5490 double offset = blob->offset ;
5591 double size = blob->size ;
5692
57- return _async-> postTask (
58- [platformContext, blobId, offset ,
59- size]( const async::AsyncTaskHandle::ResolveFunction &resolve ,
60- const async::AsyncTaskHandle::RejectFunction &reject ) {
93+ return Promise::createPromise (
94+ runtime ,
95+ [platformContext, callInvoker, blobId, offset ,
96+ size](jsi::Runtime & /* runtime */ , std::shared_ptr<Promise> promise ) {
6197 platformContext->createImageBitmapAsync (
6298 blobId, offset, size,
63- [resolve ](ImageData imageData) {
99+ [callInvoker, promise ](ImageData imageData) {
64100 auto imageBitmap = std::make_shared<ImageBitmap>(imageData);
65- resolve ([imageBitmap](jsi::Runtime &runtime) {
66- return JSIConverter<std::shared_ptr<ImageBitmap>>::toJSI (
67- runtime, imageBitmap);
68- });
101+ callInvoker->invokeAsync (
102+ [promise, imageBitmap]() {
103+ promise->resolve (
104+ JSIConverter<std::shared_ptr<ImageBitmap>>::toJSI (
105+ promise->runtime , imageBitmap));
106+ });
69107 },
70- [reject](std::string error) { reject (error); });
71- },
72- false // Don't keep pumping - this is a one-shot async operation
73- );
108+ [callInvoker, promise](std::string error) {
109+ callInvoker->invokeAsync (
110+ [promise, error]() { promise->reject (error); });
111+ });
112+ });
74113 }
75114
76115 std::shared_ptr<Canvas> getNativeSurface (int contextId) {
@@ -98,36 +137,7 @@ class RNWebGPU : public NativeObject<RNWebGPU> {
98137private:
99138 std::shared_ptr<GPU > _gpu;
100139 std::shared_ptr<PlatformContext> _platformContext;
101- std::shared_ptr<async::AsyncRunner> _async;
102- };
103-
104- template <> struct JSIConverter <std::shared_ptr<Blob>> {
105- static std::shared_ptr<Blob>
106- fromJSI (jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
107- if (!outOfBounds && arg.isObject ()) {
108- auto result = std::make_unique<Blob>();
109- auto val = arg.asObject (runtime);
110- if (val.hasProperty (runtime, " _data" )) {
111- auto value = val.getPropertyAsObject (runtime, " _data" );
112- result->blobId = JSIConverter<std::string>::fromJSI (
113- runtime, value.getProperty (runtime, " blobId" ), false );
114- // result->type = JSIConverter<std::string>::fromJSI(
115- // runtime, value.getProperty(runtime, "type"), false);
116- // result->name = JSIConverter<std::string>::fromJSI(
117- // runtime, value.getProperty(runtime, "name"), false);
118- result->size = JSIConverter<double >::fromJSI (
119- runtime, value.getProperty (runtime, " size" ), false );
120- result->offset = JSIConverter<double >::fromJSI (
121- runtime, value.getProperty (runtime, " offset" ), false );
122- }
123- return result;
124- } else {
125- throw std::runtime_error (" Invalid Blob::fromJSI()" );
126- }
127- }
128- static jsi::Value toJSI (jsi::Runtime &runtime, std::shared_ptr<Blob> arg) {
129- throw std::runtime_error (" Invalid Blob::toJSI()" );
130- }
140+ std::shared_ptr<facebook::react::CallInvoker> _callInvoker;
131141};
132142
133143} // namespace rnwgpu
0 commit comments