Changeset 287889 in webkit
- Timestamp:
- Jan 11, 2022, 10:43:31 AM (5 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 3 edited
-
ChangeLog (modified) (1 diff)
-
platform/graphics/cg/ColorCG.cpp (modified) (4 diffs)
-
platform/graphics/cg/GradientRendererCG.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r287884 r287889 1 2022-01-11 Sam Weinig <weinig@apple.com> 2 3 Use the new template ColorSpaceCG helpers to cleanup some code 4 https://bugs.webkit.org/show_bug.cgi?id=235034 5 6 Reviewed by Cameron McCormack. 7 8 Utilize the new template helpers in ColorSpaceCG.h to remove some #ifdefs. 9 10 * platform/graphics/cg/ColorCG.cpp: 11 (WebCore::cachedCGColorTransform): 12 Add a singleton for this transform since it is always the same. 13 14 (WebCore::Color::createAndLosslesslyConvertToSupportedColorSpace): 15 (WebCore::convertToCGCompatibleComponents): 16 (WebCore::createCGColor): 17 (WebCore::platformConvertColorComponents): 18 Replace #ifdefs with constexpr checking of HasCGColorSpaceMapping<>. Also switch 19 some c-style arrays to std::array for consistency with the rest of the codebase. 20 21 * platform/graphics/cg/GradientRendererCG.cpp: 22 (WebCore::classifyAlphaType): 23 Make constexpr (feedback from a previous change). 24 25 (WebCore::GradientRendererCG::makeGradient const): 26 (WebCore::GradientRendererCG::Shading::shadingFunction): 27 (WebCore::GradientRendererCG::makeShading const): 28 Replace #ifdefs with constexpr checking of HasCGColorSpaceMapping<>. 29 1 30 2022-01-11 Michael Saboff <msaboff@apple.com> 2 31 -
trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp
r287821 r287889 89 89 } 90 90 91 template<ColorSpace space> 92 static CGColorTransformRef cachedCGColorTransform() 93 { 94 static LazyNeverDestroyed<RetainPtr<CGColorTransformRef>> transform; 95 96 static std::once_flag onceFlag; 97 std::call_once(onceFlag, [] { 98 transform.construct(adoptCF(CGColorTransformCreate(cachedCGColorSpace<space>(), nullptr))); 99 }); 100 101 return transform->get(); 102 } 103 91 104 Color Color::createAndLosslesslyConvertToSupportedColorSpace(CGColorRef color, OptionSet<Flags> flags) 92 105 { 106 // FIXME: This should probably use ExtendedSRGBA rather than XYZ_D50, as it is a more commonly used color space and just as expressive. 107 constexpr auto destinationColorSpace = HasCGColorSpaceMapping<ColorSpace::XYZ_D50> ? ColorSpace::XYZ_D50 : ColorSpace::SRGB; 108 ASSERT(CGColorSpaceGetNumberOfComponents(cachedCGColorSpace<destinationColorSpace>()) == 3); 109 93 110 auto sourceCGColorSpace = CGColorGetColorSpace(color); 94 #if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)95 auto destinationCGColorSpace = xyzD50ColorSpaceRef();96 auto destinationColorSpace = ColorSpace::XYZ_D50;97 #else98 auto destinationCGColorSpace = sRGBColorSpaceRef();99 auto destinationColorSpace = ColorSpace::SRGB;100 #endif101 ASSERT(CGColorSpaceGetNumberOfComponents(destinationCGColorSpace) == 3);102 103 111 auto sourceComponents = CGColorGetComponents(color); 104 CGFloat destinationComponents[3] { }; 105 106 auto transform = adoptCF(CGColorTransformCreate(destinationCGColorSpace, nullptr)); 107 auto result = CGColorTransformConvertColorComponents(transform.get(), sourceCGColorSpace, kCGRenderingIntentDefault, sourceComponents, destinationComponents); 112 std::array<CGFloat, 3> destinationComponents { }; 113 114 auto result = CGColorTransformConvertColorComponents(cachedCGColorTransform<destinationColorSpace>(), sourceCGColorSpace, kCGRenderingIntentDefault, sourceComponents, destinationComponents.data()); 108 115 ASSERT_UNUSED(result, result); 109 116 … … 144 151 // not supported. 145 152 146 auto cgColorSpace = cachedNullableCGColorSpace(colorSpace); 147 if (!cgColorSpace) { 148 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 149 auto componentsConvertedToExtendedSRGBA = callWithColorType(components, colorSpace, [] (const auto& color) { 150 return asColorComponents(convertColor<ExtendedSRGBA<float>>(color).resolved()); 151 }); 152 return { extendedSRGBColorSpaceRef(), componentsConvertedToExtendedSRGBA }; 153 #else 154 auto componentsConvertedToSRGBA = callWithColorType(components, colorSpace, [] (const auto& color) { 155 return asColorComponents(convertColor<SRGBA<float>>(color).resolved()); 156 }); 157 return { sRGBColorSpaceRef(), componentsConvertedToSRGBA }; 158 #endif 159 } 160 161 return { cgColorSpace, components }; 153 using FallbackColorType = std::conditional_t<HasCGColorSpaceMapping<ColorSpace::ExtendedSRGB>, ExtendedSRGBA<float>, SRGBA<float>>; 154 155 if (auto cgColorSpace = cachedNullableCGColorSpace(colorSpace)) 156 return { cgColorSpace, components }; 157 158 auto componentsConvertedToFallbackColorSpace = callWithColorType(components, colorSpace, [] (const auto& color) { 159 return asColorComponents(convertColor<FallbackColorType>(color).resolved()); 160 }); 161 return { cachedCGColorSpace<ColorSpaceFor<FallbackColorType>>(), componentsConvertedToFallbackColorSpace }; 162 162 } 163 163 … … 168 168 169 169 auto [c1, c2, c3, c4] = cgCompatibleComponents; 170 CGFloat cgFloatComponents[4]{ c1, c2, c3, c4 };171 172 return adoptCF(CGColorCreate(cgColorSpace, cgFloatComponents ));170 std::array<CGFloat, 4> cgFloatComponents { c1, c2, c3, c4 }; 171 172 return adoptCF(CGColorCreate(cgColorSpace, cgFloatComponents.data())); 173 173 } 174 174 … … 220 220 221 221 auto [c1, c2, c3, c4] = cgCompatibleComponents; 222 CGFloat sourceComponents[4]{ c1, c2, c3, c4 };223 CGFloat destinationComponents[4]{ };222 std::array<CGFloat, 4> sourceComponents { c1, c2, c3, c4 }; 223 std::array<CGFloat, 4> destinationComponents { }; 224 224 225 225 auto transform = adoptCF(CGColorTransformCreate(outputColorSpace.platformColorSpace(), nullptr)); 226 auto result = CGColorTransformConvertColorComponents(transform.get(), cgInputColorSpace, kCGRenderingIntentDefault, sourceComponents , destinationComponents);226 auto result = CGColorTransformConvertColorComponents(transform.get(), cgInputColorSpace, kCGRenderingIntentDefault, sourceComponents.data(), destinationComponents.data()); 227 227 ASSERT_UNUSED(result, result); 228 228 // FIXME: CGColorTransformConvertColorComponents doesn't copy over any alpha component. -
trunk/Source/WebCore/platform/graphics/cg/GradientRendererCG.cpp
r287879 r287889 55 55 }; 56 56 57 static AlphaType classifyAlphaType(float alpha)57 static constexpr AlphaType classifyAlphaType(float alpha) 58 58 { 59 59 if (alpha == 1.0f) … … 466 466 colorComponents.reserveInitialCapacity(numberOfStops * 4); 467 467 468 auto gradientColorSpace = sRGBColorSpaceRef(); 469 470 // FIXME: Now that we only ever use CGGradientCreateWithColorComponents, we should investigate 471 // if there is any real benefit to using sRGB when all the stops are bounded vs just using 472 // extended sRGB for all gradients. 473 if (hasOnlyBoundedSRGBColorStops(stops)) { 468 auto cgColorSpace = [&] { 469 // FIXME: Now that we only ever use CGGradientCreateWithColorComponents, we should investigate 470 // if there is any real benefit to using sRGB when all the stops are bounded vs just using 471 // extended sRGB for all gradients. 472 if (hasOnlyBoundedSRGBColorStops(stops)) { 473 for (const auto& stop : stops) { 474 auto [r, g, b, a] = stop.color.toColorTypeLossy<SRGBA<float>>().resolved(); 475 colorComponents.uncheckedAppend(r); 476 colorComponents.uncheckedAppend(g); 477 colorComponents.uncheckedAppend(b); 478 colorComponents.uncheckedAppend(a); 479 480 locations.uncheckedAppend(stop.offset); 481 } 482 return cachedCGColorSpace<ColorSpaceFor<SRGBA<float>>>(); 483 } 484 485 using OutputSpaceColorType = std::conditional_t<HasCGColorSpaceMapping<ColorSpace::ExtendedSRGB>, ExtendedSRGBA<float>, SRGBA<float>>; 474 486 for (const auto& stop : stops) { 475 auto [r, g, b, a] = stop.color.toColorTypeLossy< SRGBA<float>>().resolved();487 auto [r, g, b, a] = stop.color.toColorTypeLossy<OutputSpaceColorType>().resolved(); 476 488 colorComponents.uncheckedAppend(r); 477 489 colorComponents.uncheckedAppend(g); … … 481 493 locations.uncheckedAppend(stop.offset); 482 494 } 483 } else { 484 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 485 gradientColorSpace = extendedSRGBColorSpaceRef(); 486 #endif 487 488 for (const auto& stop : stops) { 489 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 490 auto [r, g, b, a] = stop.color.toColorTypeLossy<ExtendedSRGBA<float>>().resolved(); 495 return cachedCGColorSpace<ColorSpaceFor<OutputSpaceColorType>>(); 496 }(); 497 498 #if HAVE(CORE_GRAPHICS_GRADIENT_CREATE_WITH_OPTIONS) 499 return Gradient { adoptCF(CGGradientCreateWithColorComponentsAndOptions(cgColorSpace, colorComponents.data(), locations.data(), numberOfStops, gradientOptionsDictionary(colorInterpolationMethod))) }; 491 500 #else 492 auto [r, g, b, a] = stop.color.toColorTypeLossy<SRGBA<float>>().resolved(); 493 #endif 494 colorComponents.uncheckedAppend(r); 495 colorComponents.uncheckedAppend(g); 496 colorComponents.uncheckedAppend(b); 497 colorComponents.uncheckedAppend(a); 498 499 locations.uncheckedAppend(stop.offset); 500 } 501 } 502 503 #if HAVE(CORE_GRAPHICS_GRADIENT_CREATE_WITH_OPTIONS) 504 return Gradient { adoptCF(CGGradientCreateWithColorComponentsAndOptions(gradientColorSpace, colorComponents.data(), locations.data(), numberOfStops, gradientOptionsDictionary(colorInterpolationMethod))) }; 505 #else 506 return Gradient { adoptCF(CGGradientCreateWithColorComponents(gradientColorSpace, colorComponents.data(), locations.data(), numberOfStops)) }; 501 return Gradient { adoptCF(CGGradientCreateWithColorComponents(cgColorSpace, colorComponents.data(), locations.data(), numberOfStops)) }; 507 502 #endif 508 503 } … … 514 509 { 515 510 using InterpolationSpaceColorType = typename InterpolationSpace::ColorType; 516 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 517 using OutputSpaceColorType = ExtendedSRGBA<float>; 518 #else 519 using OutputSpaceColorType = SRGBA<float>; 520 #endif 511 using OutputSpaceColorType = std::conditional_t<HasCGColorSpaceMapping<ColorSpace::ExtendedSRGB>, ExtendedSRGBA<float>, SRGBA<float>>; 521 512 522 513 auto* data = static_cast<GradientRendererCG::Shading::Data*>(info); … … 555 546 GradientRendererCG::Strategy GradientRendererCG::makeShading(ColorInterpolationMethod colorInterpolationMethod, const GradientColorStops& stops) const 556 547 { 548 using OutputSpaceColorType = std::conditional_t<HasCGColorSpaceMapping<ColorSpace::ExtendedSRGB>, ExtendedSRGBA<float>, SRGBA<float>>; 549 557 550 auto makeData = [&] (auto colorInterpolationMethod, auto& stops) { 558 551 auto convertColorToColorInterpolationSpace = [&] (const Color& color, auto colorInterpolationMethod) -> ColorComponents<float, 4> { … … 627 620 }; 628 621 629 static const CGFloat domain[2] = { 0, 1 }; 630 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 631 static const CGFloat range[8] = { -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), 0, 1 }; 632 #else 633 static const CGFloat range[8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; 634 #endif 622 constexpr auto outputSpaceComponentInfo = OutputSpaceColorType::Model::componentInfo; 623 624 static constexpr std::array<CGFloat, 2> domain = { 0, 1 }; 625 static constexpr std::array<CGFloat, 8> range = { 626 outputSpaceComponentInfo[0].min, outputSpaceComponentInfo[0].max, 627 outputSpaceComponentInfo[1].min, outputSpaceComponentInfo[1].max, 628 outputSpaceComponentInfo[2].min, outputSpaceComponentInfo[2].max, 629 0, 1 630 }; 635 631 636 632 Ref dataRefCopy = data; 637 return adoptCF(CGFunctionCreate(&dataRefCopy.leakRef(), 1, domain, 4, range, &callbacks));633 return adoptCF(CGFunctionCreate(&dataRefCopy.leakRef(), domain.size() / 2, domain.data(), range.size() / 2, range.data(), &callbacks)); 638 634 }; 639 635 … … 642 638 643 639 // FIXME: Investigate using bounded sRGB when the input stops are all bounded sRGB. 644 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE) 645 auto colorSpace = extendedSRGBColorSpaceRef(); 646 #else 647 auto colorSpace = sRGBColorSpaceRef(); 648 #endif 640 auto colorSpace = cachedCGColorSpace<ColorSpaceFor<OutputSpaceColorType>>(); 649 641 650 642 return Shading { WTFMove(data), WTFMove(function), colorSpace };
Note:
See TracChangeset
for help on using the changeset viewer.