| 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 #ifndef DisplayItemClient_h | 5 #ifndef DisplayItemClient_h |
| 6 #define DisplayItemClient_h | 6 #define DisplayItemClient_h |
| 7 | 7 |
| 8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/geometry/LayoutRect.h" | 9 #include "platform/geometry/LayoutRect.h" |
| 10 #include "platform/graphics/PaintInvalidationReason.h" |
| 10 #include "wtf/Assertions.h" | 11 #include "wtf/Assertions.h" |
| 11 #include "wtf/text/WTFString.h" | 12 #include "wtf/text/WTFString.h" |
| 12 | 13 |
| 13 #define CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS 1 | 14 #define CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS 1 |
| 14 // TODO(wangxianzhu): Restore the following line after we fix crbug.com/609218. | 15 // TODO(wangxianzhu): Restore the following line after we fix crbug.com/609218. |
| 15 // #define CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS DCHECK_IS_ON() | 16 // #define CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS DCHECK_IS_ON() |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 19 // Holds a unique cache generation id of display items and paint controllers. | 20 // The class for objects that can be associated with display items. |
| 20 // | |
| 21 // A paint controller sets its cache generation to DisplayItemCacheGeneration::n
ext() | |
| 22 // at the end of each commitNewDisplayItems, and updates the cache generation of
each | |
| 23 // client with cached drawings by calling DisplayItemClient::setDisplayItemsCach
ed(). | |
| 24 // A display item is treated as validly cached in a paint controller if its cach
e generation | |
| 25 // matches the paint controller's cache generation. | |
| 26 // | |
| 27 // SPv1 only: If a display item is painted on multiple paint controllers, becaus
e cache | |
| 28 // generations are unique, the client's cache generation matches the last paint
controller | |
| 29 // only. The client will be treated as invalid on other paint controllers regard
less if | |
| 30 // it's validly cached by these paint controllers. The situation is very rare (a
bout 0.07% | |
| 31 // clients were painted on multiple paint controllers) so the performance penalt
y is trivial. | |
| 32 class PLATFORM_EXPORT DisplayItemCacheGeneration { | |
| 33 DISALLOW_NEW(); | |
| 34 public: | |
| 35 DisplayItemCacheGeneration() : m_value(kInvalidGeneration) { } | |
| 36 | |
| 37 void invalidate() { m_value = kInvalidGeneration; } | |
| 38 static DisplayItemCacheGeneration next() { return DisplayItemCacheGeneration
(s_nextGeneration++); } | |
| 39 bool matches(const DisplayItemCacheGeneration& other) | |
| 40 { | |
| 41 return m_value != kInvalidGeneration && other.m_value != kInvalidGenerat
ion && m_value == other.m_value; | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 typedef uint32_t Generation; | |
| 46 DisplayItemCacheGeneration(Generation value) : m_value(value) { } | |
| 47 | |
| 48 static const Generation kInvalidGeneration = 0; | |
| 49 static Generation s_nextGeneration; | |
| 50 Generation m_value; | |
| 51 }; | |
| 52 | |
| 53 // The interface for objects that can be associated with display items. | |
| 54 // A DisplayItemClient object should live at least longer than the document cycl
e | 21 // A DisplayItemClient object should live at least longer than the document cycl
e |
| 55 // in which its display items are created during painting. | 22 // in which its display items are created during painting. |
| 56 // After the document cycle, a pointer/reference to DisplayItemClient should be | 23 // After the document cycle, a pointer/reference to DisplayItemClient should be |
| 57 // no longer dereferenced unless we can make sure the client is still valid. | 24 // no longer dereferenced unless we can make sure the client is still valid. |
| 58 class PLATFORM_EXPORT DisplayItemClient { | 25 class PLATFORM_EXPORT DisplayItemClient { |
| 59 public: | 26 public: |
| 60 #if CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS | 27 #if CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS |
| 61 DisplayItemClient(); | 28 DisplayItemClient(); |
| 62 virtual ~DisplayItemClient(); | 29 virtual ~DisplayItemClient(); |
| 63 | 30 |
| 64 // Tests if this DisplayItemClient object has been created and has not been
deleted yet. | 31 // Tests if this DisplayItemClient object has been created and has not been
deleted yet. |
| 65 bool isAlive() const; | 32 bool isAlive() const; |
| 66 // Called when any DisplayItem of this DisplayItemClient is added into Paint
Controller | 33 // Called when any DisplayItem of this DisplayItemClient is added into Paint
Controller |
| 67 // using PaintController::createAndAppend() or into a cached subsequence. | 34 // using PaintController::createAndAppend() or into a cached subsequence. |
| 68 void beginShouldKeepAlive(const void* owner) const; | 35 void beginShouldKeepAlive(const void* owner) const; |
| 69 // Clears all should-keep-alive DisplayItemClients of a PaintController. Cal
led after | 36 // Clears all should-keep-alive DisplayItemClients of a PaintController. Cal
led after |
| 70 // PaintController commits new display items or the subsequence owner is inv
alidated. | 37 // PaintController commits new display items or the subsequence owner is inv
alidated. |
| 71 static void endShouldKeepAliveAllClients(const void* owner); | 38 static void endShouldKeepAliveAllClients(const void* owner); |
| 72 static void endShouldKeepAliveAllClients(); | 39 static void endShouldKeepAliveAllClients(); |
| 73 | |
| 74 // Called to clear should-keep-alive of DisplayItemClients in a subsequence
if this | |
| 75 // object is a subsequence. | |
| 76 #define ON_DISPLAY_ITEM_CLIENT_INVALIDATION() endShouldKeepAliveAllClients(this) | |
| 77 #else | |
| 78 virtual ~DisplayItemClient() { } | |
| 79 #define ON_DISPLAY_ITEM_CLIENT_INVALIDATION() | |
| 80 #endif | 40 #endif |
| 81 | 41 |
| 82 virtual String debugName() const = 0; | 42 virtual String debugName() const = 0; |
| 83 | 43 |
| 84 // The visual rect of this DisplayItemClient, in object space of the object
that owns the GraphicsLayer, i.e. | 44 // The visual rect of this DisplayItemClient, in object space of the object
that owns the GraphicsLayer, i.e. |
| 85 // offset by offsetFromLayoutObjectWithSubpixelAccumulation(). | 45 // offset by offsetFromLayoutObjectWithSubpixelAccumulation(). |
| 86 virtual LayoutRect visualRect() const = 0; | 46 virtual LayoutRect visualRect() const = 0; |
| 87 | 47 |
| 88 virtual bool displayItemsAreCached(DisplayItemCacheGeneration) const = 0; | 48 void setDisplayItemsUncached(PaintInvalidationReason reason = PaintInvalidat
ionFull) const |
| 89 virtual void setDisplayItemsCached(DisplayItemCacheGeneration) const = 0; | 49 { |
| 90 virtual void setDisplayItemsUncached() const = 0; | 50 m_cacheGenerationOrInvalidationReason.invalidate(reason); |
| 51 #if CHECK_DISPLAY_ITEM_CLIENT_ALIVENESS |
| 52 // Clear should-keep-alive of DisplayItemClients in a subsequence if thi
s |
| 53 // object is a subsequence. |
| 54 endShouldKeepAliveAllClients(this); |
| 55 #endif |
| 56 } |
| 57 |
| 58 PaintInvalidationReason getPaintInvalidationReason() const { return m_cacheG
enerationOrInvalidationReason.getPaintInvalidationReason(); } |
| 59 |
| 60 private: |
| 61 friend class PaintController; |
| 62 |
| 63 // Holds a unique cache generation id of DisplayItemClients and PaintControl
lers, |
| 64 // or PaintInvalidationReason if the DisplayItemClient or PaintController is |
| 65 // invalidated. |
| 66 // |
| 67 // A paint controller sets its cache generation to DisplayItemCacheGeneratio
n::next() |
| 68 // at the end of each commitNewDisplayItems, and updates the cache generatio
n of each |
| 69 // client with cached drawings by calling DisplayItemClient::setDisplayItems
Cached(). |
| 70 // A display item is treated as validly cached in a paint controller if its
cache generation |
| 71 // matches the paint controller's cache generation. |
| 72 // |
| 73 // SPv1 only: If a display item is painted on multiple paint controllers, be
cause cache |
| 74 // generations are unique, the client's cache generation matches the last pa
int controller |
| 75 // only. The client will be treated as invalid on other paint controllers re
gardless if |
| 76 // it's validly cached by these paint controllers. The situation is very rar
e (about 0.07% |
| 77 // clients were painted on multiple paint controllers) so the performance pe
nalty is trivial. |
| 78 class CacheGenerationOrInvalidationReason { |
| 79 DISALLOW_NEW(); |
| 80 public: |
| 81 CacheGenerationOrInvalidationReason() { invalidate(); } |
| 82 |
| 83 void invalidate(PaintInvalidationReason reason = PaintInvalidationFull)
{ m_value = static_cast<ValueType>(reason); } |
| 84 |
| 85 static CacheGenerationOrInvalidationReason next() |
| 86 { |
| 87 // In case the value overflowed in the previous call. |
| 88 if (s_nextGeneration < kFirstValidGeneration) |
| 89 s_nextGeneration = kFirstValidGeneration; |
| 90 return CacheGenerationOrInvalidationReason(s_nextGeneration++); |
| 91 } |
| 92 |
| 93 bool matches(const CacheGenerationOrInvalidationReason& other) const |
| 94 { |
| 95 return m_value >= kFirstValidGeneration && other.m_value >= kFirstVa
lidGeneration && m_value == other.m_value; |
| 96 } |
| 97 |
| 98 PaintInvalidationReason getPaintInvalidationReason() const |
| 99 { |
| 100 return m_value < kFirstValidGeneration ? static_cast<PaintInvalidati
onReason>(m_value) : PaintInvalidationNone; |
| 101 } |
| 102 |
| 103 private: |
| 104 typedef uint32_t ValueType; |
| 105 explicit CacheGenerationOrInvalidationReason(ValueType value) : m_value(
value) { } |
| 106 |
| 107 static const ValueType kFirstValidGeneration = static_cast<ValueType>(Pa
intInvalidationReasonMax) + 1; |
| 108 static ValueType s_nextGeneration; |
| 109 ValueType m_value; |
| 110 }; |
| 111 |
| 112 bool displayItemsAreCached(CacheGenerationOrInvalidationReason other) const
{ return m_cacheGenerationOrInvalidationReason.matches(other); } |
| 113 void setDisplayItemsCached(CacheGenerationOrInvalidationReason cacheGenerati
on) const { m_cacheGenerationOrInvalidationReason = cacheGeneration; } |
| 114 |
| 115 mutable CacheGenerationOrInvalidationReason m_cacheGenerationOrInvalidationR
eason; |
| 91 }; | 116 }; |
| 92 | 117 |
| 93 #define DISPLAY_ITEM_CACHE_STATUS_IMPLEMENTATION \ | |
| 94 bool displayItemsAreCached(DisplayItemCacheGeneration cacheGeneration) const
final { return m_cacheGeneration.matches(cacheGeneration); } \ | |
| 95 void setDisplayItemsCached(DisplayItemCacheGeneration cacheGeneration) const
final { m_cacheGeneration = cacheGeneration; } \ | |
| 96 void setDisplayItemsUncached() const final \ | |
| 97 { \ | |
| 98 m_cacheGeneration.invalidate(); \ | |
| 99 ON_DISPLAY_ITEM_CLIENT_INVALIDATION(); \ | |
| 100 } \ | |
| 101 mutable DisplayItemCacheGeneration m_cacheGeneration; | |
| 102 | |
| 103 #define DISPLAY_ITEM_CACHE_STATUS_UNCACHEABLE_IMPLEMENTATION \ | |
| 104 bool displayItemsAreCached(DisplayItemCacheGeneration) const final { return
false; } \ | |
| 105 void setDisplayItemsCached(DisplayItemCacheGeneration) const final { } \ | |
| 106 void setDisplayItemsUncached() const final { } | |
| 107 | |
| 108 inline bool operator==(const DisplayItemClient& client1, const DisplayItemClient
& client2) { return &client1 == &client2; } | 118 inline bool operator==(const DisplayItemClient& client1, const DisplayItemClient
& client2) { return &client1 == &client2; } |
| 109 inline bool operator!=(const DisplayItemClient& client1, const DisplayItemClient
& client2) { return &client1 != &client2; } | 119 inline bool operator!=(const DisplayItemClient& client1, const DisplayItemClient
& client2) { return &client1 != &client2; } |
| 110 | 120 |
| 111 } // namespace blink | 121 } // namespace blink |
| 112 | 122 |
| 113 #endif // DisplayItemClient_h | 123 #endif // DisplayItemClient_h |
| OLD | NEW |