Changeset 199893 in webkit


Ignore:
Timestamp:
Apr 22, 2016 12:25:40 PM (8 years ago)
Author:
Antti Koivisto
Message:

TextAutoSizingKey should use normal refcounting
https://bugs.webkit.org/show_bug.cgi?id=156893

Reviewed by Andreas Kling.

Get rid of special refcounting of style in favor of RefPtr. It also becomes a move-only type
to support future switch to non-refcounted RenderStyle.

Also general cleanups and modernization.

  • dom/Document.cpp:

(WebCore::TextAutoSizingTraits::constructDeletedValue):
(WebCore::TextAutoSizingTraits::isDeletedValue):
(WebCore::Document::addAutoSizingNode):
(WebCore::Document::validateAutoSizingNodes):
(WebCore::Document::resetAutoSizingNodes):

Adopt to being move-only.

  • rendering/TextAutoSizing.cpp:

(WebCore::cloneRenderStyleWithState):
(WebCore::TextAutoSizingKey::TextAutoSizingKey):

Clone the style for safety against mutations. Cloning is cheap.

(WebCore::TextAutoSizingValue::numNodes):
(WebCore::TextAutoSizingValue::adjustNodeSizes):
(WebCore::TextAutoSizingValue::reset):
(WebCore::TextAutoSizingKey::~TextAutoSizingKey): Deleted.
(WebCore::TextAutoSizingKey::operator=): Deleted.
(WebCore::TextAutoSizingKey::ref): Deleted.
(WebCore::TextAutoSizingKey::deref): Deleted.

  • rendering/TextAutoSizing.h:

(WebCore::TextAutoSizingKey::TextAutoSizingKey):
(WebCore::TextAutoSizingKey::style):
(WebCore::TextAutoSizingKey::isDeleted):
(WebCore::operator==):
(WebCore::TextAutoSizingKey::doc): Deleted.
(WebCore::TextAutoSizingKey::isValidDoc): Deleted.
(WebCore::TextAutoSizingKey::isValidStyle): Deleted.
(WebCore::TextAutoSizingKey::deletedKeyDoc): Deleted.
(WebCore::TextAutoSizingKey::deletedKeyStyle): Deleted.

m_doc member is not used for anything except deleted value comparisons. Replace it with a bit.

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r199890 r199893  
     12016-04-22  Antti Koivisto  <antti@apple.com>
     2
     3        TextAutoSizingKey should use normal refcounting
     4        https://bugs.webkit.org/show_bug.cgi?id=156893
     5
     6        Reviewed by Andreas Kling.
     7
     8        Get rid of special refcounting of style in favor of RefPtr. It also becomes a move-only type
     9        to support future switch to non-refcounted RenderStyle.
     10
     11        Also general cleanups and modernization.
     12
     13        * dom/Document.cpp:
     14        (WebCore::TextAutoSizingTraits::constructDeletedValue):
     15        (WebCore::TextAutoSizingTraits::isDeletedValue):
     16        (WebCore::Document::addAutoSizingNode):
     17        (WebCore::Document::validateAutoSizingNodes):
     18        (WebCore::Document::resetAutoSizingNodes):
     19
     20            Adopt to being move-only.
     21
     22        * rendering/TextAutoSizing.cpp:
     23        (WebCore::cloneRenderStyleWithState):
     24        (WebCore::TextAutoSizingKey::TextAutoSizingKey):
     25
     26            Clone the style for safety against mutations. Cloning is cheap.
     27
     28        (WebCore::TextAutoSizingValue::numNodes):
     29        (WebCore::TextAutoSizingValue::adjustNodeSizes):
     30        (WebCore::TextAutoSizingValue::reset):
     31        (WebCore::TextAutoSizingKey::~TextAutoSizingKey): Deleted.
     32        (WebCore::TextAutoSizingKey::operator=): Deleted.
     33        (WebCore::TextAutoSizingKey::ref): Deleted.
     34        (WebCore::TextAutoSizingKey::deref): Deleted.
     35        * rendering/TextAutoSizing.h:
     36        (WebCore::TextAutoSizingKey::TextAutoSizingKey):
     37        (WebCore::TextAutoSizingKey::style):
     38        (WebCore::TextAutoSizingKey::isDeleted):
     39        (WebCore::operator==):
     40        (WebCore::TextAutoSizingKey::doc): Deleted.
     41        (WebCore::TextAutoSizingKey::isValidDoc): Deleted.
     42        (WebCore::TextAutoSizingKey::isValidStyle): Deleted.
     43        (WebCore::TextAutoSizingKey::deletedKeyDoc): Deleted.
     44        (WebCore::TextAutoSizingKey::deletedKeyStyle): Deleted.
     45
     46            m_doc member is not used for anything except deleted value comparisons. Replace it with a bit.
     47
    1482016-04-22  Chris Dumez  <cdumez@apple.com>
    249
  • trunk/Source/WebCore/dom/Document.cpp

    r199882 r199893  
    429429void TextAutoSizingTraits::constructDeletedValue(TextAutoSizingKey& slot)
    430430{
    431     new (&slot) TextAutoSizingKey(TextAutoSizingKey::deletedKeyStyle(), TextAutoSizingKey::deletedKeyDoc());
     431    new (&slot) TextAutoSizingKey(TextAutoSizingKey::Deleted);
    432432}
    433433
    434434bool TextAutoSizingTraits::isDeletedValue(const TextAutoSizingKey& value)
    435435{
    436     return value.style() == TextAutoSizingKey::deletedKeyStyle() && value.doc() == TextAutoSizingKey::deletedKeyDoc();
     436    return value.isDeleted();
    437437}
    438438#endif
     
    52925292void Document::addAutoSizingNode(Node* node, float candidateSize)
    52935293{
    5294     TextAutoSizingKey key(&node->renderer()->style(), &document());
    5295     TextAutoSizingMap::AddResult result = m_textAutoSizedNodes.add(key, nullptr);
    5296     if (result.isNewEntry)
    5297         result.iterator->value = TextAutoSizingValue::create();
    5298     result.iterator->value->addNode(node, candidateSize);
     5294    TextAutoSizingKey key(&node->renderer()->style());
     5295    auto addResult = m_textAutoSizedNodes.ensure(WTFMove(key), [] {
     5296        return TextAutoSizingValue::create();
     5297    });
     5298    addResult.iterator->value->addNode(node, candidateSize);
    52995299}
    53005300
     
    53065306        // Update all the nodes in the collection to reflect the new
    53075307        // candidate size.
    5308         if (!value)
    5309             continue;
    5310 
    53115308        value->adjustNodeSizes();
    5312         if (!value->numNodes())
    5313             nodesForRemoval.append(keyValuePair.key);
    5314     }
    5315     for (auto& key : nodesForRemoval)
    5316         m_textAutoSizedNodes.remove(key);
     5309    }
     5310    m_textAutoSizedNodes.removeIf([] (TextAutoSizingMap::KeyValuePairType& keyAndValue) {
     5311        return !keyAndValue.value->numNodes();
     5312    });
    53175313}
    53185314   
    53195315void Document::resetAutoSizingNodes()
    53205316{
    5321     for (auto& value : m_textAutoSizedNodes.values()) {
    5322         if (value)
    5323             value->reset();
    5324     }
     5317    for (auto& value : m_textAutoSizedNodes.values())
     5318        value->reset();
    53255319    m_textAutoSizedNodes.clear();
    53265320}
  • trunk/Source/WebCore/rendering/TextAutoSizing.cpp

    r189830 r199893  
    3737namespace WebCore {
    3838
    39 static PassRefPtr<RenderStyle> cloneRenderStyleWithState(const RenderStyle& currentStyle)
    40 {
    41     RefPtr<RenderStyle> newStyle = RenderStyle::clone(&currentStyle);
     39static Ref<RenderStyle> cloneRenderStyleWithState(const RenderStyle& currentStyle)
     40{
     41    auto newStyle = RenderStyle::clone(&currentStyle);
    4242    if (currentStyle.lastChildState())
    4343        newStyle->setLastChildState();
    4444    if (currentStyle.firstChildState())
    4545        newStyle->setFirstChildState();
    46     return newStyle.release();
    47 }
    48 
    49 TextAutoSizingKey::TextAutoSizingKey()
    50     : m_style(0)
    51     , m_doc(0)
    52 {
    53 }
    54 
    55 TextAutoSizingKey::TextAutoSizingKey(RenderStyle* style, Document* doc)
    56     : m_style(style)
    57     , m_doc(doc)
    58 {
    59     ref();
    60 }
    61 
    62 TextAutoSizingKey::TextAutoSizingKey(const TextAutoSizingKey& other)
    63     : m_style(other.m_style)
    64     , m_doc(other.m_doc)
    65 {
    66     ref();
    67 }
    68 
    69 TextAutoSizingKey::~TextAutoSizingKey()
    70 {
    71     deref();
    72 }
    73 
    74 TextAutoSizingKey& TextAutoSizingKey::operator=(const TextAutoSizingKey& other)
    75 {
    76     other.ref();
    77     deref();
    78     m_style = other.m_style;
    79     m_doc = other.m_doc;
    80     return *this;
    81 }
    82 
    83 void TextAutoSizingKey::ref() const
    84 {
    85     if (isValidStyle())
    86         m_style->ref();
    87 }
    88 
    89 void TextAutoSizingKey::deref() const
    90 {
    91     if (isValidStyle() && isValidDoc())
    92         m_style->deref();
     46    return newStyle;
     47}
     48
     49TextAutoSizingKey::TextAutoSizingKey(DeletedTag)
     50    : m_isDeleted(true)
     51{
     52}
     53
     54TextAutoSizingKey::TextAutoSizingKey(RenderStyle* style)
     55    : m_style(RenderStyle::clone(style))
     56{
    9357}
    9458
     
    11579    // collection.  Return true indicates we need to do that removal.
    11680    Vector<RefPtr<Node> > nodesForRemoval;
    117     HashSet<RefPtr<Node> >::iterator end = m_autoSizedNodes.end();
    118     for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
    119         RefPtr<Node> autoSizingNode = *i;
     81    for (auto& autoSizingNode : m_autoSizedNodes) {
    12082        RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
    12183        if (!text || !text->style().textSizeAdjust().isAuto() || !text->candidateComputedTextSize()) {
     
    12688    }
    12789   
    128     unsigned count = nodesForRemoval.size();
    129     for (unsigned i = 0; i < count; i++)
    130         m_autoSizedNodes.remove(nodesForRemoval[i]);
     90    for (auto& node : nodesForRemoval)
     91        m_autoSizedNodes.remove(node);
    13192   
    13293    // If we only have one piece of text with the style on the page don't
     
    13798    // Compute average size
    13899    float cumulativeSize = 0;
    139     end = m_autoSizedNodes.end();
    140     for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
    141         RefPtr<Node> autoSizingNode = *i;
     100    for (auto& autoSizingNode : m_autoSizedNodes) {
    142101        RenderText* renderText = static_cast<RenderText*>(autoSizingNode->renderer());
    143102        cumulativeSize += renderText->candidateComputedTextSize();
     
    148107    // Adjust sizes
    149108    bool firstPass = true;
    150     end = m_autoSizedNodes.end();
    151     for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
    152         const RefPtr<Node>& autoSizingNode = *i;
     109    for (auto& autoSizingNode : m_autoSizedNodes) {
    153110        RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
    154111        if (text && text->style().fontDescription().computedSize() != averageSize) {
     
    161118            }
    162119           
    163             RefPtr<RenderStyle> style = cloneRenderStyleWithState(text->style());
     120            auto style = cloneRenderStyleWithState(text->style());
    164121            auto fontDescription = style->fontDescription();
    165122            fontDescription.setComputedSize(averageSize);
    166123            style->setFontDescription(fontDescription);
    167124            style->fontCascade().update(&autoSizingNode->document().fontSelector());
    168             text->parent()->setStyle(style.releaseNonNull());
     125            text->parent()->setStyle(WTFMove(style));
    169126           
    170127            RenderElement* parentRenderer = text->parent();
     
    175132            RenderObject* listMarkerRenderer = parentRenderer->firstChild();
    176133            if (listMarkerRenderer->isListMarker()) {
    177                 RefPtr<RenderStyle> style = cloneRenderStyleWithState(listMarkerRenderer->style());
     134                auto style = cloneRenderStyleWithState(listMarkerRenderer->style());
    178135                style->setFontDescription(fontDescription);
    179136                style->fontCascade().update(&autoSizingNode->document().fontSelector());
    180                 downcast<RenderListMarker>(*listMarkerRenderer).setStyle(style.releaseNonNull());
     137                downcast<RenderListMarker>(*listMarkerRenderer).setStyle(WTFMove(style));
    181138            }
    182139           
     
    193150            int lineHeight = specifiedLineHeight * scaleChange;
    194151            if (!lineHeightLength.isFixed() || lineHeightLength.value() != lineHeight) {
    195                 RefPtr<RenderStyle> newParentStyle = cloneRenderStyleWithState(parentStyle);
     152                auto newParentStyle = cloneRenderStyleWithState(parentStyle);
    196153                newParentStyle->setLineHeight(Length(lineHeight, Fixed));
    197154                newParentStyle->setSpecifiedLineHeight(lineHeightLength);
    198155                newParentStyle->setFontDescription(fontDescription);
    199156                newParentStyle->fontCascade().update(&autoSizingNode->document().fontSelector());
    200                 parentRenderer->setStyle(newParentStyle.releaseNonNull());
     157                parentRenderer->setStyle(WTFMove(newParentStyle));
    201158            }
    202159        }
     
    208165void TextAutoSizingValue::reset()
    209166{
    210     HashSet<RefPtr<Node> >::iterator end = m_autoSizedNodes.end();
    211     for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
    212         const RefPtr<Node>& autoSizingNode = *i;
     167    for (auto& autoSizingNode : m_autoSizedNodes) {
    213168        RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
    214169        if (!text)
     
    219174        if (fontDescription.computedSize() != originalSize) {
    220175            fontDescription.setComputedSize(originalSize);
    221             RefPtr<RenderStyle> style = cloneRenderStyleWithState(text->style());
     176            auto style = cloneRenderStyleWithState(text->style());
    222177            style->setFontDescription(fontDescription);
    223178            style->fontCascade().update(&autoSizingNode->document().fontSelector());
    224             text->parent()->setStyle(style.releaseNonNull());
     179            text->parent()->setStyle(WTFMove(style));
    225180        }
    226181        // Reset the line height of the parent.
     
    235190        Length originalLineHeight = parentStyle.specifiedLineHeight();
    236191        if (originalLineHeight != parentStyle.lineHeight()) {
    237             RefPtr<RenderStyle> newParentStyle = cloneRenderStyleWithState(parentStyle);
     192            auto newParentStyle = cloneRenderStyleWithState(parentStyle);
    238193            newParentStyle->setLineHeight(originalLineHeight);
    239194            newParentStyle->setFontDescription(fontDescription);
    240195            newParentStyle->fontCascade().update(&autoSizingNode->document().fontSelector());
    241             parentRenderer->setStyle(newParentStyle.releaseNonNull());
     196            parentRenderer->setStyle(WTFMove(newParentStyle));
    242197        }
    243198    }
  • trunk/Source/WebCore/rendering/TextAutoSizing.h

    r184147 r199893  
    4141class TextAutoSizingKey {
    4242public:
    43     TextAutoSizingKey();
    44     TextAutoSizingKey(RenderStyle*, Document*);
    45     ~TextAutoSizingKey();
    46     TextAutoSizingKey(const TextAutoSizingKey&);
    47     TextAutoSizingKey& operator=(const TextAutoSizingKey&);
    48     Document* doc() const { return m_doc; }
    49     RenderStyle* style() const { return m_style; }
    50     inline bool isValidDoc() const { return m_doc && m_doc != deletedKeyDoc(); }
    51     inline bool isValidStyle() const { return m_style && m_style != deletedKeyStyle(); }
    52     static Document* deletedKeyDoc() { return reinterpret_cast<Document*>(-1); }
    53     static RenderStyle* deletedKeyStyle() { return reinterpret_cast<RenderStyle*>(-1); }
     43    TextAutoSizingKey() = default;
     44    enum DeletedTag { Deleted };
     45    explicit TextAutoSizingKey(DeletedTag);
     46    explicit TextAutoSizingKey(RenderStyle*);
     47    TextAutoSizingKey(TextAutoSizingKey&&) = default;
     48
     49    TextAutoSizingKey& operator=(TextAutoSizingKey&&) = default;
     50
     51    RenderStyle* style() const { return m_style.get(); }
     52    inline bool isDeleted() const { return m_isDeleted; }
     53
    5454private:
    55     void ref() const;
    56     void deref() const;
    57     RenderStyle* m_style;
    58     Document* m_doc;
     55    RefPtr<RenderStyle> m_style;
     56    bool m_isDeleted { false };
    5957};
    6058
    6159inline bool operator==(const TextAutoSizingKey& a, const TextAutoSizingKey& b)
    6260{
    63     if (a.isValidStyle() && b.isValidStyle())
    64         return a.style()->equalForTextAutosizing(b.style());
    65     return a.style() == b.style();
     61    if (a.isDeleted() || b.isDeleted())
     62        return false;
     63    if (!a.style() || !b.style())
     64        return a.style() == b.style();
     65    return a.style()->equalForTextAutosizing(b.style());
    6666}
    6767
Note: See TracChangeset for help on using the changeset viewer.