Skip to content

Commit db89ff4

Browse files
authored
cherrypick: Bring changes to improve our RCTUIGraphicsImageRenderer shim (#2219)
* Rename `RCTUIGraphicsImageRenderer` to `RCTMakeUIGraphicsImageRenderer` (facebook#46772) Summary: Because `UIGraphicsImageRenderer` doesn't exist on macOS, I need to shim it for React Native macOS (See #2209). I planned to use the name `RCTUIGraphicsImageRenderer`. However.. it seems that is used by a static helper function in `RCTBorderDrawing.m`. So.. let's rename it? The function is just a helper method to make an instance of the class, so I think the name `RCTMakeUIGraphicsImageRenderer` is slightly more idiomatic anyway. This method is not public, so it should not break the public API of React Native. [IOS] [CHANGED] - Rename `RCTUIGraphicsImageRenderer` to `RCTMakeUIGraphicsImageRenderer` Pull Request resolved: facebook#46772 Test Plan: CI should pass Reviewed By: joevilches Differential Revision: D63765490 Pulled By: cipolleschi fbshipit-source-id: de68dce0f92ec249ea8586dbf7b9ba34a8476074 * fix(iOS): Replace uses of `CGColorRef` with UIColor to avoid manual memory management (facebook#46847) Summary: Update React Native on iOS to do less manual memory management, by replacing uses of `CGColorRef` with `UIColor`, and only calling `UIColor.CGColor` when needed. This results in much less manual memory management, which is probably a good thing in 2024 :D. The downside is there is a breaking change: the signature of a method in `RCTBorderDrawing` changes. This is a followup to facebook#46797 . After that PR merged and I tested merging React Native macOS to the `0.76-stable` branch cut commit again, I saw even more places I needed to manually call `CGColorRetain` / `CGColorRelease`. The reason is due to React Native macOS specifics (explained below) and I could update React Native macOS to not need these changes, but I thought I would at least throw up a PR to propose the changes, as it may be good to move away from using Core Graphics' C base API as much as possible. With #2209 , I wrote a shim of [UIGraphicsImageRenderer](https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer) for macOS. The main difference is my shim calls the NSImage API [imageWithSize:flipped:drawingHandler:](https://developer.apple.com/documentation/appkit/nsimage/1519860-imagewithsize?language=objc) to shim the UIGraphicsImageRenderer API [imageWithData:](https://developer.apple.com/documentation/uikit/uiimage/1624137-imagewithdata). The difference between the two is that the macOS API copies the block, and executes it when Appkit is about to draw the image, while the iOS API executes the block right away. Because of this, I think I am hitting way more places where `CGColorRef` variables needed to be retained / released. Additionally, I hit more of them when I merge to the 0.76 branch cut commit (this is why I didn't catch it with facebook#46797). Given this constraint, I have a couple of options: 1. Refactor my macOS shim to use the deprecated API [[NSImage lockFocus]](https://developer.apple.com/documentation/appkit/nsimage/1519891-lockfocus) - I am not a fan of this because `lockFocus` was deprecated for `imageWithSize:flipped:drawingHandler:` 2. Refactor my macOS shim to do what we used to do: Create a CGContext manually and write it to an image - This is probably OK. Relies on less Appkit specifics, and potentially gives more control to RN on the rendering layer, which I recall lenaic told me is better for Fabric) 3. Refactor React Native to avoid CGColorRef altogether, and use `UIColor` (which ARC will memory manage for us) as much as possible. - This is the approach of this PR and my preferred approach. The downside is this changes the signature of some of the methods in `RCTBorderDrawing` which is a breaking change. I've seen other PRs do this, so maybe its OK? [IOS] [BREAKING] - Replace uses of `CGColorRef` with UIColor to avoid manual memory management Pull Request resolved: facebook#46847 Test Plan: Launching RNTester's View example (which tests a lot of the border / outline / shadow rendering) does not crash for me on both iOS and macOS. Reviewed By: NickGerleman Differential Revision: D63989547 Pulled By: joevilches fbshipit-source-id: 5e85e17567e3dd8b4b0388452398954ad2e02464 * Followup fixes * Remove extra scaleFactor argument that is now unused * Further reduce diffs related to scaleFactor * fix typos and dead code
1 parent 8cd5b67 commit db89ff4

File tree

5 files changed

+60
-103
lines changed

5 files changed

+60
-103
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,8 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
260260

261261
// `shadowColor`
262262
if (oldViewProps.shadowColor != newViewProps.shadowColor) {
263-
CGColorRef shadowColor = RCTCreateCGColorRefFromSharedColor(newViewProps.shadowColor);
264-
self.layer.shadowColor = shadowColor;
265-
CGColorRelease(shadowColor);
263+
RCTUIColor *shadowColor = RCTUIColorFromSharedColor(newViewProps.shadowColor); // [macOS]
264+
self.layer.shadowColor = shadowColor.CGColor;
266265
needsInvalidateLayer = YES;
267266
}
268267

@@ -584,18 +583,10 @@ static RCTCornerRadii RCTCornerRadiiFromBorderRadii(BorderRadii borderRadii)
584583
static RCTBorderColors RCTCreateRCTBorderColorsFromBorderColors(BorderColors borderColors)
585584
{
586585
return RCTBorderColors{
587-
.top = RCTCreateCGColorRefFromSharedColor(borderColors.top),
588-
.left = RCTCreateCGColorRefFromSharedColor(borderColors.left),
589-
.bottom = RCTCreateCGColorRefFromSharedColor(borderColors.bottom),
590-
.right = RCTCreateCGColorRefFromSharedColor(borderColors.right)};
591-
}
592-
593-
static void RCTReleaseRCTBorderColors(RCTBorderColors borderColors)
594-
{
595-
CGColorRelease(borderColors.top);
596-
CGColorRelease(borderColors.left);
597-
CGColorRelease(borderColors.bottom);
598-
CGColorRelease(borderColors.right);
586+
.top = RCTUIColorFromSharedColor(borderColors.top),
587+
.left = RCTUIColorFromSharedColor(borderColors.left),
588+
.bottom = RCTUIColorFromSharedColor(borderColors.bottom),
589+
.right = RCTUIColorFromSharedColor(borderColors.right)};
599590
}
600591

601592
static CALayerCornerCurve CornerCurveFromBorderCurve(BorderCurve borderCurve)
@@ -800,24 +791,22 @@ - (void)invalidateLayer
800791
(*borderMetrics.borderColors.left).getUIColor() != nullptr));
801792

802793
#if !TARGET_OS_OSX // [macOS]
803-
CGColorRef backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection].CGColor;
794+
RCTUIColor *backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection];
804795
#else // [macOS
805-
CGColorRef backgroundColor = _backgroundColor.CGColor;
796+
RCTUIColor *backgroundColor = _backgroundColor;
806797
#endif // macOS]
807798

808799
if (useCoreAnimationBorderRendering) {
809800
layer.mask = nil;
810801
[_borderLayer removeFromSuperlayer];
811802

812803
layer.borderWidth = (CGFloat)borderMetrics.borderWidths.left;
813-
CGColorRef borderColor = RCTCreateCGColorRefFromSharedColor(borderMetrics.borderColors.left);
814-
layer.borderColor = borderColor;
815-
CGColorRelease(borderColor);
804+
layer.borderColor = RCTUIColorFromSharedColor(borderMetrics.borderColors.left).CGColor;
816805
layer.cornerRadius = (CGFloat)borderMetrics.borderRadii.topLeft;
817806

818807
layer.cornerCurve = CornerCurveFromBorderCurve(borderMetrics.borderCurves.topLeft);
819808

820-
layer.backgroundColor = backgroundColor;
809+
layer.backgroundColor = backgroundColor.CGColor;
821810
} else {
822811
if (!_borderLayer) {
823812
CALayer *borderLayer = [CALayer new];
@@ -835,23 +824,15 @@ - (void)invalidateLayer
835824

836825
RCTBorderColors borderColors = RCTCreateRCTBorderColorsFromBorderColors(borderMetrics.borderColors);
837826

838-
#if TARGET_OS_OSX // [macOS
839-
CGFloat scaleFactor = _layoutMetrics.pointScaleFactor;
840-
#else
841-
// On iOS setting the scaleFactor to 0.0 will default to the device's native scale factor.
842-
CGFloat scaleFactor = 0.0;
843-
#endif // macOS]
844827
UIImage *image = RCTGetBorderImage(
845828
RCTBorderStyleFromBorderStyle(borderMetrics.borderStyles.left),
846829
layer.bounds.size,
847830
RCTCornerRadiiFromBorderRadii(borderMetrics.borderRadii),
848831
RCTUIEdgeInsetsFromEdgeInsets(borderMetrics.borderWidths),
849832
borderColors,
850833
backgroundColor,
851-
self.clipsToBounds,
852-
scaleFactor); // [macOS]
834+
self.clipsToBounds);
853835

854-
RCTReleaseRCTBorderColors(borderColors);
855836

856837
if (image == nil) {
857838
_borderLayer.contents = nil;
@@ -866,6 +847,7 @@ - (void)invalidateLayer
866847
_borderLayer.contents = (id)image.CGImage;
867848
_borderLayer.contentsScale = image.scale;
868849
#else // [macOS
850+
CGFloat scaleFactor = _layoutMetrics.pointScaleFactor;
869851
_borderLayer.contents = [image layerContentsForContentsScale:scaleFactor];
870852
_borderLayer.contentsScale = scaleFactor;
871853
#endif // macOS]

packages/react-native/React/Fabric/RCTConversions.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ inline RCTUIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::Sh
4040
return RCTPlatformColorFromColor(*sharedColor);
4141
}
4242

43-
inline CF_RETURNS_RETAINED CGColorRef _Nullable RCTCreateCGColorRefFromSharedColor(
44-
const facebook::react::SharedColor &sharedColor)
45-
{
46-
return CGColorRetain(RCTUIColorFromSharedColor(sharedColor).CGColor);
47-
}
48-
4943
inline CGPoint RCTCGPointFromPoint(const facebook::react::Point &point)
5044
{
5145
return {point.x, point.y};

packages/react-native/React/Views/RCTBorderDrawing.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ typedef struct {
2525
} RCTCornerInsets;
2626

2727
typedef struct {
28-
CGColorRef top;
29-
CGColorRef left;
30-
CGColorRef bottom;
31-
CGColorRef right;
28+
RCTUIColor *top;
29+
RCTUIColor *left;
30+
RCTUIColor *bottom;
31+
RCTUIColor *right;
3232
} RCTBorderColors;
3333

3434
/**
@@ -64,6 +64,5 @@ RCT_EXTERN UIImage *RCTGetBorderImage(
6464
RCTCornerRadii cornerRadii,
6565
UIEdgeInsets borderInsets,
6666
RCTBorderColors borderColors,
67-
CGColorRef backgroundColor,
68-
BOOL drawToEdge,
69-
CGFloat scaleFactor); // [macOS]
67+
RCTUIColor *backgroundColor, // [macOS]
68+
BOOL drawToEdge);

packages/react-native/React/Views/RCTBorderDrawing.m

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ BOOL RCTCornerRadiiAreEqual(RCTCornerRadii cornerRadii)
2626

2727
BOOL RCTBorderColorsAreEqual(RCTBorderColors borderColors)
2828
{
29-
return CGColorEqualToColor(borderColors.left, borderColors.right) &&
30-
CGColorEqualToColor(borderColors.left, borderColors.top) &&
31-
CGColorEqualToColor(borderColors.left, borderColors.bottom);
29+
return CGColorEqualToColor(borderColors.left.CGColor, borderColors.right.CGColor) &&
30+
CGColorEqualToColor(borderColors.left.CGColor, borderColors.top.CGColor) &&
31+
CGColorEqualToColor(borderColors.left.CGColor, borderColors.bottom.CGColor);
3232
}
3333

3434
RCTCornerInsets RCTGetCornerInsets(RCTCornerRadii cornerRadii, UIEdgeInsets edgeInsets)
@@ -172,9 +172,9 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
172172
}
173173

174174
static RCTUIGraphicsImageRenderer * // [macOS]
175-
RCTMakeUIGraphicsImageRenderer(CGSize size, CGColorRef backgroundColor, BOOL hasCornerRadii, BOOL drawToEdge) // [macOS]
175+
RCTMakeUIGraphicsImageRenderer(CGSize size, RCTUIColor *backgroundColor, BOOL hasCornerRadii, BOOL drawToEdge) // [macOS]
176176
{
177-
const CGFloat alpha = CGColorGetAlpha(backgroundColor);
177+
const CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
178178
const BOOL opaque = (drawToEdge || !hasCornerRadii) && alpha == 1.0;
179179
RCTUIGraphicsImageRendererFormat *const rendererFormat = [RCTUIGraphicsImageRendererFormat defaultFormat]; // [macOS]
180180
rendererFormat.opaque = opaque;
@@ -187,10 +187,9 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
187187
CGSize viewSize,
188188
UIEdgeInsets borderInsets,
189189
RCTBorderColors borderColors,
190-
CGColorRef backgroundColor,
191-
BOOL drawToEdge,
192-
CGFloat scaleFactor // [macOS]
193-
) {
190+
RCTUIColor *backgroundColor, // [macOS]
191+
BOOL drawToEdge)
192+
{
194193
const BOOL hasCornerRadii = RCTCornerRadiiAreAboveThreshold(cornerRadii);
195194
const RCTCornerInsets cornerInsets = RCTGetCornerInsets(cornerRadii, borderInsets);
196195

@@ -227,19 +226,17 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
227226
} // macOS]
228227

229228
RCTUIGraphicsImageRenderer *const imageRenderer =
230-
RCTMakeUIGraphicsImageRenderer(size, backgroundColor, hasCornerRadii, drawToEdge); // [macOS]
231-
CGColorRetain(backgroundColor); // [macOS] CGColorRefs are not atuomtically retained when passed into a block
229+
RCTMakeUIGraphicsImageRenderer(size, backgroundColor, hasCornerRadii, drawToEdge);
232230
UIImage *image = [imageRenderer imageWithActions:^(RCTUIGraphicsImageRendererContext *_Nonnull rendererContext) { // [macOS]
233231
const CGContextRef context = rendererContext.CGContext;
234232
const CGRect rect = {.size = size};
235233
CGPathRef path = RCTPathCreateOuterOutline(drawToEdge, rect, cornerRadii);
236234

237235
if (backgroundColor) {
238-
CGContextSetFillColorWithColor(context, backgroundColor);
236+
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
239237
CGContextAddPath(context, path);
240238
CGContextFillPath(context);
241239
}
242-
CGColorRelease(backgroundColor); // [macOS]
243240

244241
CGContextAddPath(context, path);
245242
CGPathRelease(path);
@@ -251,7 +248,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
251248

252249
BOOL hasEqualColors = RCTBorderColorsAreEqual(borderColors);
253250
if ((drawToEdge || !hasCornerRadii) && hasEqualColors) {
254-
CGContextSetFillColorWithColor(context, borderColors.left);
251+
CGContextSetFillColorWithColor(context, borderColors.left.CGColor);
255252
CGContextAddRect(context, rect);
256253
CGContextAddPath(context, insetPath);
257254
CGContextEOFillPath(context);
@@ -316,7 +313,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
316313
}
317314
}
318315

319-
CGColorRef currentColor = NULL;
316+
RCTUIColor *currentColor = nil;
320317

321318
// RIGHT
322319
if (borderInsets.right > 0) {
@@ -340,8 +337,8 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
340337
(CGPoint){size.width, size.height},
341338
};
342339

343-
if (!CGColorEqualToColor(currentColor, borderColors.bottom)) {
344-
CGContextSetFillColorWithColor(context, currentColor);
340+
if (!CGColorEqualToColor(currentColor.CGColor, borderColors.bottom.CGColor)) {
341+
CGContextSetFillColorWithColor(context, currentColor.CGColor);
345342
CGContextFillPath(context);
346343
currentColor = borderColors.bottom;
347344
}
@@ -357,8 +354,8 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
357354
(CGPoint){0, size.height},
358355
};
359356

360-
if (!CGColorEqualToColor(currentColor, borderColors.left)) {
361-
CGContextSetFillColorWithColor(context, currentColor);
357+
if (!CGColorEqualToColor(currentColor.CGColor, borderColors.left.CGColor)) {
358+
CGContextSetFillColorWithColor(context, currentColor.CGColor);
362359
CGContextFillPath(context);
363360
currentColor = borderColors.left;
364361
}
@@ -374,15 +371,15 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
374371
(CGPoint){size.width, 0},
375372
};
376373

377-
if (!CGColorEqualToColor(currentColor, borderColors.top)) {
378-
CGContextSetFillColorWithColor(context, currentColor);
374+
if (!CGColorEqualToColor(currentColor.CGColor, borderColors.top.CGColor)) {
375+
CGContextSetFillColorWithColor(context, currentColor.CGColor);
379376
CGContextFillPath(context);
380377
currentColor = borderColors.top;
381378
}
382379
CGContextAddLines(context, points, sizeof(points) / sizeof(*points));
383380
}
384381

385-
CGContextSetFillColorWithColor(context, currentColor);
382+
CGContextSetFillColorWithColor(context, currentColor.CGColor);
386383
CGContextFillPath(context);
387384
}
388385

@@ -466,10 +463,9 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
466463
CGSize viewSize,
467464
UIEdgeInsets borderInsets,
468465
RCTBorderColors borderColors,
469-
CGColorRef backgroundColor,
470-
BOOL drawToEdge,
471-
CGFloat scaleFactor // [macOS]
472-
) {
466+
RCTUIColor *backgroundColor, // [macOS]
467+
BOOL drawToEdge)
468+
{
473469
NSCParameterAssert(borderStyle == RCTBorderStyleDashed || borderStyle == RCTBorderStyleDotted);
474470

475471
if (!RCTBorderColorsAreEqual(borderColors) || !RCTBorderInsetsAreEqual(borderInsets)) {
@@ -489,7 +485,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
489485

490486
const BOOL hasCornerRadii = RCTCornerRadiiAreAboveThreshold(cornerRadii);
491487
RCTUIGraphicsImageRenderer *const imageRenderer = // [macOS]
492-
RCTMakeUIGraphicsImageRenderer(viewSize, backgroundColor, hasCornerRadii, drawToEdge); // [macOS]
488+
RCTMakeUIGraphicsImageRenderer(viewSize, backgroundColor, hasCornerRadii, drawToEdge);
493489
return [imageRenderer imageWithActions:^(RCTUIGraphicsImageRendererContext *_Nonnull rendererContext) { // [macOS]
494490
const CGContextRef context = rendererContext.CGContext;
495491
const CGRect rect = {.size = viewSize};
@@ -499,7 +495,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
499495
CGContextAddPath(context, outerPath);
500496
CGPathRelease(outerPath);
501497

502-
CGContextSetFillColorWithColor(context, backgroundColor);
498+
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
503499
CGContextFillPath(context);
504500
}
505501

@@ -518,7 +514,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
518514
CGContextSetStrokeColorWithColor(context, [RCTUIColor yellowColor].CGColor); // [macOS]
519515

520516
CGContextAddPath(context, path);
521-
CGContextSetStrokeColorWithColor(context, borderColors.top);
517+
CGContextSetStrokeColorWithColor(context, borderColors.top.CGColor);
522518
CGContextStrokePath(context);
523519

524520
CGPathRelease(path);
@@ -531,17 +527,16 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
531527
RCTCornerRadii cornerRadii,
532528
UIEdgeInsets borderInsets,
533529
RCTBorderColors borderColors,
534-
CGColorRef backgroundColor,
535-
BOOL drawToEdge,
536-
CGFloat scaleFactor // [macOS]
537-
) {
530+
RCTUIColor *backgroundColor, // [macOS]
531+
BOOL drawToEdge)
532+
{
538533
switch (borderStyle) {
539534
case RCTBorderStyleSolid:
540-
return RCTGetSolidBorderImage(cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge, scaleFactor); // [macOS]
535+
return RCTGetSolidBorderImage(cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge);
541536
case RCTBorderStyleDashed:
542537
case RCTBorderStyleDotted:
543538
return RCTGetDashedOrDottedBorderImage(
544-
borderStyle, cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge, scaleFactor); // [macOS]
539+
borderStyle, cornerRadii, viewSize, borderInsets, borderColors, backgroundColor, drawToEdge);
545540
case RCTBorderStyleUnset:
546541
break;
547542
}

packages/react-native/React/Views/RCTView.m

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,10 @@ - (RCTBorderColors)borderColors
11171117
#endif // [macOS]
11181118

11191119
return (RCTBorderColors){
1120-
(borderTopColor ?: borderColor).CGColor,
1121-
(directionAwareBorderLeftColor ?: borderColor).CGColor,
1122-
(borderBottomColor ?: borderColor).CGColor,
1123-
(directionAwareBorderRightColor ?: borderColor).CGColor,
1120+
(borderTopColor ?: borderColor),
1121+
(directionAwareBorderLeftColor ?: borderColor),
1122+
(borderBottomColor ?: borderColor),
1123+
(directionAwareBorderRightColor ?: borderColor),
11241124
};
11251125
}
11261126

@@ -1165,18 +1165,17 @@ - (void)displayLayer:(CALayer *)layer
11651165
// the content. For this reason, only use iOS border drawing when clipping
11661166
// or when the border is hidden.
11671167

1168-
(borderInsets.top == 0 || (borderColors.top && CGColorGetAlpha(borderColors.top) == 0) || self.clipsToBounds);
1168+
(borderInsets.top == 0 || (borderColors.top && CGColorGetAlpha(borderColors.top.CGColor) == 0) ||
1169+
self.clipsToBounds);
11691170

11701171
// iOS clips to the outside of the border, but CSS clips to the inside. To
11711172
// solve this, we'll need to add a container view inside the main view to
11721173
// correctly clip the subviews.
11731174

1174-
CGColorRef backgroundColor;
1175-
11761175
#if !TARGET_OS_OSX // [macOS]
1177-
backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection].CGColor;
1176+
RCTUIColor *backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection];
11781177
#else // [macOS
1179-
backgroundColor = [_backgroundColor CGColor];
1178+
RCTUIColor *backgroundColor = _backgroundColor;
11801179
#endif // macOS]
11811180

11821181
#if TARGET_OS_OSX // [macOS
@@ -1194,30 +1193,17 @@ - (void)displayLayer:(CALayer *)layer
11941193
#endif // macOS]
11951194
if (useIOSBorderRendering) {
11961195
layer.cornerRadius = cornerRadii.topLeft;
1197-
layer.borderColor = borderColors.left;
1196+
layer.borderColor = borderColors.left.CGColor;
11981197
layer.borderWidth = borderInsets.left;
1199-
layer.backgroundColor = backgroundColor;
1198+
layer.backgroundColor = backgroundColor.CGColor;
12001199
layer.contents = nil;
12011200
layer.needsDisplayOnBoundsChange = NO;
12021201
layer.mask = nil;
12031202
return;
12041203
}
12051204

1206-
#if TARGET_OS_OSX // [macOS
1207-
CGFloat scaleFactor = self.window.backingScaleFactor;
1208-
if (scaleFactor == 0.0 && RCTRunningInTestEnvironment()) {
1209-
// When running in the test environment the view is not on screen.
1210-
// Use a scaleFactor of 1 so that the test results are machine independent.
1211-
scaleFactor = 1;
1212-
}
1213-
RCTAssert(scaleFactor != 0.0, @"displayLayer occurs before the view is in a window?");
1214-
#else
1215-
// On iOS setting the scaleFactor to 0.0 will default to the device's native scale factor.
1216-
CGFloat scaleFactor = 0.0;
1217-
#endif // macOS]
1218-
12191205
UIImage *image = RCTGetBorderImage(
1220-
_borderStyle, layer.bounds.size, cornerRadii, borderInsets, borderColors, backgroundColor, self.clipsToBounds, scaleFactor); // [macOS]
1206+
_borderStyle, layer.bounds.size, cornerRadii, borderInsets, borderColors, backgroundColor, self.clipsToBounds);
12211207

12221208
layer.backgroundColor = NULL;
12231209

@@ -1238,6 +1224,7 @@ - (void)displayLayer:(CALayer *)layer
12381224
layer.contents = (id)image.CGImage;
12391225
layer.contentsScale = image.scale;
12401226
#else // [macOS
1227+
CGFloat scaleFactor = self.window.backingScaleFactor;
12411228
layer.contents = [image layerContentsForContentsScale:scaleFactor];
12421229
layer.contentsScale = scaleFactor;
12431230
#endif // macOS]

0 commit comments

Comments
 (0)