Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / Canvas2DLayerManager.cpp
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 #include "config.h"
25
26 #include "platform/graphics/Canvas2DLayerManager.h"
27
28 #include "public/platform/Platform.h"
29 #include "wtf/StdLibExtras.h"
30
31 using blink::WebThread;
32
33 namespace {
34
35 enum {
36     DefaultMaxBytesAllocated = 64*1024*1024,
37     DefaultTargetBytesAllocated = 16*1024*1024,
38 };
39
40 } // unnamed namespace
41
42 namespace WebCore {
43
44 Canvas2DLayerManager::Canvas2DLayerManager()
45     : m_bytesAllocated(0)
46     , m_maxBytesAllocated(DefaultMaxBytesAllocated)
47     , m_targetBytesAllocated(DefaultTargetBytesAllocated)
48     , m_taskObserverActive(false)
49 {
50 }
51
52 Canvas2DLayerManager::~Canvas2DLayerManager()
53 {
54     ASSERT(!m_bytesAllocated);
55     ASSERT(!m_layerList.head());
56     ASSERT(!m_taskObserverActive);
57 }
58
59 void Canvas2DLayerManager::init(size_t maxBytesAllocated, size_t targetBytesAllocated)
60 {
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;
67     }
68 }
69
70 Canvas2DLayerManager& Canvas2DLayerManager::get()
71 {
72     DEFINE_STATIC_LOCAL(Canvas2DLayerManager, manager, ());
73     return manager;
74 }
75
76 void Canvas2DLayerManager::willProcessTask()
77 {
78 }
79
80 void Canvas2DLayerManager::didProcessTask()
81 {
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();
87     while (layer) {
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();
93     }
94 }
95
96 void Canvas2DLayerManager::layerDidDraw(Canvas2DLayerBridge* layer)
97 {
98     if (isInList(layer)) {
99         if (layer != m_layerList.head()) {
100             m_layerList.remove(layer);
101             m_layerList.push(layer); // Set as MRU
102         }
103     }
104
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);
109     }
110 }
111
112 void Canvas2DLayerManager::layerTransientResourceAllocationChanged(Canvas2DLayerBridge* layer, intptr_t deltaBytes)
113 {
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);
120         layer->setNext(0);
121         layer->setPrev(0);
122     }
123
124     if (deltaBytes > 0)
125         freeMemoryIfNecessary();
126 }
127
128 void Canvas2DLayerManager::freeMemoryIfNecessary()
129 {
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());
138         }
139
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());
148         }
149     }
150 }
151
152 void Canvas2DLayerManager::removeLayerFromList(Canvas2DLayerBridge* layer)
153 {
154     ASSERT(isInList(layer));
155     ASSERT(!layer->hasTransientResources());
156
157 }
158
159 bool Canvas2DLayerManager::isInList(Canvas2DLayerBridge* layer) const
160 {
161     return layer->prev() || m_layerList.head() == layer;
162 }
163
164 }
165