Skip to content

Commit 52f2e44

Browse files
authored
chore(🖼️): minor refactoring in platform context (#2795)
1 parent cc15a30 commit 52f2e44

File tree

8 files changed

+113
-74
lines changed

8 files changed

+113
-74
lines changed

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

+19-35
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "MainThreadDispatcher.h"
2121
#include "RNSkAndroidVideo.h"
2222
#include "RNSkPlatformContext.h"
23-
2423
#pragma clang diagnostic push
2524
#pragma clang diagnostic ignored "-Wdocumentation"
2625

@@ -78,26 +77,15 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
7877
#endif
7978
}
8079

81-
sk_sp<SkImage> makeImageFromNativeTexture(jsi::Runtime &runtime,
82-
jsi::Value jsiTextureInfo,
80+
sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &texInfo,
8381
int width, int height,
8482
bool mipMapped) override {
85-
if (!jsiTextureInfo.isObject()) {
86-
throw new std::runtime_error("Invalid textureInfo");
87-
}
88-
auto jsiTextureInfoObj = jsiTextureInfo.asObject(runtime);
89-
9083
GrGLTextureInfo textureInfo;
91-
textureInfo.fTarget =
92-
(GrGLenum)jsiTextureInfoObj.getProperty(runtime, "fTarget").asNumber();
93-
textureInfo.fID =
94-
(GrGLuint)jsiTextureInfoObj.getProperty(runtime, "fID").asNumber();
95-
textureInfo.fFormat =
96-
(GrGLenum)jsiTextureInfoObj.getProperty(runtime, "fFormat").asNumber();
84+
textureInfo.fTarget = (GrGLenum)texInfo.glTarget;
85+
textureInfo.fID = (GrGLuint)texInfo.glID;
86+
textureInfo.fFormat = (GrGLenum)texInfo.glFormat;
9787
textureInfo.fProtected =
98-
jsiTextureInfoObj.getProperty(runtime, "fProtected").asBool()
99-
? skgpu::Protected::kYes
100-
: skgpu::Protected::kNo;
88+
texInfo.glProtected ? skgpu::Protected::kYes : skgpu::Protected::kNo;
10189

10290
OpenGLContext::getInstance().makeCurrent();
10391
if (glIsTexture(textureInfo.fID) == GL_FALSE) {
@@ -185,42 +173,38 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
185173
#endif
186174
}
187175

188-
jsi::Value getTexture(jsi::Runtime &runtime, sk_sp<SkImage> image) override {
176+
const TextureInfo getTexture(sk_sp<SkImage> image) override {
189177
GrBackendTexture texture;
190178
if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
191-
return jsi::Value::null();
179+
throw std::runtime_error("Couldn't get backend texture from image.");
192180
}
193-
return getJSITextureInfo(runtime, texture);
181+
return getJSITextureInfo(texture);
194182
}
195183

196-
jsi::Value getTexture(jsi::Runtime &runtime,
197-
sk_sp<SkSurface> surface) override {
184+
const TextureInfo getTexture(sk_sp<SkSurface> surface) override {
198185
GrBackendTexture texture = SkSurfaces::GetBackendTexture(
199186
surface.get(), SkSurface::BackendHandleAccess::kFlushRead);
200-
return getJSITextureInfo(runtime, texture);
187+
return getJSITextureInfo(texture);
201188
}
202189

203-
static jsi::Value getJSITextureInfo(jsi::Runtime &runtime,
204-
const GrBackendTexture &texture) {
190+
static const TextureInfo getJSITextureInfo(const GrBackendTexture &texture) {
205191
if (!texture.isValid()) {
206-
return jsi::Value::null();
192+
throw std::runtime_error("invalid backend texture");
207193
}
208194
GrGLTextureInfo textureInfo;
209195
if (!GrBackendTextures::GetGLTextureInfo(texture, &textureInfo)) {
210-
return jsi::Value::null();
196+
throw std::runtime_error("couldn't get OpenGL texture");
211197
}
212198

213199
OpenGLContext::getInstance().makeCurrent();
214200
glFlush();
215201

216-
jsi::Object jsiTextureInfo = jsi::Object(runtime);
217-
jsiTextureInfo.setProperty(runtime, "fTarget", (int)textureInfo.fTarget);
218-
jsiTextureInfo.setProperty(runtime, "fFormat", (int)textureInfo.fFormat);
219-
jsiTextureInfo.setProperty(runtime, "fID", (int)textureInfo.fID);
220-
jsiTextureInfo.setProperty(runtime, "fProtected",
221-
(bool)textureInfo.fProtected);
222-
223-
return jsiTextureInfo;
202+
TextureInfo texInfo;
203+
texInfo.glProtected = textureInfo.isProtected();
204+
texInfo.glID = textureInfo.fID;
205+
texInfo.glFormat = textureInfo.fFormat;
206+
texInfo.glTarget = textureInfo.fTarget;
207+
return texInfo;
224208
}
225209

226210
#if !defined(SK_GRAPHITE)

packages/skia/cpp/api/JsiSkImage.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "JsiSkShader.h"
1111
#include "third_party/base64.h"
1212

13+
#include "JsiTextureInfo.h"
1314
#include "RNSkTypedArray.h"
1415

1516
#if defined(SK_GRAPHITE)
@@ -220,7 +221,8 @@ class JsiSkImage : public JsiSkWrappingSkPtrHostObject<SkImage> {
220221
if (!image->isTextureBacked()) {
221222
return jsi::Value::null();
222223
}
223-
return getContext()->getTexture(runtime, image);
224+
auto texInfo = getContext()->getTexture(image);
225+
return JsiTextureInfo::toValue(runtime, texInfo);
224226
}
225227

226228
EXPORT_JSI_API_TYPENAME(JsiSkImage, Image)

packages/skia/cpp/api/JsiSkImageFactory.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ class JsiSkImageFactory : public JsiSkHostObject {
7979
}
8080

8181
JSI_HOST_FUNCTION(MakeImageFromNativeTextureUnstable) {
82+
auto texInfo = JsiTextureInfo::fromValue(runtime, arguments[0]);
8283
auto image = getContext()->makeImageFromNativeTexture(
83-
runtime, jsi::Value(runtime, arguments[0]), arguments[1].asNumber(),
84-
arguments[2].asNumber(), count > 3 && arguments[3].asBool());
84+
texInfo, arguments[1].asNumber(), arguments[2].asNumber(),
85+
count > 3 && arguments[3].asBool());
8586
if (image == nullptr) {
8687
throw std::runtime_error("Failed to convert native texture to SkImage!");
8788
}

packages/skia/cpp/api/JsiSkSurface.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <jsi/jsi.h>
77

88
#include "JsiSkHostObjects.h"
9+
#include "JsiTextureInfo.h"
910

1011
#include "JsiSkCanvas.h"
1112
#include "JsiSkImage.h"
@@ -79,7 +80,8 @@ class JsiSkSurface : public JsiSkWrappingSkPtrHostObject<SkSurface> {
7980
}
8081

8182
JSI_HOST_FUNCTION(getNativeTextureUnstable) {
82-
return getContext()->getTexture(runtime, getObject());
83+
auto texInfo = getContext()->getTexture(getObject());
84+
return JsiTextureInfo::toValue(runtime, texInfo);
8385
}
8486

8587
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkSurface, width),
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#include <jsi/jsi.h>
4+
5+
#include "RNSkPlatformContext.h"
6+
7+
namespace jsi = facebook::jsi;
8+
namespace react = facebook::react;
9+
10+
namespace RNSkia {
11+
12+
namespace JsiTextureInfo {
13+
14+
inline jsi::Value toValue(jsi::Runtime &runtime, const TextureInfo &texInfo) {
15+
jsi::Object textureInfo(runtime);
16+
textureInfo.setProperty(
17+
runtime, "mtlTexture",
18+
jsi::BigInt::fromUint64(runtime,
19+
reinterpret_cast<uint64_t>(texInfo.mtlTexture)));
20+
textureInfo.setProperty(runtime, "glTarget",
21+
static_cast<int>(texInfo.glTarget));
22+
textureInfo.setProperty(runtime, "glID", static_cast<int>(texInfo.glID));
23+
textureInfo.setProperty(runtime, "glFormat",
24+
static_cast<int>(texInfo.glFormat));
25+
textureInfo.setProperty(runtime, "glProtected",
26+
static_cast<int>(texInfo.glProtected));
27+
return textureInfo;
28+
}
29+
30+
inline TextureInfo fromValue(jsi::Runtime &runtime, const jsi::Value &value) {
31+
auto object = value.getObject(runtime);
32+
TextureInfo texInfo;
33+
if (object.hasProperty(runtime, "mtlTexture")) {
34+
texInfo.mtlTexture =
35+
reinterpret_cast<const void *>(object.getProperty(runtime, "mtlTexture")
36+
.asBigInt(runtime)
37+
.asUint64(runtime));
38+
}
39+
if (object.hasProperty(runtime, "glID")) {
40+
texInfo.glTarget = static_cast<unsigned int>(
41+
object.getProperty(runtime, "glTarget").asNumber());
42+
texInfo.glID = static_cast<unsigned int>(
43+
object.getProperty(runtime, "glID").asNumber());
44+
texInfo.glFormat = static_cast<unsigned int>(
45+
object.getProperty(runtime, "glFormat").asNumber());
46+
texInfo.glProtected =
47+
object.getProperty(runtime, "glProtected").asNumber() != 0;
48+
}
49+
return texInfo;
50+
}
51+
52+
} // namespace JsiTextureInfo
53+
} // namespace RNSkia

packages/skia/cpp/rnskia/RNSkPlatformContext.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@
2323

2424
#pragma clang diagnostic pop
2525

26-
#include <jsi/jsi.h>
27-
2826
#include <ReactCommon/CallInvoker.h>
2927

3028
namespace RNSkia {
3129

3230
namespace jsi = facebook::jsi;
3331
namespace react = facebook::react;
3432

33+
struct TextureInfo {
34+
const void *mtlTexture = nullptr;
35+
unsigned int glTarget = 0;
36+
unsigned int glID = 0;
37+
unsigned int glFormat = 0;
38+
bool glProtected = false;
39+
};
40+
3541
class RNSkPlatformContext {
3642
public:
3743
/**
@@ -100,10 +106,9 @@ class RNSkPlatformContext {
100106
*/
101107
virtual sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) = 0;
102108

103-
virtual sk_sp<SkImage> makeImageFromNativeTexture(jsi::Runtime &runtime,
104-
jsi::Value textureInfo,
105-
int width, int height,
106-
bool mipMapped) = 0;
109+
virtual sk_sp<SkImage>
110+
makeImageFromNativeTexture(const TextureInfo &textureInfo, int width,
111+
int height, bool mipMapped) = 0;
107112

108113
#if !defined(SK_GRAPHITE)
109114
virtual GrDirectContext *getDirectContext() = 0;
@@ -113,11 +118,9 @@ class RNSkPlatformContext {
113118

114119
virtual uint64_t makeNativeBuffer(sk_sp<SkImage> image) = 0;
115120

116-
virtual jsi::Value getTexture(jsi::Runtime &runtime,
117-
sk_sp<SkSurface> image) = 0;
121+
virtual const TextureInfo getTexture(sk_sp<SkSurface> image) = 0;
118122

119-
virtual jsi::Value getTexture(jsi::Runtime &runtime,
120-
sk_sp<SkImage> image) = 0;
123+
virtual const TextureInfo getTexture(sk_sp<SkImage> image) = 0;
121124

122125
virtual std::shared_ptr<RNSkVideo> createVideo(const std::string &url) = 0;
123126

packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ class RNSkiOSPlatformContext : public RNSkPlatformContext {
4242

4343
sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override;
4444

45-
sk_sp<SkImage> makeImageFromNativeTexture(jsi::Runtime &runtime,
46-
jsi::Value textureInfo, int width,
47-
int height,
45+
sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &textureInfo,
46+
int width, int height,
4847
bool mipMapped) override;
4948

5049
uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
5150

52-
jsi::Value getTexture(jsi::Runtime &runtime, sk_sp<SkSurface> image) override;
51+
const TextureInfo getTexture(sk_sp<SkSurface> image) override;
5352

54-
jsi::Value getTexture(jsi::Runtime &runtime, sk_sp<SkImage> image) override;
53+
const TextureInfo getTexture(sk_sp<SkImage> image) override;
5554

5655
void releaseNativeBuffer(uint64_t pointer) override;
5756

packages/skia/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm

+15-20
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,36 @@
156156
return reinterpret_cast<uint64_t>(pixelBuffer);
157157
}
158158

159-
jsi::Value RNSkiOSPlatformContext::getTexture(jsi::Runtime &runtime,
160-
sk_sp<SkImage> image) {
159+
const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp<SkImage> image) {
161160
GrBackendTexture texture;
161+
TextureInfo result;
162162
if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
163-
return jsi::Value::null();
163+
throw std::runtime_error("Couldn't get backend texture");
164164
}
165165
if (!texture.isValid()) {
166-
return jsi::Value::null();
166+
throw std::runtime_error("Invalid backend texture");
167167
}
168168
GrMtlTextureInfo textureInfo;
169169
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
170-
return jsi::Value::null();
170+
throw std::runtime_error("Couldn't get Metal texture info");
171171
}
172-
auto pointer = reinterpret_cast<uint64_t>(textureInfo.fTexture.get());
173-
return jsi::BigInt::fromUint64(runtime, pointer);
172+
result.mtlTexture = textureInfo.fTexture.get();
173+
return result;
174174
}
175175

176-
jsi::Value RNSkiOSPlatformContext::getTexture(jsi::Runtime &runtime,
177-
sk_sp<SkSurface> surface) {
176+
const TextureInfo RNSkiOSPlatformContext::getTexture(sk_sp<SkSurface> surface) {
178177
GrBackendTexture texture = SkSurfaces::GetBackendTexture(
179178
surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
179+
TextureInfo result;
180180
if (!texture.isValid()) {
181-
return jsi::Value::null();
181+
throw std::runtime_error("Invalid backend texture");
182182
}
183183
GrMtlTextureInfo textureInfo;
184184
if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
185-
return jsi::Value::null();
185+
throw std::runtime_error("Couldn't get Metal texture info");
186186
}
187-
auto pointer = reinterpret_cast<uint64_t>(textureInfo.fTexture.get());
188-
return jsi::BigInt::fromUint64(runtime, pointer);
187+
result.mtlTexture = textureInfo.fTexture.get();
188+
return result;
189189
}
190190

191191
std::shared_ptr<RNSkVideo>
@@ -226,13 +226,8 @@
226226
}
227227

228228
sk_sp<SkImage> RNSkiOSPlatformContext::makeImageFromNativeTexture(
229-
jsi::Runtime &runtime, jsi::Value jsiTextureInfo, int width, int height,
230-
bool mipMapped) {
231-
if (!jsiTextureInfo.isBigInt()) {
232-
throw std::runtime_error("Invalid textureInfo");
233-
}
234-
auto pointer = (void *)jsiTextureInfo.asBigInt(runtime).asUint64(runtime);
235-
id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(pointer);
229+
const TextureInfo &texInfo, int width, int height, bool mipMapped) {
230+
id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(texInfo.mtlTexture);
236231

237232
SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat);
238233
if (colorType == SkColorType::kUnknown_SkColorType) {

0 commit comments

Comments
 (0)