2 Copyright (C) 2012 Google Inc. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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.
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.
26 #include "platform/graphics/Canvas2DLayerManager.h"
28 #include "public/platform/Platform.h"
29 #include "wtf/StdLibExtras.h"
31 using blink::WebThread;
36 DefaultMaxBytesAllocated = 64*1024*1024,
37 DefaultTargetBytesAllocated = 16*1024*1024,
40 } // unnamed namespace
44 Canvas2DLayerManager::Canvas2DLayerManager()
46 , m_maxBytesAllocated(DefaultMaxBytesAllocated)
47 , m_targetBytesAllocated(DefaultTargetBytesAllocated)
48 , m_taskObserverActive(false)
52 Canvas2DLayerManager::~Canvas2DLayerManager()
54 ASSERT(!m_bytesAllocated);
55 ASSERT(!m_layerList.head());
56 ASSERT(!m_taskObserverActive);
59 void Canvas2DLayerManager::init(size_t maxBytesAllocated, size_t targetBytesAllocated)
61 ASSERT(maxBytesAllocated >= targetBytesAllocated);
62 m_maxBytesAllocated = maxBytesAllocated;
63 m_targetBytesAllocated = targetBytesAllocated;
64 if (m_taskObserverActive) {
65 blink::Platform::current()->currentThread()->removeTaskObserver(this);
66 m_taskObserverActive = false;
70 Canvas2DLayerManager& Canvas2DLayerManager::get()
72 DEFINE_STATIC_LOCAL(Canvas2DLayerManager, manager, ());
76 void Canvas2DLayerManager::willProcessTask()
80 void Canvas2DLayerManager::didProcessTask()
82 // Called after the script action for the current frame has been processed.
83 ASSERT(m_taskObserverActive);
84 blink::Platform::current()->currentThread()->removeTaskObserver(this);
85 m_taskObserverActive = false;
86 Canvas2DLayerBridge* layer = m_layerList.head();
88 Canvas2DLayerBridge* currentLayer = layer;
89 // must increment iterator before calling limitPendingFrames, which
90 // may result in the layer being removed from the list.
91 layer = layer->next();
92 currentLayer->limitPendingFrames();
96 void Canvas2DLayerManager::layerDidDraw(Canvas2DLayerBridge* layer)
98 if (isInList(layer)) {
99 if (layer != m_layerList.head()) {
100 m_layerList.remove(layer);
101 m_layerList.push(layer); // Set as MRU
105 if (!m_taskObserverActive) {
106 m_taskObserverActive = true;
107 // Schedule a call to didProcessTask() after completion of the current script task.
108 blink::Platform::current()->currentThread()->addTaskObserver(this);
112 void Canvas2DLayerManager::layerTransientResourceAllocationChanged(Canvas2DLayerBridge* layer, intptr_t deltaBytes)
114 ASSERT((intptr_t)m_bytesAllocated + deltaBytes >= 0);
115 m_bytesAllocated = (intptr_t)m_bytesAllocated + deltaBytes;
116 if (!isInList(layer) && layer->hasTransientResources()) {
117 m_layerList.push(layer);
118 } else if (isInList(layer) && !layer->hasTransientResources()) {
119 m_layerList.remove(layer);
125 freeMemoryIfNecessary();
128 void Canvas2DLayerManager::freeMemoryIfNecessary()
130 if (m_bytesAllocated >= m_maxBytesAllocated) {
131 // Pass 1: Free memory from caches
132 Canvas2DLayerBridge* layer = m_layerList.tail(); // LRU
133 while (layer && m_bytesAllocated > m_targetBytesAllocated) {
134 Canvas2DLayerBridge* currentLayer = layer;
135 layer = layer->prev();
136 currentLayer->freeMemoryIfPossible(m_bytesAllocated - m_targetBytesAllocated);
137 ASSERT(isInList(currentLayer) == currentLayer->hasTransientResources());
140 // Pass 2: Flush canvases
141 layer = m_layerList.tail();
142 while (m_bytesAllocated > m_targetBytesAllocated && layer) {
143 Canvas2DLayerBridge* currentLayer = layer;
144 layer = layer->prev();
145 currentLayer->flush();
146 currentLayer->freeMemoryIfPossible(m_bytesAllocated - m_targetBytesAllocated);
147 ASSERT(isInList(currentLayer) == currentLayer->hasTransientResources());
152 void Canvas2DLayerManager::removeLayerFromList(Canvas2DLayerBridge* layer)
154 ASSERT(isInList(layer));
155 ASSERT(!layer->hasTransientResources());
159 bool Canvas2DLayerManager::isInList(Canvas2DLayerBridge* layer) const
161 return layer->prev() || m_layerList.head() == layer;