Ignore:
Timestamp:
Dec 23, 2021, 1:35:00 PM (3 years ago)
Author:
[email protected]
Message:

Encapsulate gradient color stops into a self contained class
https://bugs.webkit.org/show_bug.cgi?id=234583

Reviewed by Simon Fraser.

Source/WebCore:

Replace most uses of Gradient::ColorStopVector with new GradientColorStops class.

  • Headers.cmake:
  • WebCore.xcodeproj/project.pbxproj:

Add new file.

  • css/CSSGradientValue.h:
  • css/CSSGradientValue.cpp:

(WebCore::CSSGradientValue::computeStops):
Replace some usage of Gradient::ColorStopVector with GradientColorStops. While here,
optimize color filter transformation to only happen when there is color filter,
removing extra unnecessary copies of Colors.

Also utilizes the GradientColorStops::Sorted type to create a GradientColorStops
object that has the isSorted bit set.

(WebCore::CSSLinearGradientValue::createGradient):
(WebCore::CSSRadialGradientValue::createGradient):
(WebCore::CSSConicGradientValue::createGradient):
The calls to setSortedColorStops is no longer needed, as the GradientColorStops
now maintains that state.

  • platform/graphics/Color.h:

(WebCore::add):
Move definition of add(Hasher&, Color) here, where it makes sense, rather than
keeping it in Gradient.

  • platform/graphics/FloatPoint.h:

(WebCore::add):
Move definition of add(Hasher&, FloatPoint) here, where it makes sense, rather than
keeping it in Gradient.

  • platform/graphics/Gradient.h:
  • platform/graphics/Gradient.cpp:

(WebCore::Gradient::create):
(WebCore::Gradient::Gradient):
(WebCore::Gradient::addColorStop):
(WebCore::Gradient::hash const):
(WebCore::Gradient::setSortedColorStops): Deleted.
(WebCore::Gradient::sortStops const): Deleted.
Replace ColorStopVector with GradientColorStops. This allows removing the m_stopsSorted
bit, as the new class maintains that, as well as removing setSortedColorStops since
you can achieve this by just creating the Gradient with a GradientColorStops that knows
it is sorted (using the GradientColorStops::Sorted helper).

  • platform/graphics/GradientColorStop.h:

(WebCore::add):
Move definition of add(Hasher&, GradientColorStop) here, where it makes sense, rather than
keeping it in Gradient.

  • platform/graphics/GradientColorStops.h: Added.

(WebCore::GradientColorStops::GradientColorStops):
(WebCore::GradientColorStops::addColorStop):
(WebCore::GradientColorStops::sort):
(WebCore::GradientColorStops::sorted const):
(WebCore::GradientColorStops::size const):
(WebCore::GradientColorStops::isEmpty const):
(WebCore::GradientColorStops::begin const):
(WebCore::GradientColorStops::end const):
(WebCore::GradientColorStops::mapColors const):
(WebCore::GradientColorStops::stops const):
(WebCore::GradientColorStops::validateIsSorted const):
(WebCore::GradientColorStops::encode const):
(WebCore::GradientColorStops::decode):
Encapsulate state and functionality of the gradient color stop list, maintaining
the sorted state, and providing a pleasent API to work with. In the future, this
will be a good place to add functions to analyze and transform the list for optimizing
what can use the gradient fast paths.

  • platform/graphics/cairo/GradientCairo.cpp:

Update to use the new names.

  • platform/graphics/cg/GradientCG.cpp:

(WebCore::Gradient::paint):
Ensure the color stops passed to the gradient renderer are sorted using the sorted()
helper, which does an inplace sort and returns a reference to itself.

  • platform/graphics/cg/GradientRendererCG.h:
  • platform/graphics/cg/GradientRendererCG.cpp:

(WebCore::GradientRendererCG::GradientRendererCG):
(WebCore::GradientRendererCG::pickStrategy const):
(WebCore::GradientRendererCG::makeGradient const):
(WebCore::GradientRendererCG::makeShading const):
Replace uses of GradientColorStopVector with GradientColorStops.

  • rendering/svg/RenderSVGResourceGradient.h:
  • rendering/svg/RenderSVGResourceGradient.cpp:

(WebCore::RenderSVGResourceGradient::stopsByApplyingColorFilter):
Add early return if there is no color filter to apply, and utilize the mapColors()
function to update the colors if there is.

  • rendering/svg/RenderSVGResourceLinearGradient.cpp:

(WebCore::RenderSVGResourceLinearGradient::buildGradient const):

  • rendering/svg/RenderSVGResourceRadialGradient.cpp:

(WebCore::RenderSVGResourceRadialGradient::buildGradient const):
Remove some extraneous type names.

  • svg/GradientAttributes.h:

(WebCore::GradientAttributes::stops const):
(WebCore::GradientAttributes::setStops):

  • svg/SVGGradientElement.cpp:

(WebCore::SVGGradientElement::buildStops):

  • svg/SVGGradientElement.h:

Replace uses of GradientColorStopVector with GradientColorStops.

Tools:

  • TestRunnerShared/PlatformGTK.cmake: Added.
  • TestRunnerShared/PlatformWPE.cmake: Added.

Keep GTK and WPE ports building by propogating glib.h header to the test runnner. Change
by Fujii Hironori.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/css/CSSGradientValue.cpp

    r287310 r287411  
    301301
    302302template<typename GradientAdapter>
    303 Gradient::ColorStopVector CSSGradientValue::computeStops(GradientAdapter& gradientAdapter, const CSSToLengthConversionData& conversionData, const RenderStyle& style, float maxLengthForRepeat)
    304 {
     303GradientColorStops CSSGradientValue::computeStops(GradientAdapter& gradientAdapter, const CSSToLengthConversionData& conversionData, const RenderStyle& style, float maxLengthForRepeat)
     304{
     305    bool hasColorFilter = style.hasAppleColorFilter();
     306
    305307    if (m_gradientType == CSSDeprecatedLinearGradient || m_gradientType == CSSDeprecatedRadialGradient) {
    306         Gradient::ColorStopVector result;
     308        GradientColorStops::StopVector result;
    307309        result.reserveInitialCapacity(m_stops.size());
    308 
     310       
    309311        for (auto& stop : m_stops) {
    310312            float offset;
     
    314316                offset = stop.position->floatValue(CSSUnitType::CSS_NUMBER);
    315317
    316             Color color = stop.resolvedColor;
    317             if (style.hasAppleColorFilter())
    318                 style.appleColorFilter().transformColor(color);
    319             result.uncheckedAppend({ offset, color });
    320         }
    321 
    322         std::stable_sort(result.begin(), result.end(), [] (const Gradient::ColorStop& a, const Gradient::ColorStop& b) {
     318            result.uncheckedAppend({ offset, hasColorFilter ? style.colorByApplyingColorFilter(stop.resolvedColor) : stop.resolvedColor });
     319        }
     320
     321        std::stable_sort(result.begin(), result.end(), [] (const GradientColorStop& a, const GradientColorStop& b) {
    323322            return a.offset < b.offset;
    324323        });
    325324
    326         return result;
     325        return GradientColorStops::Sorted { WTFMove(result) };
    327326    }
    328327
     
    335334        auto& stop = m_stops[i];
    336335
    337         Color color = stop.resolvedColor;
    338         if (style.hasAppleColorFilter())
    339             style.appleColorFilter().transformColor(color);
    340 
    341         stops[i].color = color;
     336        stops[i].color = hasColorFilter ? style.colorByApplyingColorFilter(stop.resolvedColor) : stop.resolvedColor;
    342337
    343338        if (stop.position) {
     
    555550        gradientAdapter.normalizeStopsAndEndpointsOutsideRange(stops, m_colorInterpolationMethod);
    556551   
    557     Gradient::ColorStopVector result;
     552    GradientColorStops::StopVector result;
    558553    result.reserveInitialCapacity(stops.size());
    559554    for (auto& stop : stops)
    560         result.uncheckedAppend({ *stop.offset, stop.color });
    561 
    562     return result;
     555        result.uncheckedAppend({ *stop.offset, WTFMove(stop.color) });
     556
     557    return GradientColorStops::Sorted { WTFMove(result) };
    563558}
    564559
     
    868863    auto stops = computeStops(adapter, conversionData, renderer.style(), 1);
    869864
    870     auto gradient = Gradient::create(WTFMove(data), colorInterpolationMethod());
    871     gradient->setSortedColorStops(WTFMove(stops));
    872     return gradient;
     865    return Gradient::create(WTFMove(data), colorInterpolationMethod(), GradientSpreadMethod::Pad, WTFMove(stops));
    873866}
    874867
     
    12021195    auto stops = computeStops(adapter, conversionData, renderer.style(), maxExtent);
    12031196
    1204     auto gradient = Gradient::create(WTFMove(data), colorInterpolationMethod());
    1205     gradient->setSortedColorStops(WTFMove(stops));
    1206     return gradient;
     1197    return Gradient::create(WTFMove(data), colorInterpolationMethod(), GradientSpreadMethod::Pad, WTFMove(stops));
    12071198}
    12081199
     
    12761267    auto stops = computeStops(adapter, conversionData, renderer.style(), 1);
    12771268
    1278     auto gradient = Gradient::create(WTFMove(data), colorInterpolationMethod());
    1279     gradient->setSortedColorStops(WTFMove(stops));
    1280     return gradient;
     1269    return Gradient::create(WTFMove(data), colorInterpolationMethod(), GradientSpreadMethod::Pad, WTFMove(stops));
    12811270}
    12821271
Note: See TracChangeset for help on using the changeset viewer.