| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "platform/graphics/paint/DisplayItems.h" | 6 #include "platform/graphics/paint/DisplayItems.h" |
| 7 | 7 |
| 8 #include <algorithm> |
| 9 |
| 8 namespace blink { | 10 namespace blink { |
| 9 | 11 |
| 12 static const size_t capacityOfFirstInnerVector = 32; |
| 13 |
| 10 DisplayItems::DisplayItems() | 14 DisplayItems::DisplayItems() |
| 11 { | 15 { |
| 12 } | 16 } |
| 13 | 17 |
| 14 DisplayItems::~DisplayItems() | 18 DisplayItems::~DisplayItems() |
| 15 { | 19 { |
| 16 } | 20 } |
| 17 | 21 |
| 18 void DisplayItems::append(PassOwnPtr<DisplayItem> displayItem) | |
| 19 { | |
| 20 m_items.append(displayItem); | |
| 21 } | |
| 22 | |
| 23 void DisplayItems::appendByMoving(const Iterator& it) | 22 void DisplayItems::appendByMoving(const Iterator& it) |
| 24 { | 23 { |
| 25 // Release the underlying OwnPtr to move the display item ownership. | |
| 26 ASSERT(!it->isGone()); | 24 ASSERT(!it->isGone()); |
| 27 append(it.m_iterator->release()); | 25 |
| 26 DisplayItem& oldItem = *it->m_ptr; |
| 27 oldItem.appendByMoving(*this); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void DisplayItems::removeLast() | 30 void DisplayItems::removeLast() |
| 31 { | 31 { |
| 32 ASSERT(!isEmpty()); |
| 33 |
| 34 // Find the last non-empty buffer. This is where the last element is. |
| 35 auto isNonEmpty = [](const OwnPtr<InnerVector>& innerVector) -> bool { |
| 36 return !innerVector->isEmpty(); |
| 37 }; |
| 38 auto it = std::find_if(m_buffers.rbegin(), m_buffers.rend(), isNonEmpty); |
| 39 ASSERT(it != m_buffers.rend()); |
| 40 |
| 41 // This is the vector which contains the last element. |
| 42 // We can now remove the element. |
| 43 // (The pointer in m_items may be null to indicate "gone".) |
| 44 InnerVector& innerVector = **it; |
| 45 RELEASE_ASSERT(innerVector.last().asDisplayItem() == m_items.last() || !m_it
ems.last()); |
| 46 innerVector.removeLast(); |
| 32 m_items.removeLast(); | 47 m_items.removeLast(); |
| 48 |
| 49 // If there's an empty buffer after this one, remove it. |
| 50 if (it != m_buffers.rbegin()) |
| 51 m_buffers.removeLast(); |
| 33 } | 52 } |
| 34 | 53 |
| 35 void DisplayItems::clear() | 54 void DisplayItems::clear() |
| 36 { | 55 { |
| 56 m_buffers.clear(); |
| 37 m_items.clear(); | 57 m_items.clear(); |
| 38 } | 58 } |
| 39 | 59 |
| 40 void DisplayItems::swap(DisplayItems& other) | 60 void DisplayItems::swap(DisplayItems& other) |
| 41 { | 61 { |
| 62 m_buffers.swap(other.m_buffers); |
| 42 m_items.swap(other.m_items); | 63 m_items.swap(other.m_items); |
| 43 } | 64 } |
| 44 | 65 |
| 45 void DisplayItems::setGone(const Iterator& it) | 66 void DisplayItems::setGone(const Iterator& it) |
| 46 { | 67 { |
| 47 it.m_iterator->clear(); | 68 *it.m_iterator = nullptr; |
| 69 } |
| 70 |
| 71 DisplayItems::StorageBuffer& DisplayItems::appendStorageBuffer() |
| 72 { |
| 73 // Find the first inner vector with space in it. |
| 74 // We don't want to increase capacity of these vectors. |
| 75 auto hasCapacity = [](const OwnPtr<InnerVector>& innerVector) -> bool { |
| 76 return innerVector->size() != innerVector->capacity(); |
| 77 }; |
| 78 auto it = std::find_if(m_buffers.begin(), m_buffers.end(), hasCapacity); |
| 79 |
| 80 // If there's no space, allocate a new inner vector. |
| 81 if (it == m_buffers.end()) { |
| 82 size_t capacity = m_buffers.isEmpty() ? capacityOfFirstInnerVector : 2 *
m_buffers.last()->capacity(); |
| 83 OwnPtr<InnerVector> innerVector = adoptPtr(new InnerVector); |
| 84 innerVector->reserveInitialCapacity(capacity); |
| 85 m_buffers.append(innerVector.release()); |
| 86 it = m_buffers.end() - 1; |
| 87 } |
| 88 |
| 89 // Now that we have the inner vector, we can create storage buffer for one |
| 90 // display item. |
| 91 InnerVector& innerVector = **it; |
| 92 innerVector.grow(innerVector.size() + 1); |
| 93 return innerVector.last(); |
| 48 } | 94 } |
| 49 | 95 |
| 50 } // namespace blink | 96 } // namespace blink |
| OLD | NEW |