Skip to content

Commit 1ff4de3

Browse files
authored
fix(🗑️): fix Hermes memory pressure tracking for recorder pictures (#3495)
1 parent 4dcea9e commit 1ff4de3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+337
-34
lines changed

apps/example/src/Examples/API/StressTest4.tsx

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import {
33
Alert,
44
Button,
@@ -18,13 +18,35 @@ import {
1818
withTiming,
1919
} from "react-native-reanimated";
2020

21+
const logMemoryStats = () => {
22+
"worklet";
23+
if (
24+
typeof HermesInternal !== "undefined" &&
25+
// @ts-expect-error - HermesInternal is available in Hermes runtime
26+
HermesInternal?.getInstrumentedStats
27+
) {
28+
// // @ts-expect-error - HermesInternal is available in Hermes runtime
29+
// const stats = HermesInternal.getInstrumentedStats();
30+
// const externalMB = (stats.js_externalBytes / (1024 * 1024)).toFixed(2);
31+
// const totalAllocatedMB = (
32+
// stats.js_totalAllocatedBytes /
33+
// (1024 * 1024)
34+
// ).toFixed(2);
35+
// const heapSizeMB = (stats.js_heapSize / (1024 * 1024)).toFixed(2);
36+
// console.log(
37+
// `External: ${externalMB} MB, Total Allocated: ${totalAllocatedMB} MB, Heap Size: ${heapSizeMB} MB`
38+
// );
39+
}
40+
};
41+
2142
const drawFrame = (
2243
surface: SharedValue<SkSurface | null>,
2344
image: SharedValue<SkImage | null>,
2445
x: SharedValue<number>,
2546
pixelRatio: number
2647
) => {
2748
"worklet";
49+
logMemoryStats();
2850
if (!surface.value) {
2951
Alert.alert("surface is null");
3052
return false;
@@ -56,11 +78,16 @@ function continueBlink(
5678
blinkCnt: SharedValue<number>,
5779
isLeft: SharedValue<boolean>,
5880
pixelRatio: number,
59-
revert: (d: string) => void
81+
revert: (d: string) => void,
82+
shouldContinue: SharedValue<boolean>
6083
) {
6184
"worklet";
85+
if (!shouldContinue.value) {
86+
return;
87+
}
6288
blinkCnt.value++;
6389
looper.value = withTiming(1 - looper.value, { duration: 10 }, () => {
90+
if (!shouldContinue.value) return;
6491
if (!drawFrame(surface, image, x, pixelRatio)) return;
6592
if (blinkCnt.value < 20) {
6693
continueBlink(
@@ -71,13 +98,16 @@ function continueBlink(
7198
blinkCnt,
7299
isLeft,
73100
pixelRatio,
74-
revert
101+
revert,
102+
shouldContinue
75103
);
76104
} else {
105+
if (!shouldContinue.value) return;
77106
runOnJS(revert)(isLeft.value ? "right" : "left");
78107
isLeft.value = !isLeft.value;
79108
blinkCnt.value = 0;
80109
x.value = withTiming(400 - x.value, { duration: 200 }, () => {
110+
if (!shouldContinue.value) return;
81111
continueBlink(
82112
surface,
83113
image,
@@ -86,7 +116,8 @@ function continueBlink(
86116
blinkCnt,
87117
isLeft,
88118
pixelRatio,
89-
revert
119+
revert,
120+
shouldContinue
90121
);
91122
});
92123
}
@@ -101,9 +132,11 @@ const startBlink = (
101132
blinkCnt: SharedValue<number>,
102133
isLeft: SharedValue<boolean>,
103134
pixelRatio: number,
104-
revert: (d: string) => void
135+
revert: (d: string) => void,
136+
shouldContinue: SharedValue<boolean>
105137
) => {
106138
"worklet";
139+
if (!shouldContinue.value) return;
107140
blinkCnt.value = 0;
108141
if (drawFrame(surface, image, x, pixelRatio)) {
109142
continueBlink(
@@ -114,7 +147,8 @@ const startBlink = (
114147
blinkCnt,
115148
isLeft,
116149
pixelRatio,
117-
revert
150+
revert,
151+
shouldContinue
118152
);
119153
}
120154
};
@@ -127,7 +161,8 @@ const startAnimation = (
127161
blinkCnt: SharedValue<number>,
128162
isLeft: SharedValue<boolean>,
129163
pixelRatio: number,
130-
revert: (d: string) => void
164+
revert: (d: string) => void,
165+
shouldContinue: SharedValue<boolean>
131166
) => {
132167
"worklet";
133168
if (!surface.value) {
@@ -136,7 +171,17 @@ const startAnimation = (
136171
pixelRatio * 400
137172
);
138173
}
139-
startBlink(surface, image, x, looper, blinkCnt, isLeft, pixelRatio, revert);
174+
startBlink(
175+
surface,
176+
image,
177+
x,
178+
looper,
179+
blinkCnt,
180+
isLeft,
181+
pixelRatio,
182+
revert,
183+
shouldContinue
184+
);
140185
};
141186

142187
export const StressTest4 = () => {
@@ -149,10 +194,12 @@ export const StressTest4 = () => {
149194
const [direction, setDirection] = useState("left");
150195
const isLeft = useSharedValue(true);
151196
const blinkCnt = useSharedValue(0);
197+
const shouldContinue = useSharedValue(true);
152198

153199
const imageX = useDerivedValue(() => -x.value);
154200

155201
const handleStart = () => {
202+
shouldContinue.value = true;
156203
runOnUI(startAnimation)(
157204
surface,
158205
image,
@@ -161,10 +208,18 @@ export const StressTest4 = () => {
161208
blinkCnt,
162209
isLeft,
163210
pixelRatio,
164-
setDirection
211+
setDirection,
212+
shouldContinue
165213
);
166214
};
167215

216+
useEffect(() => {
217+
// Stop animation on unmount
218+
return () => {
219+
shouldContinue.value = false;
220+
};
221+
}, [shouldContinue]);
222+
168223
return (
169224
<SafeAreaView style={styles.container}>
170225
<Text>Direction: {direction}</Text>

apps/example/src/Home/HomeScreen.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ export const HomeScreen = () => {
3939
description="Simple declarative example"
4040
route="Breathe"
4141
/>
42-
<HomeScreenButton
43-
title="🖼 Image Loading"
44-
description="Load and edit an image"
45-
route="ImageLoading"
46-
/>
4742
<HomeScreenButton
4843
title="🏞 Filters"
4944
description="Simple Image Filters"

externals/depot_tools

Submodule depot_tools updated from abc5109 to 8a1ec6a

packages/skia/cpp/api/JsiNativeBuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class JsiNativeBufferFactory : public JsiSkHostObject {
3838

3939
size_t getMemoryPressure() const override { return 1024; }
4040

41+
std::string getObjectType() const override {
42+
return "JsiNativeBufferFactory";
43+
}
44+
4145
explicit JsiNativeBufferFactory(std::shared_ptr<RNSkPlatformContext> context)
4246
: JsiSkHostObject(std::move(context)) {}
4347
};

packages/skia/cpp/api/JsiSkAnimatedImage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class JsiSkAnimatedImage
6464
std::move(image)) {}
6565

6666
size_t getMemoryPressure() const override { return 8192; }
67+
68+
std::string getObjectType() const override { return "JsiSkAnimatedImage"; }
6769
};
6870

6971
} // namespace RNSkia

packages/skia/cpp/api/JsiSkAnimatedImageFactory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class JsiSkAnimatedImageFactory : public JsiSkHostObject {
3131

3232
size_t getMemoryPressure() const override { return 1024; }
3333

34+
std::string getObjectType() const override {
35+
return "JsiSkAnimatedImageFactory";
36+
}
37+
3438
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImageFactory,
3539
MakeAnimatedImageFromEncoded))
3640

packages/skia/cpp/api/JsiSkApi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class JsiSkApi : public JsiSkHostObject {
6363
public:
6464
size_t getMemoryPressure() const override { return 8192; }
6565

66+
std::string getObjectType() const override { return "JsiSkApi"; }
67+
6668
/**
6769
* Constructs the Skia Api object that can be installed into a runtime
6870
* and provide functions for accessing and creating the Skia wrapper objects

packages/skia/cpp/api/JsiSkCanvas.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ class JsiSkCanvas : public JsiSkHostObject {
695695

696696
size_t getMemoryPressure() const override { return 1024; }
697697

698+
std::string getObjectType() const override { return "JsiSkCanvas"; }
699+
698700
explicit JsiSkCanvas(std::shared_ptr<RNSkPlatformContext> context)
699701
: JsiSkHostObject(std::move(context)) {}
700702

packages/skia/cpp/api/JsiSkColorFilter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class JsiSkColorFilter : public JsiSkWrappingSkPtrHostObject<SkColorFilter> {
2525

2626
size_t getMemoryPressure() const override { return 2048; }
2727

28+
std::string getObjectType() const override { return "JsiSkColorFilter"; }
29+
2830
EXPORT_JSI_API_TYPENAME(JsiSkColorFilter, ColorFilter)
2931
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkColorFilter, dispose))
3032
};

packages/skia/cpp/api/JsiSkColorFilterFactory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class JsiSkColorFilterFactory : public JsiSkHostObject {
9494

9595
size_t getMemoryPressure() const override { return 1024; }
9696

97+
std::string getObjectType() const override {
98+
return "JsiSkColorFilterFactory";
99+
}
100+
97101
JSI_EXPORT_FUNCTIONS(
98102
JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeMatrix),
99103
JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeBlend),

0 commit comments

Comments
 (0)