⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 287889 in webkit


Ignore:
Timestamp:
Jan 11, 2022, 10:43:31 AM (5 years ago)
Author:
weinig@apple.com
Message:

Use the new template ColorSpaceCG helpers to cleanup some code
https://bugs.webkit.org/show_bug.cgi?id=235034

Reviewed by Cameron McCormack.

Utilize the new template helpers in ColorSpaceCG.h to remove some #ifdefs.

  • platform/graphics/cg/ColorCG.cpp:

(WebCore::cachedCGColorTransform):
Add a singleton for this transform since it is always the same.

(WebCore::Color::createAndLosslesslyConvertToSupportedColorSpace):
(WebCore::convertToCGCompatibleComponents):
(WebCore::createCGColor):
(WebCore::platformConvertColorComponents):
Replace #ifdefs with constexpr checking of HasCGColorSpaceMapping<>. Also switch
some c-style arrays to std::array for consistency with the rest of the codebase.

  • platform/graphics/cg/GradientRendererCG.cpp:

(WebCore::classifyAlphaType):
Make constexpr (feedback from a previous change).

(WebCore::GradientRendererCG::makeGradient const):
(WebCore::GradientRendererCG::Shading::shadingFunction):
(WebCore::GradientRendererCG::makeShading const):
Replace #ifdefs with constexpr checking of HasCGColorSpaceMapping<>.

Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r287884 r287889  
     12022-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
    1302022-01-11  Michael Saboff  <msaboff@apple.com>
    231
  • trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp

    r287821 r287889  
    8989}
    9090
     91template<ColorSpace space>
     92static 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
    91104Color Color::createAndLosslesslyConvertToSupportedColorSpace(CGColorRef color, OptionSet<Flags> flags)
    92105{
     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
    93110    auto sourceCGColorSpace = CGColorGetColorSpace(color);
    94 #if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)
    95     auto destinationCGColorSpace = xyzD50ColorSpaceRef();
    96     auto destinationColorSpace = ColorSpace::XYZ_D50;
    97 #else
    98     auto destinationCGColorSpace = sRGBColorSpaceRef();
    99     auto destinationColorSpace = ColorSpace::SRGB;
    100 #endif
    101     ASSERT(CGColorSpaceGetNumberOfComponents(destinationCGColorSpace) == 3);
    102 
    103111    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());
    108115    ASSERT_UNUSED(result, result);
    109116
     
    144151    // not supported.
    145152
    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 };
    162162}
    163163
     
    168168   
    169169    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()));
    173173}
    174174
     
    220220
    221221    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 { };
    224224
    225225    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());
    227227    ASSERT_UNUSED(result, result);
    228228    // FIXME: CGColorTransformConvertColorComponents doesn't copy over any alpha component.
  • trunk/Source/WebCore/platform/graphics/cg/GradientRendererCG.cpp

    r287879 r287889  
    5555};
    5656
    57 static AlphaType classifyAlphaType(float alpha)
     57static constexpr AlphaType classifyAlphaType(float alpha)
    5858{
    5959    if (alpha == 1.0f)
     
    466466    colorComponents.reserveInitialCapacity(numberOfStops * 4);
    467467
    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>>;
    474486        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();
    476488            colorComponents.uncheckedAppend(r);
    477489            colorComponents.uncheckedAppend(g);
     
    481493            locations.uncheckedAppend(stop.offset);
    482494        }
    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))) };
    491500#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)) };
    507502#endif
    508503}
     
    514509{
    515510    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>>;
    521512
    522513    auto* data = static_cast<GradientRendererCG::Shading::Data*>(info);
     
    555546GradientRendererCG::Strategy GradientRendererCG::makeShading(ColorInterpolationMethod colorInterpolationMethod, const GradientColorStops& stops) const
    556547{
     548    using OutputSpaceColorType = std::conditional_t<HasCGColorSpaceMapping<ColorSpace::ExtendedSRGB>, ExtendedSRGBA<float>, SRGBA<float>>;
     549
    557550    auto makeData = [&] (auto colorInterpolationMethod, auto& stops) {
    558551        auto convertColorToColorInterpolationSpace = [&] (const Color& color, auto colorInterpolationMethod) -> ColorComponents<float, 4> {
     
    627620        };
    628621
    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        };
    635631
    636632        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));
    638634    };
    639635
     
    642638
    643639    // 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>>();
    649641
    650642    return Shading { WTFMove(data), WTFMove(function), colorSpace };
Note: See TracChangeset for help on using the changeset viewer.