Changeset 225088 in webkit


Ignore:
Timestamp:
Nov 21, 2017 5:02:24 PM (6 years ago)
Author:
Simon Fraser
Message:

Some FELighting cleanup
https://bugs.webkit.org/show_bug.cgi?id=179924

Reviewed by Sam Weinig.

Make the normal-computation functions const and have them return an IntSize.
Replace bit shifting with multiply by two (compilers know how to do this, folks).

  • platform/graphics/filters/FELighting.cpp:

(WebCore::FELighting::LightingData::topLeftNormal const):
(WebCore::FELighting::LightingData::topRowNormal const):
(WebCore::FELighting::LightingData::topRightNormal const):
(WebCore::FELighting::LightingData::leftColumnNormal const):
(WebCore::FELighting::LightingData::interiorNormal const):
(WebCore::FELighting::LightingData::rightColumnNormal const):
(WebCore::FELighting::LightingData::bottomLeftNormal const):
(WebCore::FELighting::LightingData::bottomRowNormal const):
(WebCore::FELighting::LightingData::bottomRightNormal const):
(WebCore::FELighting::inlineSetPixel):
(WebCore::FELighting::setPixel):
(WebCore::FELighting::platformApplyGenericPaint):
(WebCore::FELighting::drawLighting):
(WebCore::FELighting::LightingData::topLeft): Deleted.
(WebCore::FELighting::LightingData::topRow): Deleted.
(WebCore::FELighting::LightingData::topRight): Deleted.
(WebCore::FELighting::LightingData::leftColumn): Deleted.
(WebCore::FELighting::LightingData::interior): Deleted.
(WebCore::FELighting::LightingData::rightColumn): Deleted.
(WebCore::FELighting::LightingData::bottomLeft): Deleted.
(WebCore::FELighting::LightingData::bottomRow): Deleted.
(WebCore::FELighting::LightingData::bottomRight): Deleted.

  • platform/graphics/filters/FELighting.h:
Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r225087 r225088  
     12017-11-21  Simon Fraser  <simon.fraser@apple.com>
     2
     3        Some FELighting cleanup
     4        https://bugs.webkit.org/show_bug.cgi?id=179924
     5
     6        Reviewed by Sam Weinig.
     7
     8        Make the normal-computation functions const and have them return an IntSize.
     9        Replace bit shifting with multiply by two (compilers know how to do this, folks).
     10
     11        * platform/graphics/filters/FELighting.cpp:
     12        (WebCore::FELighting::LightingData::topLeftNormal const):
     13        (WebCore::FELighting::LightingData::topRowNormal const):
     14        (WebCore::FELighting::LightingData::topRightNormal const):
     15        (WebCore::FELighting::LightingData::leftColumnNormal const):
     16        (WebCore::FELighting::LightingData::interiorNormal const):
     17        (WebCore::FELighting::LightingData::rightColumnNormal const):
     18        (WebCore::FELighting::LightingData::bottomLeftNormal const):
     19        (WebCore::FELighting::LightingData::bottomRowNormal const):
     20        (WebCore::FELighting::LightingData::bottomRightNormal const):
     21        (WebCore::FELighting::inlineSetPixel):
     22        (WebCore::FELighting::setPixel):
     23        (WebCore::FELighting::platformApplyGenericPaint):
     24        (WebCore::FELighting::drawLighting):
     25        (WebCore::FELighting::LightingData::topLeft): Deleted.
     26        (WebCore::FELighting::LightingData::topRow): Deleted.
     27        (WebCore::FELighting::LightingData::topRight): Deleted.
     28        (WebCore::FELighting::LightingData::leftColumn): Deleted.
     29        (WebCore::FELighting::LightingData::interior): Deleted.
     30        (WebCore::FELighting::LightingData::rightColumn): Deleted.
     31        (WebCore::FELighting::LightingData::bottomLeft): Deleted.
     32        (WebCore::FELighting::LightingData::bottomRow): Deleted.
     33        (WebCore::FELighting::LightingData::bottomRight): Deleted.
     34        * platform/graphics/filters/FELighting.h:
     35
    1362017-11-21  Christopher Reid  <chris.reid@sony.com>
    237
  • trunk/Source/WebCore/platform/graphics/filters/FELighting.cpp

    r225026 r225088  
    8686const static int cAlphaChannelOffset = 3;
    8787const static unsigned char cOpaqueAlpha = static_cast<unsigned char>(0xff);
     88
     89// These factors and the normal coefficients come from the table under https://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement.
    8890const static float cFactor1div2 = -1 / 2.f;
    8991const static float cFactor1div3 = -1 / 3.f;
     
    9193const static float cFactor2div3 = -2 / 3.f;
    9294
    93 // << 1 is signed multiply by 2
    94 inline void FELighting::LightingData::topLeft(int offset, IntPoint& normalVector)
     95inline IntSize FELighting::LightingData::topLeftNormal(int offset) const
    9596{
    9697    int center = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
     
    99100    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    100101    int bottomRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    101     normalVector.setX(-(center << 1) + (right << 1) - bottom + bottomRight);
    102     normalVector.setY(-(center << 1) - right + (bottom << 1) + bottomRight);
    103 }
    104 
    105 inline void FELighting::LightingData::topRow(int offset, IntPoint& normalVector)
     102    return {
     103        -2 * center + 2 * right - bottom + bottomRight,
     104        -2 * center - right + 2 * bottom + bottomRight
     105    };
     106}
     107
     108inline IntSize FELighting::LightingData::topRowNormal(int offset) const
    106109{
    107110    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    112115    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    113116    int bottomRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    114     normalVector.setX(-(left << 1) + (right << 1) - bottomLeft + bottomRight);
    115     normalVector.setY(-left - (center << 1) - right + bottomLeft + (bottom << 1) + bottomRight);
    116 }
    117 
    118 inline void FELighting::LightingData::topRight(int offset, IntPoint& normalVector)
     117    return {
     118        -2 * left + 2 * right - bottomLeft + bottomRight,
     119        -left - 2 * center - right + bottomLeft + 2 * bottom + bottomRight
     120    };
     121}
     122
     123inline IntSize FELighting::LightingData::topRightNormal(int offset) const
    119124{
    120125    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    123128    int bottomLeft = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
    124129    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    125     normalVector.setX(-(left << 1) + (center << 1) - bottomLeft + bottom);
    126     normalVector.setY(-left - (center << 1) + bottomLeft + (bottom << 1));
    127 }
    128 
    129 inline void FELighting::LightingData::leftColumn(int offset, IntPoint& normalVector)
     130    return {
     131        -2 * left + 2 * center - bottomLeft + bottom,
     132        -left - 2 * center + bottomLeft + 2 * bottom
     133    };
     134}
     135
     136inline IntSize FELighting::LightingData::leftColumnNormal(int offset) const
    130137{
    131138    int center = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
     
    134141    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    135142    int topRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    136     offset += widthMultipliedByPixelSize << 1;
     143    offset += 2 * widthMultipliedByPixelSize;
    137144    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    138145    int bottomRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    139     normalVector.setX(-top + topRight - (center << 1) + (right << 1) - bottom + bottomRight);
    140     normalVector.setY(-(top << 1) - topRight + (bottom << 1) + bottomRight);
    141 }
    142 
    143 inline void FELighting::LightingData::interior(int offset, IntPoint& normalVector)
     146    return {
     147        -top + topRight - 2 * center + 2 * right - bottom + bottomRight,
     148        -2 * top - topRight + 2 * bottom + bottomRight
     149    };
     150}
     151
     152inline IntSize FELighting::LightingData::interiorNormal(int offset) const
    144153{
    145154    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    149158    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    150159    int topRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    151     offset += widthMultipliedByPixelSize << 1;
     160    offset += 2 * widthMultipliedByPixelSize;
    152161    int bottomLeft = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
    153162    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    154163    int bottomRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    155     normalVector.setX(-topLeft + topRight - (left << 1) + (right << 1) - bottomLeft + bottomRight);
    156     normalVector.setY(-topLeft - (top << 1) - topRight + bottomLeft + (bottom << 1) + bottomRight);
    157 }
    158 
    159 inline void FELighting::LightingData::rightColumn(int offset, IntPoint& normalVector)
     164    return {
     165        -topLeft + topRight - 2 * left + 2 * right - bottomLeft + bottomRight,
     166        -topLeft - 2 * top - topRight + bottomLeft + 2 * bottom + bottomRight
     167    };
     168}
     169
     170inline IntSize FELighting::LightingData::rightColumnNormal(int offset) const
    160171{
    161172    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    164175    int topLeft = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
    165176    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    166     offset += widthMultipliedByPixelSize << 1;
     177    offset += 2 * widthMultipliedByPixelSize;
    167178    int bottomLeft = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
    168179    int bottom = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    169     normalVector.setX(-topLeft + top - (left << 1) + (center << 1) - bottomLeft + bottom);
    170     normalVector.setY(-topLeft - (top << 1) + bottomLeft + (bottom << 1));
    171 }
    172 
    173 inline void FELighting::LightingData::bottomLeft(int offset, IntPoint& normalVector)
     180    return {
     181        -topLeft + top - 2 * left + 2 * center - bottomLeft + bottom,
     182        -topLeft - 2 * top + bottomLeft + 2 * bottom
     183    };
     184}
     185
     186inline IntSize FELighting::LightingData::bottomLeftNormal(int offset) const
    174187{
    175188    int center = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
     
    178191    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    179192    int topRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    180     normalVector.setX(-top + topRight - (center << 1) + (right << 1));
    181     normalVector.setY(-(top << 1) - topRight + (center << 1) + right);
    182 }
    183 
    184 inline void FELighting::LightingData::bottomRow(int offset, IntPoint& normalVector)
     193    return {
     194        -top + topRight - 2 * center + 2 * right,
     195        -2 * top - topRight + 2 * center + right
     196    };
     197}
     198
     199inline IntSize FELighting::LightingData::bottomRowNormal(int offset) const
    185200{
    186201    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    191206    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    192207    int topRight = static_cast<int>(pixels->item(offset + cPixelSize + cAlphaChannelOffset));
    193     normalVector.setX(-topLeft + topRight - (left << 1) + (right << 1));
    194     normalVector.setY(-topLeft - (top << 1) - topRight + left + (center << 1) + right);
    195 }
    196 
    197 inline void FELighting::LightingData::bottomRight(int offset, IntPoint& normalVector)
     208    return {
     209        -topLeft + topRight - 2 * left + 2 * right,
     210        -topLeft - 2 * top - topRight + left + 2 * center + right
     211    };
     212}
     213
     214inline IntSize FELighting::LightingData::bottomRightNormal(int offset) const
    198215{
    199216    int left = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
     
    202219    int topLeft = static_cast<int>(pixels->item(offset - cPixelSize + cAlphaChannelOffset));
    203220    int top = static_cast<int>(pixels->item(offset + cAlphaChannelOffset));
    204     normalVector.setX(-topLeft + top - (left << 1) + (center << 1));
    205     normalVector.setY(-topLeft - (top << 1) + left + (center << 1));
    206 }
    207 
    208 inline void FELighting::inlineSetPixel(int offset, LightingData& data, LightSource::PaintingData& paintingData,
    209                                        int lightX, int lightY, float factorX, float factorY, IntPoint& normal2DVector)
     221    return {
     222        -topLeft + top - 2 * left + 2 * center,
     223        topLeft - 2 * top + left + 2 * center
     224    };
     225}
     226
     227inline void FELighting::inlineSetPixel(int offset, LightingData& data, LightSource::PaintingData& paintingData, int lightX, int lightY, float factorX, float factorY, IntSize normal2DVector)
    210228{
    211229    m_lightSource->updatePaintingData(paintingData, lightX, lightY, static_cast<float>(data.pixels->item(offset + cAlphaChannelOffset)) * data.surfaceScale);
    212230
    213231    float lightStrength;
    214     if (!normal2DVector.x() && !normal2DVector.y()) {
     232    if (normal2DVector.isZero()) {
    215233        // Normal vector is (0, 0, 1). This is a quite frequent case.
    216234        if (m_lightingType == FELighting::DiffuseLighting)
     
    227245    } else {
    228246        FloatPoint3D normalVector;
    229         normalVector.setX(factorX * static_cast<float>(normal2DVector.x()) * data.surfaceScale);
    230         normalVector.setY(factorY * static_cast<float>(normal2DVector.y()) * data.surfaceScale);
     247        normalVector.setX(factorX * static_cast<float>(normal2DVector.width()) * data.surfaceScale);
     248        normalVector.setY(factorY * static_cast<float>(normal2DVector.height()) * data.surfaceScale);
    231249        normalVector.setZ(1);
    232250        float normalVectorLength = normalVector.length();
     
    255273}
    256274
    257 void FELighting::setPixel(int offset, LightingData& data, LightSource::PaintingData& paintingData,
    258                           int lightX, int lightY, float factorX, float factorY, IntPoint& normalVector)
     275void FELighting::setPixel(int offset, LightingData& data, LightSource::PaintingData& paintingData, int lightX, int lightY, float factorX, float factorY, IntSize normalVector)
    259276{
    260277    inlineSetPixel(offset, data, paintingData, lightX, lightY, factorX, factorY, normalVector);
     
    263280inline void FELighting::platformApplyGenericPaint(LightingData& data, LightSource::PaintingData& paintingData, int startY, int endY)
    264281{
    265     IntPoint normalVector;
    266     int offset = 0;
    267 
    268282    for (int y = startY; y < endY; ++y) {
    269         offset = y * data.widthMultipliedByPixelSize + cPixelSize;
     283        int offset = y * data.widthMultipliedByPixelSize + cPixelSize;
    270284        for (int x = 1; x < data.widthDecreasedByOne; ++x, offset += cPixelSize) {
    271             data.interior(offset, normalVector);
    272             inlineSetPixel(offset, data, paintingData, x, y, cFactor1div4, cFactor1div4, normalVector);
     285            inlineSetPixel(offset, data, paintingData, x, y, cFactor1div4, cFactor1div4, data.interiorNormal(offset));
    273286        }
    274287    }
     
    342355    m_lightSource->initPaintingData(paintingData);
    343356
    344     // Top/Left corner.
    345     IntPoint normalVector;
     357    // Top left.
    346358    int offset = 0;
    347     data.topLeft(offset, normalVector);
    348     setPixel(offset, data, paintingData, 0, 0, cFactor2div3, cFactor2div3, normalVector);
    349 
    350     // Top/Right pixel.
     359    setPixel(offset, data, paintingData, 0, 0, cFactor2div3, cFactor2div3, data.topLeftNormal(offset));
     360
     361    // Top right.
    351362    offset = data.widthMultipliedByPixelSize - cPixelSize;
    352     data.topRight(offset, normalVector);
    353     setPixel(offset, data, paintingData, data.widthDecreasedByOne, 0, cFactor2div3, cFactor2div3, normalVector);
    354 
    355     // Bottom/Left pixel.
     363    setPixel(offset, data, paintingData, data.widthDecreasedByOne, 0, cFactor2div3, cFactor2div3, data.topRightNormal(offset));
     364
     365    // Bottom left.
    356366    offset = data.heightDecreasedByOne * data.widthMultipliedByPixelSize;
    357     data.bottomLeft(offset, normalVector);
    358     setPixel(offset, data, paintingData, 0, data.heightDecreasedByOne, cFactor2div3, cFactor2div3, normalVector);
    359 
    360     // Bottom/Right pixel.
     367    setPixel(offset, data, paintingData, 0, data.heightDecreasedByOne, cFactor2div3, cFactor2div3, data.bottomLeftNormal(offset));
     368
     369    // Bottom right.
    361370    offset = height * data.widthMultipliedByPixelSize - cPixelSize;
    362     data.bottomRight(offset, normalVector);
    363     setPixel(offset, data, paintingData, data.widthDecreasedByOne, data.heightDecreasedByOne, cFactor2div3, cFactor2div3, normalVector);
     371    setPixel(offset, data, paintingData, data.widthDecreasedByOne, data.heightDecreasedByOne, cFactor2div3, cFactor2div3, data.bottomRightNormal(offset));
    364372
    365373    if (width >= 3) {
    366374        // Top row.
    367375        offset = cPixelSize;
    368         for (int x = 1; x < data.widthDecreasedByOne; ++x, offset += cPixelSize) {
    369             data.topRow(offset, normalVector);
    370             inlineSetPixel(offset, data, paintingData, x, 0, cFactor1div3, cFactor1div2, normalVector);
    371         }
     376        for (int x = 1; x < data.widthDecreasedByOne; ++x, offset += cPixelSize)
     377            inlineSetPixel(offset, data, paintingData, x, 0, cFactor1div3, cFactor1div2, data.topRowNormal(offset));
     378
    372379        // Bottom row.
    373380        offset = data.heightDecreasedByOne * data.widthMultipliedByPixelSize + cPixelSize;
    374         for (int x = 1; x < data.widthDecreasedByOne; ++x, offset += cPixelSize) {
    375             data.bottomRow(offset, normalVector);
    376             inlineSetPixel(offset, data, paintingData, x, data.heightDecreasedByOne, cFactor1div3, cFactor1div2, normalVector);
    377         }
     381        for (int x = 1; x < data.widthDecreasedByOne; ++x, offset += cPixelSize)
     382            inlineSetPixel(offset, data, paintingData, x, data.heightDecreasedByOne, cFactor1div3, cFactor1div2, data.bottomRowNormal(offset));
    378383    }
    379384
     
    381386        // Left column.
    382387        offset = data.widthMultipliedByPixelSize;
    383         for (int y = 1; y < data.heightDecreasedByOne; ++y, offset += data.widthMultipliedByPixelSize) {
    384             data.leftColumn(offset, normalVector);
    385             inlineSetPixel(offset, data, paintingData, 0, y, cFactor1div2, cFactor1div3, normalVector);
    386         }
     388        for (int y = 1; y < data.heightDecreasedByOne; ++y, offset += data.widthMultipliedByPixelSize)
     389            inlineSetPixel(offset, data, paintingData, 0, y, cFactor1div2, cFactor1div3, data.leftColumnNormal(offset));
     390
    387391        // Right column.
    388         offset = (data.widthMultipliedByPixelSize << 1) - cPixelSize;
    389         for (int y = 1; y < data.heightDecreasedByOne; ++y, offset += data.widthMultipliedByPixelSize) {
    390             data.rightColumn(offset, normalVector);
    391             inlineSetPixel(offset, data, paintingData, data.widthDecreasedByOne, y, cFactor1div2, cFactor1div3, normalVector);
    392         }
     392        offset = 2 * data.widthMultipliedByPixelSize - cPixelSize;
     393        for (int y = 1; y < data.heightDecreasedByOne; ++y, offset += data.widthMultipliedByPixelSize)
     394            inlineSetPixel(offset, data, paintingData, data.widthDecreasedByOne, y, cFactor1div2, cFactor1div3, data.rightColumnNormal(offset));
    393395    }
    394396
  • trunk/Source/WebCore/platform/graphics/filters/FELighting.h

    r225026 r225088  
    7373        int heightDecreasedByOne;
    7474
    75         inline void topLeft(int offset, IntPoint& normalVector);
    76         inline void topRow(int offset, IntPoint& normalVector);
    77         inline void topRight(int offset, IntPoint& normalVector);
    78         inline void leftColumn(int offset, IntPoint& normalVector);
    79         inline void interior(int offset, IntPoint& normalVector);
    80         inline void rightColumn(int offset, IntPoint& normalVector);
    81         inline void bottomLeft(int offset, IntPoint& normalVector);
    82         inline void bottomRow(int offset, IntPoint& normalVector);
    83         inline void bottomRight(int offset, IntPoint& normalVector);
     75        inline IntSize topLeftNormal(int offset) const;
     76        inline IntSize topRowNormal(int offset) const;
     77        inline IntSize topRightNormal(int offset) const;
     78        inline IntSize leftColumnNormal(int offset) const;
     79        inline IntSize interiorNormal(int offset) const;
     80        inline IntSize rightColumnNormal(int offset) const;
     81        inline IntSize bottomLeftNormal(int offset) const;
     82        inline IntSize bottomRowNormal(int offset) const;
     83        inline IntSize bottomRightNormal(int offset) const;
    8484    };
    8585
     
    102102    bool drawLighting(Uint8ClampedArray*, int, int);
    103103    inline void inlineSetPixel(int offset, LightingData&, LightSource::PaintingData&,
    104                                int lightX, int lightY, float factorX, float factorY, IntPoint& normalVector);
     104        int lightX, int lightY, float factorX, float factorY, IntSize normalVector);
    105105
    106106    // Not worth to inline every occurence of setPixel.
    107107    void setPixel(int offset, LightingData&, LightSource::PaintingData&,
    108                   int lightX, int lightY, float factorX, float factorY, IntPoint& normalVector);
     108        int lightX, int lightY, float factorX, float factorY, IntSize normalVector);
    109109
    110110    void platformApplySoftware() override;
     
    115115    inline void platformApplyGeneric(LightingData&, LightSource::PaintingData&);
    116116
     117#if CPU(ARM_NEON) && CPU(ARM_TRADITIONAL) && COMPILER(GCC_OR_CLANG)
    117118    static int getPowerCoefficients(float exponent);
    118119    inline void platformApplyNeon(LightingData&, LightSource::PaintingData&);
     120#endif
    119121
    120122    LightingType m_lightingType;
Note: See TracChangeset for help on using the changeset viewer.