| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 Copyright (C) 2012 Google Inc. All rights reserved. | |
| 3 | |
| 4 Redistribution and use in source and binary forms, with or without | |
| 5 modification, are permitted provided that the following conditions | |
| 6 are met: | |
| 7 1. Redistributions of source code must retain the above copyright | |
| 8 notice, this list of conditions and the following disclaimer. | |
| 9 2. Redistributions in binary form must reproduce the above copyright | |
| 10 notice, this list of conditions and the following disclaimer in the | |
| 11 documentation and/or other materials provided with the distribution. | |
| 12 | |
| 13 THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY | |
| 14 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 16 DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
| 20 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 23 */ | |
| 24 | |
| 25 #include "sky/engine/platform/graphics/Canvas2DLayerManager.h" | |
| 26 | |
| 27 #include "sky/engine/public/platform/Platform.h" | |
| 28 #include "sky/engine/wtf/StdLibExtras.h" | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 enum { | |
| 33 DefaultMaxBytesAllocated = 64*1024*1024, | |
| 34 DefaultTargetBytesAllocated = 16*1024*1024, | |
| 35 }; | |
| 36 | |
| 37 } // unnamed namespace | |
| 38 | |
| 39 namespace blink { | |
| 40 | |
| 41 Canvas2DLayerManager::Canvas2DLayerManager() | |
| 42 : m_bytesAllocated(0) | |
| 43 , m_maxBytesAllocated(DefaultMaxBytesAllocated) | |
| 44 , m_targetBytesAllocated(DefaultTargetBytesAllocated) | |
| 45 , m_limitPendingFramesTimer(this, &Canvas2DLayerManager::limitPendingFramesT
imerFired) | |
| 46 { | |
| 47 } | |
| 48 | |
| 49 Canvas2DLayerManager::~Canvas2DLayerManager() | |
| 50 { | |
| 51 ASSERT(!m_bytesAllocated); | |
| 52 ASSERT(!m_layerList.head()); | |
| 53 } | |
| 54 | |
| 55 void Canvas2DLayerManager::init(size_t maxBytesAllocated, size_t targetBytesAllo
cated) | |
| 56 { | |
| 57 ASSERT(maxBytesAllocated >= targetBytesAllocated); | |
| 58 m_maxBytesAllocated = maxBytesAllocated; | |
| 59 m_targetBytesAllocated = targetBytesAllocated; | |
| 60 if (m_limitPendingFramesTimer.isActive()) | |
| 61 m_limitPendingFramesTimer.stop(); | |
| 62 } | |
| 63 | |
| 64 Canvas2DLayerManager& Canvas2DLayerManager::get() | |
| 65 { | |
| 66 DEFINE_STATIC_LOCAL(Canvas2DLayerManager, manager, ()); | |
| 67 return manager; | |
| 68 } | |
| 69 | |
| 70 void Canvas2DLayerManager::limitPendingFramesTimerFired(Timer<Canvas2DLayerManag
er>*) | |
| 71 { | |
| 72 Canvas2DLayerBridge* layer = m_layerList.head(); | |
| 73 while (layer) { | |
| 74 Canvas2DLayerBridge* currentLayer = layer; | |
| 75 // must increment iterator before calling limitPendingFrames, which | |
| 76 // may result in the layer being removed from the list. | |
| 77 layer = layer->next(); | |
| 78 currentLayer->limitPendingFrames(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void Canvas2DLayerManager::layerDidDraw(Canvas2DLayerBridge* layer) | |
| 83 { | |
| 84 if (isInList(layer)) { | |
| 85 if (layer != m_layerList.head()) { | |
| 86 m_layerList.remove(layer); | |
| 87 m_layerList.push(layer); // Set as MRU | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 if (!m_limitPendingFramesTimer.isActive()) | |
| 92 m_limitPendingFramesTimer.startOneShot(0, FROM_HERE); | |
| 93 } | |
| 94 | |
| 95 void Canvas2DLayerManager::layerTransientResourceAllocationChanged(Canvas2DLayer
Bridge* layer, intptr_t deltaBytes) | |
| 96 { | |
| 97 ASSERT((intptr_t)m_bytesAllocated + deltaBytes >= 0); | |
| 98 m_bytesAllocated = (intptr_t)m_bytesAllocated + deltaBytes; | |
| 99 if (!isInList(layer) && layer->hasTransientResources()) { | |
| 100 m_layerList.push(layer); | |
| 101 } else if (isInList(layer) && !layer->hasTransientResources()) { | |
| 102 m_layerList.remove(layer); | |
| 103 layer->setNext(0); | |
| 104 layer->setPrev(0); | |
| 105 } | |
| 106 | |
| 107 if (deltaBytes > 0) | |
| 108 freeMemoryIfNecessary(); | |
| 109 } | |
| 110 | |
| 111 void Canvas2DLayerManager::freeMemoryIfNecessary() | |
| 112 { | |
| 113 if (m_bytesAllocated >= m_maxBytesAllocated) { | |
| 114 // Pass 1: Free memory from caches | |
| 115 Canvas2DLayerBridge* layer = m_layerList.tail(); // LRU | |
| 116 while (layer && m_bytesAllocated > m_targetBytesAllocated) { | |
| 117 Canvas2DLayerBridge* currentLayer = layer; | |
| 118 layer = layer->prev(); | |
| 119 currentLayer->freeMemoryIfPossible(m_bytesAllocated - m_targetBytesA
llocated); | |
| 120 ASSERT(isInList(currentLayer) == currentLayer->hasTransientResources
()); | |
| 121 } | |
| 122 | |
| 123 // Pass 2: Flush canvases | |
| 124 layer = m_layerList.tail(); | |
| 125 while (m_bytesAllocated > m_targetBytesAllocated && layer) { | |
| 126 Canvas2DLayerBridge* currentLayer = layer; | |
| 127 layer = layer->prev(); | |
| 128 currentLayer->flush(); | |
| 129 currentLayer->freeMemoryIfPossible(m_bytesAllocated - m_targetBytesA
llocated); | |
| 130 ASSERT(isInList(currentLayer) == currentLayer->hasTransientResources
()); | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 bool Canvas2DLayerManager::isInList(Canvas2DLayerBridge* layer) const | |
| 136 { | |
| 137 return layer->prev() || m_layerList.head() == layer; | |
| 138 } | |
| 139 | |
| 140 } // namespace blink | |
| 141 | |
| OLD | NEW |