Skip to content

Commit 6c217b1

Browse files
authored
chore(🗑️): avoid spurious Hermes GC cycles from canvas texture memory pressure (#3803)
1 parent 8eb0c1e commit 6c217b1

34 files changed

+275
-239
lines changed

‎packages/skia/android/cpp/jni/JniWebGPUView.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include <jni.h>
21
#include <android/native_window_jni.h>
2+
#include <jni.h>
33

44
#ifdef SK_GRAPHITE
5-
#include "webgpu/webgpu_cpp.h"
6-
#include "rnwgpu/SurfaceRegistry.h"
75
#include "rnskia/RNDawnContext.h"
6+
#include "rnwgpu/SurfaceRegistry.h"
7+
#include "webgpu/webgpu_cpp.h"
88
#endif
99

1010
extern "C" JNIEXPORT void JNICALL

‎packages/skia/android/cpp/rnskia-android/OpenGLContext.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class OpenGLContext {
5959
}
6060

6161
sk_sp<SkSurface> MakeOffscreen(int width, int height,
62-
bool useP3ColorSpace = false) {
62+
bool useP3ColorSpace = false) {
6363
auto colorType = kRGBA_8888_SkColorType;
6464

6565
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
@@ -91,10 +91,9 @@ class OpenGLContext {
9191
.texture = texture};
9292

9393
sk_sp<SkColorSpace> colorSpace =
94-
useP3ColorSpace
95-
? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
96-
SkNamedGamut::kDisplayP3)
97-
: nullptr;
94+
useP3ColorSpace ? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
95+
SkNamedGamut::kDisplayP3)
96+
: nullptr;
9897

9998
// Create a SkSurface from the GrBackendTexture
10099
return SkSurfaces::WrapBackendTexture(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class RNSkAndroidPlatformContext : public RNSkPlatformContext {
5151
}
5252

5353
sk_sp<SkSurface> makeOffscreenSurface(int width, int height,
54-
bool useP3ColorSpace = false) override {
54+
bool useP3ColorSpace = false) override {
5555
#if defined(SK_GRAPHITE)
5656
return DawnContext::getInstance().MakeOffscreen(width, height,
5757
useP3ColorSpace);

‎packages/skia/apple/MetalContext.h‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "include/core/SkSurface.h"
77

8+
#include "include/core/SkColorSpace.h"
89
#import <include/gpu/ganesh/GrBackendSurface.h>
910
#import <include/gpu/ganesh/GrDirectContext.h>
1011
#import <include/gpu/ganesh/SkImageGanesh.h>
@@ -13,7 +14,6 @@
1314
#import <include/gpu/ganesh/mtl/GrMtlBackendSurface.h>
1415
#import <include/gpu/ganesh/mtl/GrMtlDirectContext.h>
1516
#import <include/gpu/ganesh/mtl/SkSurfaceMetal.h>
16-
#include "include/core/SkColorSpace.h"
1717

1818
struct OffscreenRenderContext {
1919
id<MTLTexture> texture;
@@ -46,7 +46,7 @@ class MetalContext {
4646
}
4747

4848
sk_sp<SkSurface> MakeOffscreen(int width, int height,
49-
bool useP3ColorSpace = false) {
49+
bool useP3ColorSpace = false) {
5050
auto device = _device;
5151
auto ctx = new OffscreenRenderContext(device, _directContext, _commandQueue,
5252
width, height);
@@ -58,10 +58,9 @@ class MetalContext {
5858
GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped::kNo, info);
5959

6060
sk_sp<SkColorSpace> colorSpace =
61-
useP3ColorSpace
62-
? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
63-
SkNamedGamut::kDisplayP3)
64-
: nullptr;
61+
useP3ColorSpace ? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
62+
SkNamedGamut::kDisplayP3)
63+
: nullptr;
6564

6665
// Create a SkSurface from the GrBackendTexture
6766
auto surface = SkSurfaces::WrapBackendTexture(

‎packages/skia/apple/MetalWindowContext.mm‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@
7272
_layer.drawableSize.width, _layer.drawableSize.height, fbInfo);
7373

7474
sk_sp<SkColorSpace> skColorSpace =
75-
_useP3ColorSpace
76-
? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
77-
SkNamedGamut::kDisplayP3)
78-
: nullptr;
75+
_useP3ColorSpace ? SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
76+
SkNamedGamut::kDisplayP3)
77+
: nullptr;
7978
_skSurface = SkSurfaces::WrapBackendRenderTarget(
8079
_directContext, backendRT, kTopLeft_GrSurfaceOrigin,
8180
kBGRA_8888_SkColorType, skColorSpace, nullptr);

‎packages/skia/apple/RNSkApplePlatformContext.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class RNSkApplePlatformContext : public RNSkPlatformContext {
7070

7171
void raiseError(const std::exception &err) override;
7272
sk_sp<SkSurface> makeOffscreenSurface(int width, int height,
73-
bool useP3ColorSpace = false) override;
73+
bool useP3ColorSpace = false) override;
7474

7575
sk_sp<SkFontMgr> createFontMgr() override;
7676

‎packages/skia/apple/RNSkApplePlatformContext.mm‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@
251251
RCTFatal(RCTErrorWithMessage([NSString stringWithUTF8String:err.what()]));
252252
}
253253

254-
sk_sp<SkSurface> RNSkApplePlatformContext::makeOffscreenSurface(
255-
int width, int height, bool useP3ColorSpace) {
254+
sk_sp<SkSurface>
255+
RNSkApplePlatformContext::makeOffscreenSurface(int width, int height,
256+
bool useP3ColorSpace) {
256257
#if defined(SK_GRAPHITE)
257258
return DawnContext::getInstance().MakeOffscreen(width, height,
258-
useP3ColorSpace);
259+
useP3ColorSpace);
259260
#else
260261
return MetalContext::getInstance().MakeOffscreen(width, height,
261262
useP3ColorSpace);

‎packages/skia/apple/SkiaCVPixelBufferUtils.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#import <vector>
1111

12-
#import <CoreVideo/CVPixelBuffer.h>
1312
#import <CoreVideo/CVMetalTextureCache.h>
13+
#import <CoreVideo/CVPixelBuffer.h>
1414
#import <MetalKit/MetalKit.h>
1515

1616
#pragma clang diagnostic push

‎packages/skia/apple/SkiaCVPixelBufferUtils.mm‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@
3131
#include <cmath>
3232
#if TARGET_RT_BIG_ENDIAN
3333
#define FourCC2Str(fourcc) \
34-
(const char[]) { \
35-
*((char *)&fourcc), *(((char *)&fourcc) + 1), *(((char *)&fourcc) + 2), \
36-
*(((char *)&fourcc) + 3), 0 \
37-
}
34+
(const char[]){*((char *)&fourcc), *(((char *)&fourcc) + 1), \
35+
*(((char *)&fourcc) + 2), *(((char *)&fourcc) + 3), 0}
3836
#else
3937
#define FourCC2Str(fourcc) \
40-
(const char[]) { \
41-
*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
42-
*(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0 \
43-
}
38+
(const char[]){*(((char *)&fourcc) + 3), *(((char *)&fourcc) + 2), \
39+
*(((char *)&fourcc) + 1), *(((char *)&fourcc) + 0), 0}
4440
#endif
4541

4642
// pragma MARK: TextureHolder
@@ -270,19 +266,20 @@
270266

271267
// pragma MARK: YUV getColorspace()
272268

273-
SkYUVColorSpace SkiaCVPixelBufferUtils::YUV::getColorspace(
274-
CVPixelBufferRef pixelBuffer) {
269+
SkYUVColorSpace
270+
SkiaCVPixelBufferUtils::YUV::getColorspace(CVPixelBufferRef pixelBuffer) {
275271
const OSType pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);
276272

277-
CFTypeRef matrixAttachment =
278-
CVBufferCopyAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey, nullptr);
273+
CFTypeRef matrixAttachment = CVBufferCopyAttachment(
274+
pixelBuffer, kCVImageBufferYCbCrMatrixKey, nullptr);
279275
CFStringRef matrix = nullptr;
280276
if (matrixAttachment != nullptr &&
281277
CFGetTypeID(matrixAttachment) == CFStringGetTypeID()) {
282278
matrix = reinterpret_cast<CFStringRef>(matrixAttachment);
283279
}
284280

285-
SkYUVColorSpace colorspace = getSkYUVColorSpaceFromMatrix(matrix, pixelFormat);
281+
SkYUVColorSpace colorspace =
282+
getSkYUVColorSpaceFromMatrix(matrix, pixelFormat);
286283
if (matrixAttachment != nullptr) {
287284
CFRelease(matrixAttachment);
288285
}
@@ -320,8 +317,9 @@
320317
}
321318
}
322319

323-
SkYUVColorSpace SkiaCVPixelBufferUtils::YUV::getSkYUVColorSpaceFromMatrix(
324-
CFStringRef matrix, OSType pixelFormat) {
320+
SkYUVColorSpace
321+
SkiaCVPixelBufferUtils::YUV::getSkYUVColorSpaceFromMatrix(CFStringRef matrix,
322+
OSType pixelFormat) {
325323
const bool isFullRange = isFullRangeYUVFormat(pixelFormat);
326324
const bool isTenBit = isTenBitYUVFormat(pixelFormat);
327325

@@ -381,10 +379,11 @@
381379
"Requested out-of-bounds plane index " + std::to_string(planeIndex) +
382380
" for pixel buffer with " + std::to_string(planesCount) + " planes.");
383381
}
384-
size_t width = planesCount > 0 ? CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex)
385-
: CVPixelBufferGetWidth(pixelBuffer);
386-
size_t height =
387-
planesCount > 0 ? CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex)
382+
size_t width = planesCount > 0
383+
? CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex)
384+
: CVPixelBufferGetWidth(pixelBuffer);
385+
size_t height = planesCount > 0
386+
? CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex)
388387
: CVPixelBufferGetHeight(pixelBuffer);
389388
MTLPixelFormat pixelFormat =
390389
getMTLPixelFormatForCVPixelBufferPlane(pixelBuffer, planeIndex);
@@ -424,12 +423,12 @@
424423
MTLPixelFormat SkiaCVPixelBufferUtils::getMTLPixelFormatForCVPixelBufferPlane(
425424
CVPixelBufferRef pixelBuffer, size_t planeIndex) {
426425
const OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);
427-
auto throwInvalidPlaneIndexForFormat = [&](size_t expectedPlanes)
428-
-> MTLPixelFormat {
426+
auto throwInvalidPlaneIndexForFormat =
427+
[&](size_t expectedPlanes) -> MTLPixelFormat {
429428
throw std::runtime_error(
430429
"Invalid plane index " + std::to_string(planeIndex) +
431-
" for pixel format " + std::string(FourCC2Str(format)) + " (expected 0.." +
432-
std::to_string(expectedPlanes - 1) + ").");
430+
" for pixel format " + std::string(FourCC2Str(format)) +
431+
" (expected 0.." + std::to_string(expectedPlanes - 1) + ").");
433432
};
434433

435434
switch (format) {
@@ -495,17 +494,18 @@
495494
planesCount > 0 ? CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex)
496495
: CVPixelBufferGetWidth(pixelBuffer);
497496
const size_t bytesPerRow =
498-
planesCount > 0 ? CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, planeIndex)
499-
: CVPixelBufferGetBytesPerRow(pixelBuffer);
497+
planesCount > 0
498+
? CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, planeIndex)
499+
: CVPixelBufferGetBytesPerRow(pixelBuffer);
500500
if (width == 0) [[unlikely]] {
501501
throw std::runtime_error("Invalid plane width for pixel format " +
502502
std::string(FourCC2Str(format)) + "!");
503503
}
504504
if (bytesPerRow % width != 0) [[unlikely]] {
505-
throw std::runtime_error(
506-
"Invalid bytes per row! Bytes per row must be evenly divisible by width "
507-
"for pixel format " +
508-
std::string(FourCC2Str(format)) + "!");
505+
throw std::runtime_error("Invalid bytes per row! Bytes per row must be "
506+
"evenly divisible by width "
507+
"for pixel format " +
508+
std::string(FourCC2Str(format)) + "!");
509509
}
510510
const size_t bytesPerPixel = bytesPerRow / width;
511511
if (bytesPerPixel == 1) {
@@ -519,6 +519,7 @@
519519
}
520520

521521
[[unlikely]] throw std::runtime_error(
522-
"Invalid bytes per row! Expected 1 (R), 2 (RG) or 4 (RGBA), but received " +
522+
"Invalid bytes per row! Expected 1 (R), 2 (RG) or 4 (RGBA), but "
523+
"received " +
523524
std::to_string(bytesPerPixel));
524525
}

‎packages/skia/apple/SkiaPlatformContext.mm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
namespace rnwgpu {
88

99
wgpu::Surface SkiaPlatformContext::makeSurface(wgpu::Instance instance,
10-
void *surface, int width,
11-
int height) {
10+
void *surface, int width,
11+
int height) {
1212
wgpu::SurfaceSourceMetalLayer metalSurfaceDesc;
1313
metalSurfaceDesc.layer = surface;
1414
wgpu::SurfaceDescriptor surfaceDescriptor;

0 commit comments

Comments
 (0)