Changeset 275471 in webkit


Ignore:
Timestamp:
Apr 5, 2021 6:45:37 PM (3 years ago)
Author:
Simon Fraser
Message:

Wasted vector capacity in FEColorMatrix and filters
https://bugs.webkit.org/show_bug.cgi?id=224169

Reviewed by Said Abou-Hallawa.

When building filter effects, avoid the inputEffects() having a larger
capacity than needed.

Shrink the Vector<float> which is input to FEColorMatrix.

This saves around 40KB on facebook.com feed pages.

  • platform/graphics/filters/FEColorMatrix.cpp:

(WebCore::FEColorMatrix::FEColorMatrix):
(WebCore::FEColorMatrix::create):

  • platform/graphics/filters/FEColorMatrix.h:
  • rendering/CSSFilter.cpp:

(WebCore::CSSFilter::build):
(WebCore::endMatrixRow): Deleted.
(WebCore::lastMatrixRow): Deleted.

  • rendering/svg/RenderSVGResourceFilter.cpp:

(WebCore::RenderSVGResourceFilter::buildPrimitives const):

  • svg/SVGFEColorMatrixElement.cpp:

(WebCore::SVGFEColorMatrixElement::build const):

  • svg/SVGFEComponentTransferElement.cpp:

(WebCore::SVGFEComponentTransferElement::build const):

  • svg/SVGFEConvolveMatrixElement.cpp:

(WebCore::SVGFEConvolveMatrixElement::build const):

  • svg/SVGFEDiffuseLightingElement.cpp:

(WebCore::SVGFEDiffuseLightingElement::build const):

  • svg/SVGFEDropShadowElement.cpp:

(WebCore::SVGFEDropShadowElement::build const):

  • svg/SVGFEGaussianBlurElement.cpp:

(WebCore::SVGFEGaussianBlurElement::build const):

  • svg/SVGFEMergeElement.cpp:

(WebCore::SVGFEMergeElement::build const):

  • svg/SVGFEMorphologyElement.cpp:

(WebCore::SVGFEMorphologyElement::build const):

  • svg/SVGFEOffsetElement.cpp:

(WebCore::SVGFEOffsetElement::build const):

  • svg/SVGFESpecularLightingElement.cpp:

(WebCore::SVGFESpecularLightingElement::build const):

  • svg/SVGFETileElement.cpp:

(WebCore::SVGFETileElement::build const):

Location:
trunk/Source/WebCore
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r275466 r275471  
     12021-04-05  Simon Fraser  <simon.fraser@apple.com>
     2
     3        Wasted vector capacity in FEColorMatrix and filters
     4        https://bugs.webkit.org/show_bug.cgi?id=224169
     5
     6        Reviewed by Said Abou-Hallawa.
     7
     8        When building filter effects, avoid the inputEffects() having a larger
     9        capacity than needed.
     10
     11        Shrink the Vector<float> which is input to FEColorMatrix.
     12
     13        This saves around 40KB on facebook.com feed pages.
     14
     15        * platform/graphics/filters/FEColorMatrix.cpp:
     16        (WebCore::FEColorMatrix::FEColorMatrix):
     17        (WebCore::FEColorMatrix::create):
     18        * platform/graphics/filters/FEColorMatrix.h:
     19        * rendering/CSSFilter.cpp:
     20        (WebCore::CSSFilter::build):
     21        (WebCore::endMatrixRow): Deleted.
     22        (WebCore::lastMatrixRow): Deleted.
     23        * rendering/svg/RenderSVGResourceFilter.cpp:
     24        (WebCore::RenderSVGResourceFilter::buildPrimitives const):
     25        * svg/SVGFEColorMatrixElement.cpp:
     26        (WebCore::SVGFEColorMatrixElement::build const):
     27        * svg/SVGFEComponentTransferElement.cpp:
     28        (WebCore::SVGFEComponentTransferElement::build const):
     29        * svg/SVGFEConvolveMatrixElement.cpp:
     30        (WebCore::SVGFEConvolveMatrixElement::build const):
     31        * svg/SVGFEDiffuseLightingElement.cpp:
     32        (WebCore::SVGFEDiffuseLightingElement::build const):
     33        * svg/SVGFEDropShadowElement.cpp:
     34        (WebCore::SVGFEDropShadowElement::build const):
     35        * svg/SVGFEGaussianBlurElement.cpp:
     36        (WebCore::SVGFEGaussianBlurElement::build const):
     37        * svg/SVGFEMergeElement.cpp:
     38        (WebCore::SVGFEMergeElement::build const):
     39        * svg/SVGFEMorphologyElement.cpp:
     40        (WebCore::SVGFEMorphologyElement::build const):
     41        * svg/SVGFEOffsetElement.cpp:
     42        (WebCore::SVGFEOffsetElement::build const):
     43        * svg/SVGFESpecularLightingElement.cpp:
     44        (WebCore::SVGFESpecularLightingElement::build const):
     45        * svg/SVGFETileElement.cpp:
     46        (WebCore::SVGFETileElement::build const):
     47
    1482021-04-05  Saam Barati  <sbarati@apple.com>
    249
  • trunk/Source/WebCore/platform/graphics/filters/FEColorMatrix.cpp

    r275206 r275471  
    3636namespace WebCore {
    3737
    38 FEColorMatrix::FEColorMatrix(Filter& filter, ColorMatrixType type, const Vector<float>& values)
     38FEColorMatrix::FEColorMatrix(Filter& filter, ColorMatrixType type, Vector<float>&& values)
    3939    : FilterEffect(filter, Type::ColorMatrix)
    4040    , m_type(type)
    41     , m_values(values)
    42 {
    43 }
    44 
    45 Ref<FEColorMatrix> FEColorMatrix::create(Filter& filter, ColorMatrixType type, const Vector<float>& values)
    46 {
    47     return adoptRef(*new FEColorMatrix(filter, type, values));
     41    , m_values(WTFMove(values))
     42{
     43}
     44
     45Ref<FEColorMatrix> FEColorMatrix::create(Filter& filter, ColorMatrixType type, Vector<float>&& values)
     46{
     47    return adoptRef(*new FEColorMatrix(filter, type, WTFMove(values)));
    4848}
    4949
  • trunk/Source/WebCore/platform/graphics/filters/FEColorMatrix.h

    r266542 r275471  
    3939class FEColorMatrix : public FilterEffect {
    4040public:
    41     static Ref<FEColorMatrix> create(Filter&, ColorMatrixType, const Vector<float>&);
     41    static Ref<FEColorMatrix> create(Filter&, ColorMatrixType, Vector<float>&&);
    4242
    4343    ColorMatrixType type() const { return m_type; }
     
    5151
    5252private:
    53     FEColorMatrix(Filter&, ColorMatrixType, const Vector<float>&);
     53    FEColorMatrix(Filter&, ColorMatrixType, Vector<float>&&);
    5454
    5555    const char* filterName() const final { return "FEColorMatrix"; }
  • trunk/Source/WebCore/rendering/CSSFilter.cpp

    r272475 r275471  
    5353namespace WebCore {
    5454
    55 static inline void endMatrixRow(Vector<float>& parameters)
    56 {
    57     parameters.append(0);
    58     parameters.append(0);
    59 }
    60 
    61 static inline void lastMatrixRow(Vector<float>& parameters)
    62 {
    63     parameters.append(0);
    64     parameters.append(0);
    65     parameters.append(0);
    66     parameters.append(1);
    67     parameters.append(0);
    68 }
    69 
    7055Ref<CSSFilter> CSSFilter::create()
    7156{
     
    156141        case FilterOperation::GRAYSCALE: {
    157142            auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
    158             Vector<float> inputParameters;
    159143            double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
    160144
     
    162146            // for information on parameters.
    163147
    164             inputParameters.append(narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount));
    165             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
    166             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
    167             endMatrixRow(inputParameters);
    168 
    169             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
    170             inputParameters.append(narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount));
    171             inputParameters.append(narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount));
    172             endMatrixRow(inputParameters);
    173 
    174             inputParameters.append(narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount));
    175             inputParameters.append(narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount));
    176             inputParameters.append(narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount));
    177             endMatrixRow(inputParameters);
    178 
    179             lastMatrixRow(inputParameters);
    180 
    181             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
     148            Vector<float> inputParameters {
     149                narrowPrecisionToFloat(0.2126 + 0.7874 * oneMinusAmount),
     150                narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount),
     151                narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount),
     152                0,
     153                0,
     154
     155                narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount),
     156                narrowPrecisionToFloat(0.7152 + 0.2848 * oneMinusAmount),
     157                narrowPrecisionToFloat(0.0722 - 0.0722 * oneMinusAmount),
     158                0,
     159                0,
     160
     161                narrowPrecisionToFloat(0.2126 - 0.2126 * oneMinusAmount),
     162                narrowPrecisionToFloat(0.7152 - 0.7152 * oneMinusAmount),
     163                narrowPrecisionToFloat(0.0722 + 0.9278 * oneMinusAmount),
     164                0,
     165                0,
     166
     167                0,
     168                0,
     169                0,
     170                1,
     171                0,
     172            };
     173
     174            effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, WTFMove(inputParameters));
    182175            break;
    183176        }
    184177        case FilterOperation::SEPIA: {
    185178            auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
    186             Vector<float> inputParameters;
    187179            double oneMinusAmount = clampTo(1 - colorMatrixOperation.amount(), 0.0, 1.0);
    188180
     
    190182            // for information on parameters.
    191183
    192             inputParameters.append(narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount));
    193             inputParameters.append(narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount));
    194             inputParameters.append(narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount));
    195             endMatrixRow(inputParameters);
    196 
    197             inputParameters.append(narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount));
    198             inputParameters.append(narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount));
    199             inputParameters.append(narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount));
    200             endMatrixRow(inputParameters);
    201 
    202             inputParameters.append(narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount));
    203             inputParameters.append(narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount));
    204             inputParameters.append(narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount));
    205             endMatrixRow(inputParameters);
    206 
    207             lastMatrixRow(inputParameters);
    208 
    209             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, inputParameters);
     184            Vector<float> inputParameters {
     185                narrowPrecisionToFloat(0.393 + 0.607 * oneMinusAmount),
     186                narrowPrecisionToFloat(0.769 - 0.769 * oneMinusAmount),
     187                narrowPrecisionToFloat(0.189 - 0.189 * oneMinusAmount),
     188                0,
     189                0,
     190
     191                narrowPrecisionToFloat(0.349 - 0.349 * oneMinusAmount),
     192                narrowPrecisionToFloat(0.686 + 0.314 * oneMinusAmount),
     193                narrowPrecisionToFloat(0.168 - 0.168 * oneMinusAmount),
     194                0,
     195                0,
     196
     197                narrowPrecisionToFloat(0.272 - 0.272 * oneMinusAmount),
     198                narrowPrecisionToFloat(0.534 - 0.534 * oneMinusAmount),
     199                narrowPrecisionToFloat(0.131 + 0.869 * oneMinusAmount),
     200                0,
     201                0,
     202
     203                0,
     204                0,
     205                0,
     206                1,
     207                0,
     208            };
     209
     210            effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_MATRIX, WTFMove(inputParameters));
    210211            break;
    211212        }
    212213        case FilterOperation::SATURATE: {
    213214            auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
    214             Vector<float> inputParameters;
    215             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
    216             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_SATURATE, inputParameters);
     215            Vector<float> inputParameters { narrowPrecisionToFloat(colorMatrixOperation.amount()) };
     216            effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_SATURATE, WTFMove(inputParameters));
    217217            break;
    218218        }
    219219        case FilterOperation::HUE_ROTATE: {
    220220            auto& colorMatrixOperation = downcast<BasicColorMatrixFilterOperation>(filterOperation);
    221             Vector<float> inputParameters;
    222             inputParameters.append(narrowPrecisionToFloat(colorMatrixOperation.amount()));
    223             effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_HUEROTATE, inputParameters);
     221            Vector<float> inputParameters { narrowPrecisionToFloat(colorMatrixOperation.amount()) };
     222            effect = FEColorMatrix::create(*this, FECOLORMATRIX_TYPE_HUEROTATE, WTFMove(inputParameters));
    224223            break;
    225224        }
     
    297296           
    298297            if (filterOperation.type() != FilterOperation::REFERENCE) {
    299                 effect->inputEffects().append(WTFMove(previousEffect));
     298                effect->inputEffects() = { WTFMove(previousEffect) };
    300299                m_effects.append(*effect);
    301300            }
     
    307306    if (m_effects.isEmpty())
    308307        return false;
     308
     309    m_effects.shrinkToFit();
    309310
    310311    setMaxEffectRects(m_sourceDrawingRegion);
  • trunk/Source/WebCore/rendering/svg/RenderSVGResourceFilter.cpp

    r272475 r275471  
    9595   
    9696    for (auto& element : childrenOfType<SVGFilterPrimitiveStandardAttributes>(filterElement())) {
    97         RefPtr<FilterEffect> effect = element.build(builder.get(), filter);
     97        auto effect = element.build(builder.get(), filter);
    9898        if (!effect) {
    9999            builder->clearEffects();
  • trunk/Source/WebCore/svg/SVGFEColorMatrixElement.cpp

    r246490 r275471  
    114114    if (!hasAttribute(SVGNames::valuesAttr)) {
    115115        switch (filterType) {
    116         case FECOLORMATRIX_TYPE_MATRIX:
    117             for (size_t i = 0; i < 20; i++)
    118                 filterValues.append((i % 6) ? 0 : 1);
     116        case FECOLORMATRIX_TYPE_MATRIX: {
     117            static constexpr unsigned matrixValueCount = 20;
     118            filterValues.reserveInitialCapacity(matrixValueCount);
     119            for (size_t i = 0; i < matrixValueCount; i++)
     120                filterValues.uncheckedAppend((i % 6) ? 0 : 1);
    119121            break;
     122        }
    120123        case FECOLORMATRIX_TYPE_HUEROTATE:
    121             filterValues.append(0);
     124            filterValues = { 0 };
    122125            break;
    123126        case FECOLORMATRIX_TYPE_SATURATE:
    124             filterValues.append(1);
     127            filterValues = { 1 };
    125128            break;
    126129        default:
     
    136139       
    137140        filterValues = values();
     141        filterValues.shrinkToFit();
    138142    }
    139143
    140     auto effect = FEColorMatrix::create(filter, filterType, filterValues);
    141     effect->inputEffects().append(input1);
     144    auto effect = FEColorMatrix::create(filter, filterType, WTFMove(filterValues));
     145    effect->inputEffects() = { input1 };
    142146    return effect;
    143147}
  • trunk/Source/WebCore/svg/SVGFEComponentTransferElement.cpp

    r246490 r275471  
    8787   
    8888    auto effect = FEComponentTransfer::create(filter, red, green, blue, alpha);
    89     effect->inputEffects().append(input1);
     89    effect->inputEffects() = { input1 };
    9090    return effect;
    9191}
  • trunk/Source/WebCore/svg/SVGFEConvolveMatrixElement.cpp

    r263334 r275471  
    247247
    248248    auto effect = FEConvolveMatrix::create(filter, IntSize(orderXValue, orderYValue), divisorValue, bias(), IntPoint(targetXValue, targetYValue), edgeMode(), FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), preserveAlpha(), kernelMatrix);
    249     effect->inputEffects().append(input1);
     249    effect->inputEffects() = { input1 };
    250250    return effect;
    251251}
  • trunk/Source/WebCore/svg/SVGFEDiffuseLightingElement.cpp

    r263334 r275471  
    172172
    173173    auto effect = FEDiffuseLighting::create(filter, color, surfaceScale(), diffuseConstant(), kernelUnitLengthX(), kernelUnitLengthY(), WTFMove(lightSource));
    174     effect->inputEffects().append(input1);
     174    effect->inputEffects() = { input1 };
    175175    return effect;
    176176}
  • trunk/Source/WebCore/svg/SVGFEDropShadowElement.cpp

    r263334 r275471  
    117117
    118118    auto effect = FEDropShadow::create(filter, stdDeviationX(), stdDeviationY(), dx(), dy(), color, opacity);
    119     effect->inputEffects().append(input1);
     119    effect->inputEffects() = { input1 };
    120120    return effect;
    121121}
  • trunk/Source/WebCore/svg/SVGFEGaussianBlurElement.cpp

    r263334 r275471  
    9999{
    100100    auto input1 = filterBuilder->getEffectById(in1());
    101 
    102101    if (!input1)
    103102        return nullptr;
     
    107106
    108107    auto effect = FEGaussianBlur::create(filter, stdDeviationX(), stdDeviationY(), edgeMode());
    109     effect->inputEffects().append(input1);
     108    effect->inputEffects() = { input1 };
    110109    return effect;
    111110}
  • trunk/Source/WebCore/svg/SVGFEMergeElement.cpp

    r243185 r275471  
    4848{
    4949    auto effect = FEMerge::create(filter);
    50     FilterEffectVector& mergeInputs = effect->inputEffects();
     50    auto& mergeInputs = effect->inputEffects();
    5151
    5252    for (auto& mergeNode : childrenOfType<SVGFEMergeNodeElement>(*this)) {
     
    5757    }
    5858
     59    mergeInputs.shrinkToFit();
     60
    5961    if (mergeInputs.isEmpty())
    6062        return nullptr;
  • trunk/Source/WebCore/svg/SVGFEMorphologyElement.cpp

    r263334 r275471  
    128128
    129129    auto effect = FEMorphology::create(filter, svgOperator(), xRadius, yRadius);
    130     effect->inputEffects().append(input1);
     130    effect->inputEffects() = { input1 };
    131131    return effect;
    132132}
  • trunk/Source/WebCore/svg/SVGFEOffsetElement.cpp

    r246490 r275471  
    9090
    9191    auto effect = FEOffset::create(filter, dx(), dy());
    92     effect->inputEffects().append(input1);
     92    effect->inputEffects() = { input1 };
    9393    return effect;
    9494}
  • trunk/Source/WebCore/svg/SVGFESpecularLightingElement.cpp

    r263334 r275471  
    181181
    182182    auto effect = FESpecularLighting::create(filter, color, surfaceScale(), specularConstant(), specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), WTFMove(lightSource));
    183     effect->inputEffects().append(input1);
     183    effect->inputEffects() = { input1 };
    184184    return effect;
    185185}
  • trunk/Source/WebCore/svg/SVGFETileElement.cpp

    r246490 r275471  
    7979
    8080    auto effect = FETile::create(filter);
    81     effect->inputEffects().append(input1);
     81    effect->inputEffects() = { input1 };
    8282    return effect;
    8383}
Note: See TracChangeset for help on using the changeset viewer.