Skip to content

Commit 51c7a8f

Browse files
authored
fix(🐛): Fix opacity context bug where color opacity would mutate the context opacity (#3171)
1 parent 711459d commit 51c7a8f

File tree

10 files changed

+153
-84
lines changed

10 files changed

+153
-84
lines changed
14.8 KB
Loading
14 KB
Loading

apps/example/ios/Podfile.lock

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,77 +2162,77 @@ SPEC CHECKSUMS:
21622162
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
21632163
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
21642164
hermes-engine: b417d2b2aee3b89b58e63e23a51e02be91dc876d
2165-
RCT-Folly: e78785aa9ba2ed998ea4151e314036f6c49e6d82
2165+
RCT-Folly: 36fe2295e44b10d831836cc0d1daec5f8abcf809
21662166
RCTDeprecation: b2eecf2d60216df56bc5e6be5f063826d3c1ee35
21672167
RCTRequired: 78522de7dc73b81f3ed7890d145fa341f5bb32ea
21682168
RCTTypeSafety: c135dd2bf50402d87fd12884cbad5d5e64850edd
21692169
React: b229c49ed5898dab46d60f61ed5a0bfa2ee2fadb
21702170
React-callinvoker: 2ac508e92c8bd9cf834cc7d7787d94352e4af58f
2171-
React-Core: 325b4f6d9162ae8b9a6ff42fe78e260eb124180d
2172-
React-CoreModules: 558041e5258f70cd1092f82778d07b8b2ff01897
2173-
React-cxxreact: 8fff17cbe76e6a8f9991b59552e1235429f9c74b
2171+
React-Core: 13cdd1558d0b3f6d9d5a22e14d89150280e79f02
2172+
React-CoreModules: b07a6744f48305405e67c845ebf481b6551b712a
2173+
React-cxxreact: 1055a86c66ac35b4e80bd5fb766aed5f494dfff4
21742174
React-debug: 0a5fcdbacc6becba0521e910c1bcfdb20f32a3f6
2175-
React-defaultsnativemodule: 618dc50a0fad41b489997c3eb7aba3a74479fd14
2176-
React-domnativemodule: 7ba599afb6c2a7ec3eb6450153e2efe0b8747e9a
2177-
React-Fabric: 252112089d2c63308f4cbfade4010b6606db67d1
2178-
React-FabricComponents: 3c0f75321680d14d124438ab279c64ec2a3d13c4
2179-
React-FabricImage: 728b8061cdec2857ca885fd605ee03ad43ffca98
2175+
React-defaultsnativemodule: 4bb28fc97fee5be63a9ebf8f7a435cfe8ba69459
2176+
React-domnativemodule: b36a11c2597243d7563985028c51ece988d8ae33
2177+
React-Fabric: afc561718f25b2cd800b709d934101afe376a12c
2178+
React-FabricComponents: f4e0a4e18a27bf6d39cbf2a0b42f37a92fa4e37f
2179+
React-FabricImage: 37d8e8b672eda68a19d71143eb65148084efb325
21802180
React-featureflags: 19682e02ef5861d96b992af16a19109c3dfc1200
2181-
React-featureflagsnativemodule: 23528c7e7d50782b7ef0804168ba40bbaf1e86ab
2182-
React-graphics: fefe48f71bfe6f48fd037f59e8277b12e91b6be1
2183-
React-hermes: a9a0c8377627b5506ef9a7b6f60a805c306e3f51
2184-
React-idlecallbacksnativemodule: 7e2b6a3b70e042f89cd91dbd73c479bb39a72a7e
2185-
React-ImageManager: e3300996ac2e2914bf821f71e2f2c92ae6e62ae2
2186-
React-jserrorhandler: fa75876c662e5d7e79d6efc763fc9f4c88e26986
2187-
React-jsi: f3f51595cc4c089037b536368f016d4742bf9cf7
2188-
React-jsiexecutor: cca6c232db461e2fd213a11e9364cfa6fdaa20eb
2189-
React-jsinspector: 2bd4c9fddf189d6ec2abf4948461060502582bef
2190-
React-jsinspectortracing: a417d8a0ad481edaa415734b4dac81e3e5ee7dc6
2191-
React-jsitracing: 1ff7172c5b0522cbf6c98d82bdbb160e49b5804e
2192-
React-logger: 018826bfd51b9f18e87f67db1590bc510ad20664
2193-
React-Mapbuffer: 3c11cee7737609275c7b66bd0b1de475f094cedf
2194-
React-microtasksnativemodule: 843f352b32aacbe13a9c750190d34df44c3e6c2c
2195-
react-native-safe-area-context: 0f14bce545abcdfbff79ce2e3c78c109f0be283e
2196-
react-native-skia: 87d730aeeb02c54280ad5e42b443cc110a2e43ce
2197-
react-native-slider: bb7eb4732940fab78217e1c096bb647d8b0d1cf3
2198-
React-NativeModulesApple: 88433b6946778bea9c153e27b671de15411bf225
2199-
React-perflogger: 9e8d3c0dc0194eb932162812a168aa5dc662f418
2200-
React-performancetimeline: 5a2d6efef52bdcefac079c7baa30934978acd023
2181+
React-featureflagsnativemodule: d7cddf6d907b4e5ab84f9e744b7e88461656e48c
2182+
React-graphics: b0f78580cdaf5800d25437e3d41cc6c3d83b7aea
2183+
React-hermes: 71186f872c932e4574d5feb3ed754dda63a0b3bd
2184+
React-idlecallbacksnativemodule: dd2af19cdd3bc55149d17a2409ed72b694dfbe9c
2185+
React-ImageManager: a77dde8d5aa6a2b6962c702bf3a47695ef0aa32b
2186+
React-jserrorhandler: 9c14e89f12d5904257a79aaf84a70cd2e5ac07ba
2187+
React-jsi: 0775a66820496769ad83e629f0f5cce621a57fc7
2188+
React-jsiexecutor: 2cf5ba481386803f3c88b85c63fa102cba5d769e
2189+
React-jsinspector: 8052d532bb7a98b6e021755674659802fb140cc5
2190+
React-jsinspectortracing: bdd8fd0adcb4813663562e7874c5842449df6d8a
2191+
React-jsitracing: 2bab3bf55de3d04baf205def375fa6643c47c794
2192+
React-logger: 795cd5055782db394f187f9db0477d4b25b44291
2193+
React-Mapbuffer: 0502faf46cab8fb89cfc7bf3e6c6109b6ef9b5de
2194+
React-microtasksnativemodule: 663bc64e3a96c5fc91081923ae7481adc1359a78
2195+
react-native-safe-area-context: 286b3e7b5589795bb85ffc38faf4c0706c48a092
2196+
react-native-skia: 86f943730f6a64eea42fcebc02a9d9040370ce57
2197+
react-native-slider: e7f302c8d3296ddb49c642473f77f8f98809d53b
2198+
React-NativeModulesApple: 16fbd5b040ff6c492dacc361d49e63cba7a6a7a1
2199+
React-perflogger: ab51b7592532a0ea45bf6eed7e6cae14a368b678
2200+
React-performancetimeline: bc2e48198ec814d578ac8401f65d78a574358203
22012201
React-RCTActionSheet: 592674cf61142497e0e820688f5a696e41bf16dd
2202-
React-RCTAnimation: e6d669872f9b3b4ab9527aab283b7c49283236b7
2203-
React-RCTAppDelegate: de2343fe08be4c945d57e0ecce44afcc7dd8fc03
2204-
React-RCTBlob: 3e2dce94c56218becc4b32b627fc2293149f798d
2205-
React-RCTFabric: cac2c033381d79a5956e08550b0220cb2d78ea93
2206-
React-RCTFBReactNativeSpec: d10ca5e0ccbfeac8c047361fedf8e4ac653887b6
2207-
React-RCTImage: dc04b176c022d12a8f55ae7a7279b1e091066ae0
2208-
React-RCTLinking: 88f5e37fe4f26fbc80791aa2a5f01baf9b9a3fd5
2209-
React-RCTNetwork: f213693565efbd698b8e9c18d700a514b49c0c8e
2210-
React-RCTSettings: a2d32a90c45a3575568cad850abc45924999b8a5
2211-
React-RCTText: 54cdcd1cbf6f6a91dc6317f5d2c2b7fc3f6bf7a0
2212-
React-RCTVibration: 11dae0e7f577b5807bb7d31e2e881eb46f854fd4
2202+
React-RCTAnimation: 8fbb8dba757b49c78f4db403133ab6399a4ce952
2203+
React-RCTAppDelegate: 7f88baa8cb4e5d6c38bb4d84339925c70c9ac864
2204+
React-RCTBlob: f89b162d0fe6b570a18e755eb16cbe356d3c6d17
2205+
React-RCTFabric: 8ad6d875abe6e87312cef90e4b15ef7f6bed72e6
2206+
React-RCTFBReactNativeSpec: 8c29630c2f379c729300e4c1e540f3d1b78d1936
2207+
React-RCTImage: ccac9969940f170503857733f9a5f63578e106e1
2208+
React-RCTLinking: d82427bbf18415a3732105383dff119131cadd90
2209+
React-RCTNetwork: 12ad4d0fbde939e00251ca5ca890da2e6825cc3c
2210+
React-RCTSettings: e7865bf9f455abf427da349c855f8644b5c39afa
2211+
React-RCTText: 2cdfd88745059ec3202a0842ea75a956c7d6f27d
2212+
React-RCTVibration: a3a1458e6230dfd64b3768ebc0a4aac430d9d508
22132213
React-rendererconsistency: 64e897e00d2568fd8dfe31e2496f80e85c0aaad1
2214-
React-rendererdebug: 41ce452460c44bba715d9e41d5493a96de277764
2214+
React-rendererdebug: a3f6d3ae7d2fa0035885026756281c07ee32479e
22152215
React-rncore: 58748c2aa445f56b99e5118dad0aedb51c40ce9f
2216-
React-RuntimeApple: 7785ed0d8ae54da65a88736bb63ca97608a6d933
2217-
React-RuntimeCore: 6029ea70bc77f98cfd43ebe69217f14e93ba1f12
2216+
React-RuntimeApple: f0fda7bacabd32daa099cfda8f07466c30acd149
2217+
React-RuntimeCore: 683ee0b6a76d4b4bf6fbf83a541895b4887cc636
22182218
React-runtimeexecutor: a188df372373baf5066e6e229177836488799f80
2219-
React-RuntimeHermes: a264609c28b796edfffc8ae4cb8fad1773ab948b
2220-
React-runtimescheduler: 23ec3a1e0fb1ec752d1a9c1fb15258c30bfc7222
2219+
React-RuntimeHermes: 907c8e9bec13ea6466b94828c088c24590d4d0b6
2220+
React-runtimescheduler: a2e2a39125dd6426b5d8b773f689d660cd7c5f60
22212221
React-timing: bb220a53a795ed57976a4855c521f3de2f298fe5
2222-
React-utils: 3b054aaebe658fc710a8d239d0e4b9fd3e0b78f9
2223-
ReactAppDependencyProvider: a1fb08dfdc7ebc387b2e54cfc9decd283ed821d8
2224-
ReactCodegen: e232f8db3a40721044ec81b9388f95a7afaad36a
2225-
ReactCommon: 0c097b53f03d6bf166edbcd0915da32f3015dd90
2226-
ReactNativeHost: f9584a700dc379cfa223203d0d51e492df84a7a8
2227-
ReactTestApp-DevSupport: 16672810b0675a3bab6be3b3e85f1ce4b93144da
2222+
React-utils: 300d8bbb6555dcffaca71e7a0663201b5c7edbbc
2223+
ReactAppDependencyProvider: f2e81d80afd71a8058589e19d8a134243fa53f17
2224+
ReactCodegen: 50b6e45bbbef9b39d9798820cdbe87bfc7922e22
2225+
ReactCommon: 3d39389f8e2a2157d5c999f8fba57bd1c8f226f0
2226+
ReactNativeHost: f2ecc49200441384efb6c6e8bffe62ba29ee16ae
2227+
ReactTestApp-DevSupport: 15d2ef4884e8f5fd30ded3dec59b010f76384f37
22282228
ReactTestApp-Resources: 1bd9ff10e4c24f2ad87101a32023721ae923bccf
2229-
RNGestureHandler: dcb1b1db024f3744b03af56d132f4f72c4c27195
2230-
RNReanimated: 3b2312c84f8c747ab1e9d9c3ce879e93a5ba96f3
2231-
RNScreens: 790123c4a28783d80a342ce42e8c7381bed62db1
2232-
RNSVG: 8126581b369adf6a0004b6a6cab1a55e3002d5b0
2229+
RNGestureHandler: 66e593addd8952725107cfaa4f5e3378e946b541
2230+
RNReanimated: b292a2aee945230a9c5e01889043ba088b5fb9b8
2231+
RNScreens: 0f01bbed9bd8045a8d58e4b46993c28c7f498f3c
2232+
RNSVG: 8588ee1ca9b2e6fd2c99466e35b3db0e9f81bb40
22332233
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
2234-
Yoga: afd04ff05ebe0121a00c468a8a3c8080221cb14c
2234+
Yoga: 9b7fb56e7b08cde60e2153344fa6afbd88e5d99f
22352235

22362236
PODFILE CHECKSUM: 87506345285a0371afb28b9c3e6daaa999c214f3
22372237

2238-
COCOAPODS: 1.16.2
2238+
COCOAPODS: 1.15.2

packages/skia/cpp/api/recorder/DrawingCtx.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,24 @@ class DrawingCtx {
5656
SkPaint paint;
5757
paint.setAntiAlias(true);
5858
paints.push_back(paint);
59+
opacities.push_back(1.0f);
5960
}
6061

61-
void pushPaint(SkPaint &paint) { paints.push_back(paint); }
62+
float getOpacity() const { return opacities.back(); }
63+
64+
void setOpacity(float newOpacity) {
65+
opacities.back() = std::clamp(newOpacity, 0.0f, 1.0f);
66+
}
67+
68+
void pushPaint(SkPaint &paint) {
69+
paints.push_back(paint);
70+
opacities.push_back(opacities.back());
71+
}
6272

63-
void savePaint() { paints.push_back(SkPaint(getPaint())); }
73+
void savePaint() {
74+
paints.push_back(SkPaint(getPaint()));
75+
opacities.push_back(opacities.back());
76+
}
6477

6578
void saveBackdropFilter() {
6679
// Initialize image filter as nullptr
@@ -96,6 +109,7 @@ class DrawingCtx {
96109
}
97110
auto paint = paints.back();
98111
paints.pop_back();
112+
opacities.pop_back();
99113
return paint;
100114
}
101115

@@ -182,6 +196,9 @@ class DrawingCtx {
182196
std::vector<sk_sp<SkImageFilter>> imageFilters;
183197
std::vector<sk_sp<SkPathEffect>> pathEffects;
184198
std::vector<SkPaint> paintDeclarations;
199+
200+
private:
201+
std::vector<float> opacities;
185202
};
186203

187204
} // namespace RNSkia

packages/skia/cpp/api/recorder/Paint.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,11 @@ class SavePaintCmd : public Command {
153153
ctx->savePaint();
154154
auto &paint = ctx->getPaint();
155155
if (props.opacity.has_value()) {
156-
paint.setAlphaf(paint.getAlphaf() * props.opacity.value());
156+
ctx->setOpacity(ctx->getOpacity() * props.opacity.value());
157157
}
158158
if (props.color.has_value()) {
159-
auto currentOpacity = paint.getAlphaf();
160159
paint.setShader(nullptr);
161160
paint.setColor(props.color.value());
162-
paint.setAlphaf(currentOpacity * paint.getAlphaf());
163161
}
164162
if (props.blendMode.has_value()) {
165163
paint.setBlendMode(props.blendMode.value());

packages/skia/cpp/api/recorder/RNRecorder.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class Recorder {
294294
void play(DrawingCtx *ctx) {
295295
for (const auto &cmd : commands) {
296296
switch (cmd->type) {
297-
297+
298298
case Group: {
299299
// Do nothing here for now
300300
break;
@@ -483,17 +483,6 @@ class Recorder {
483483
break;
484484
}
485485

486-
case CommandType::DrawPaint: {
487-
ctx->canvas->drawPaint(ctx->getPaint());
488-
break;
489-
}
490-
491-
case CommandType::DrawText: {
492-
auto *textCmd = static_cast<TextCmd *>(cmd.get());
493-
textCmd->draw(ctx);
494-
break;
495-
}
496-
497486
case CommandType::RestorePaint: {
498487
ctx->restorePaint();
499488
break;
@@ -512,7 +501,10 @@ class Recorder {
512501
default: {
513502
// Handle all drawing commands
514503
auto currentPaints = ctx->paintDeclarations;
515-
currentPaints.push_back(ctx->getPaint()); // Add current paint
504+
// apply alpha to the current paint.
505+
SkPaint paint(ctx->getPaint());
506+
paint.setAlphaf(paint.getAlphaf() * ctx->getOpacity());
507+
currentPaints.push_back(paint);
516508
ctx->paintDeclarations.clear();
517509

518510
for (auto &paint : currentPaints) {

packages/skia/src/renderer/__tests__/e2e/Paint.spec.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Paint,
1111
Path,
1212
} from "../../components";
13-
import { checkImage } from "../../../__tests__/setup";
13+
import { checkImage, docPath } from "../../../__tests__/setup";
1414
import { fitbox } from "../../components/shapes/FitBox";
1515

1616
const blendModes = [
@@ -167,4 +167,47 @@ describe("Paint", () => {
167167
threshold: 0,
168168
});
169169
});
170+
it("should override colors", async () => {
171+
const { vec } = importSkia();
172+
const strokeWidth = 10;
173+
const { width, height } = surface;
174+
const c = vec(width / 2, height / 2);
175+
const r = (width - strokeWidth) / 2;
176+
const result = await surface.draw(
177+
<>
178+
<Circle c={c} r={r} color="transparent">
179+
<Paint color="lightblue" />
180+
<Paint color="#adbce6" style="stroke" strokeWidth={strokeWidth} />
181+
<Paint color="#ade6d8" style="stroke" strokeWidth={strokeWidth / 2} />
182+
</Circle>
183+
</>
184+
);
185+
checkImage(result, docPath("paint/stroke.png"));
186+
});
187+
it("colors don't influence opacity (1)", async () => {
188+
const { vec } = importSkia();
189+
const strokeWidth = 10;
190+
const { width, height } = surface;
191+
const c = vec(width / 2, height / 2);
192+
const r = (width - strokeWidth) / 2;
193+
const result = await surface.draw(
194+
<Group color="rgba(0,0,0,0.5)">
195+
<Circle c={c} r={r} color="lightblue" />
196+
</Group>
197+
);
198+
checkImage(result, docPath("paint/opaque-circle.png"));
199+
});
200+
it("colors don't influence opacity (2)", async () => {
201+
const { vec } = importSkia();
202+
const strokeWidth = 10;
203+
const { width, height } = surface;
204+
const c = vec(width / 2, height / 2);
205+
const r = (width - strokeWidth) / 2;
206+
const result = await surface.draw(
207+
<Group opacity={0.5}>
208+
<Circle c={c} r={r} color="lightblue" />
209+
</Group>
210+
);
211+
checkImage(result, docPath("paint/semi-transparent-circle.png"));
212+
});
170213
});

packages/skia/src/sksg/Recorder/DrawingContext.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ export const createDrawingContext = (
2222
const imageFilters: SkImageFilter[] = [];
2323
const pathEffects: SkPathEffect[] = [];
2424
const paintDeclarations: SkPaint[] = [];
25+
const opacities: number[] = [];
2526

2627
let nextPaintIndex = 1;
2728

28-
// Initialize first paint
29+
// Initialize first paint and opacity
2930
paintPool[0] = Skia.Paint();
3031
paints.push(paintPool[0]);
32+
opacities.push(1);
3133

3234
// Methods (formerly class methods)
3335
const savePaint = () => {
@@ -39,9 +41,18 @@ export const createDrawingContext = (
3941
const nextPaint = paintPool[nextPaintIndex];
4042
nextPaint.assign(getCurrentPaint()); // Reuse allocation by copying properties
4143
paints.push(nextPaint);
44+
opacities.push(opacities[opacities.length - 1]);
4245
nextPaintIndex++;
4346
};
4447

48+
const getOpacity = () => {
49+
return opacities[opacities.length - 1];
50+
};
51+
52+
const setOpacity = (newOpacity: number) => {
53+
opacities[opacities.length - 1] = Math.max(0, Math.min(1, newOpacity));
54+
};
55+
4556
const saveBackdropFilter = () => {
4657
let imageFilter: SkImageFilter | null = null;
4758
const imgf = imageFilters.pop();
@@ -63,6 +74,7 @@ export const createDrawingContext = (
6374
};
6475

6576
const restorePaint = () => {
77+
opacities.pop();
6678
return paints.pop();
6779
};
6880

@@ -125,6 +137,8 @@ export const createDrawingContext = (
125137
}, // the "getter" for the current paint
126138
restorePaint,
127139
materializePaint,
140+
getOpacity,
141+
setOpacity,
128142
};
129143
};
130144

packages/skia/src/sksg/Recorder/Player.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function play(ctx: DrawingContext, _command: Command) {
6767
ctx.paints.push(command.props.paint);
6868
} else {
6969
ctx.savePaint();
70-
setPaintProperties(ctx.Skia, ctx.paint, command.props);
70+
setPaintProperties(ctx.Skia, ctx, command.props);
7171
}
7272
} else if (isCommand(command, CommandType.RestorePaint)) {
7373
ctx.restorePaint();
@@ -101,7 +101,11 @@ function play(ctx: DrawingContext, _command: Command) {
101101
} else if (isCommand(command, CommandType.RestoreCTM)) {
102102
ctx.canvas.restore();
103103
} else {
104-
const paints = [ctx.paint, ...ctx.paintDeclarations];
104+
// TODO: is a copy needed here?
105+
// apply opacity to the current paint.
106+
const paint = ctx.paint.copy();
107+
paint.setAlphaf(paint.getAlphaf() * ctx.getOpacity());
108+
const paints = [paint, ...ctx.paintDeclarations];
105109
ctx.paintDeclarations = [];
106110
paints.forEach((p) => {
107111
ctx.paints.push(p);

packages/skia/src/sksg/Recorder/commands/Paint.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
StrokeCap,
77
StrokeJoin,
88
} from "../../../skia/types";
9-
import type { SkPaint, Skia } from "../../../skia/types";
9+
import type { Skia } from "../../../skia/types";
10+
import type { DrawingContext } from "../DrawingContext";
1011

1112
export const setPaintProperties = (
1213
Skia: Skia,
13-
paint: SkPaint,
14+
ctx: DrawingContext,
1415
{
1516
opacity,
1617
color,
@@ -25,14 +26,14 @@ export const setPaintProperties = (
2526
}: PaintProps
2627
) => {
2728
"worklet";
29+
const { paint } = ctx;
30+
2831
if (opacity !== undefined) {
29-
paint.setAlphaf(paint.getAlphaf() * opacity);
32+
ctx.setOpacity(ctx.getOpacity() * opacity);
3033
}
3134
if (color !== undefined) {
32-
const currentOpacity = paint.getAlphaf();
3335
paint.setShader(null);
3436
paint.setColor(processColor(Skia, color));
35-
paint.setAlphaf(currentOpacity * paint.getAlphaf());
3637
}
3738
if (blendMode !== undefined) {
3839
paint.setBlendMode(BlendMode[enumKey(blendMode)]);

0 commit comments

Comments
 (0)