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 AN
Y | |
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 AN
Y | |
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 O
N | |
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 "config.h" | |
26 | |
27 #include "core/platform/graphics/Canvas2DLayerManager.h" | |
28 | |
29 #include "SkDevice.h" | |
30 #include "SkSurface.h" | |
31 #include "core/platform/graphics/GraphicsContext3D.h" | |
32 #include "core/platform/testing/FakeWebGraphicsContext3D.h" | |
33 #include "public/platform/Platform.h" | |
34 #include "public/platform/WebThread.h" | |
35 | |
36 #include <gmock/gmock.h> | |
37 #include <gtest/gtest.h> | |
38 | |
39 using namespace WebCore; | |
40 using testing::InSequence; | |
41 using testing::Return; | |
42 using testing::Test; | |
43 | |
44 | |
45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { | |
46 public: | |
47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassRefPtr<Sk
DeferredCanvas> canvas) | |
48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) | |
49 , m_freeableBytes(0) | |
50 , m_freeMemoryIfPossibleCount(0) | |
51 , m_flushCount(0) | |
52 { | |
53 } | |
54 | |
55 virtual size_t storageAllocatedForRecording() OVERRIDE | |
56 { | |
57 // Because the fake layer has no canvas to query, just | |
58 // return status quo. Allocation changes that would normally be | |
59 // initiated by the canvas can be faked by invoking | |
60 // storageAllocatedForRecordingChanged directly from the test code. | |
61 return m_bytesAllocated; | |
62 } | |
63 | |
64 void fakeFreeableBytes(size_t size) | |
65 { | |
66 m_freeableBytes = size; | |
67 } | |
68 | |
69 virtual size_t freeMemoryIfPossible(size_t size) OVERRIDE | |
70 { | |
71 m_freeMemoryIfPossibleCount++; | |
72 size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes; | |
73 m_freeableBytes -= bytesFreed; | |
74 if (bytesFreed) | |
75 Canvas2DLayerManager::get().layerAllocatedStorageChanged(this, -((in
tptr_t)bytesFreed)); | |
76 m_bytesAllocated -= bytesFreed; | |
77 return bytesFreed; | |
78 } | |
79 | |
80 virtual void flush() OVERRIDE | |
81 { | |
82 flushedDrawCommands(); | |
83 m_flushCount++; | |
84 } | |
85 | |
86 public: | |
87 size_t m_freeableBytes; | |
88 int m_freeMemoryIfPossibleCount; | |
89 int m_flushCount; | |
90 }; | |
91 | |
92 static PassRefPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) | |
93 { | |
94 return adoptRef(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1)))
; | |
95 } | |
96 | |
97 FakeCanvas2DLayerBridge* fake(const Canvas2DLayerBridgePtr& layer) | |
98 { | |
99 return static_cast<FakeCanvas2DLayerBridge*>(layer.get()); | |
100 } | |
101 | |
102 class Canvas2DLayerManagerTest : public Test { | |
103 protected: | |
104 void storageAllocationTrackingTest() | |
105 { | |
106 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | |
107 manager.init(10, 10); | |
108 { | |
109 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic
sContextFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | |
110 RefPtr<SkDeferredCanvas> canvas1(createCanvas(context.get())); | |
111 Canvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(c
ontext, canvas1.release()))); | |
112 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); | |
113 layer1->storageAllocatedForRecordingChanged(1); | |
114 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | |
115 // Test allocation increase | |
116 layer1->storageAllocatedForRecordingChanged(2); | |
117 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); | |
118 // Test allocation decrease | |
119 layer1->storageAllocatedForRecordingChanged(1); | |
120 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | |
121 { | |
122 RefPtr<SkDeferredCanvas> canvas2(createCanvas(context.get())); | |
123 Canvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBrid
ge(context, canvas2.release()))); | |
124 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | |
125 // verify multi-layer allocation tracking | |
126 layer2->storageAllocatedForRecordingChanged(2); | |
127 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); | |
128 } | |
129 // Verify tracking after destruction | |
130 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | |
131 } | |
132 } | |
133 | |
134 void evictionTest() | |
135 { | |
136 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | |
137 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | |
138 manager.init(10, 5); | |
139 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | |
140 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex
t, canvas.release()))); | |
141 fake(layer)->fakeFreeableBytes(10); | |
142 layer->storageAllocatedForRecordingChanged(8); // under the max | |
143 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); | |
144 layer->storageAllocatedForRecordingChanged(12); // over the max | |
145 EXPECT_EQ(1, fake(layer)->m_freeMemoryIfPossibleCount); | |
146 EXPECT_EQ((size_t)3, fake(layer)->m_freeableBytes); | |
147 EXPECT_EQ(0, fake(layer)->m_flushCount); // eviction succeeded without t
riggering a flush | |
148 EXPECT_EQ((size_t)5, layer->bytesAllocated()); | |
149 } | |
150 | |
151 void flushEvictionTest() | |
152 { | |
153 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | |
154 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | |
155 manager.init(10, 5); | |
156 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | |
157 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex
t, canvas.release()))); | |
158 fake(layer)->fakeFreeableBytes(1); // Not enough freeable bytes, will ca
use aggressive eviction by flushing | |
159 layer->storageAllocatedForRecordingChanged(8); // under the max | |
160 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); | |
161 layer->storageAllocatedForRecordingChanged(12); // over the max | |
162 EXPECT_EQ(2, fake(layer)->m_freeMemoryIfPossibleCount); // Two tries, on
e before flush, one after flush | |
163 EXPECT_EQ((size_t)0, fake(layer)->m_freeableBytes); | |
164 EXPECT_EQ(1, fake(layer)->m_flushCount); // flush was attempted | |
165 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer
from manager's tracking list | |
166 EXPECT_FALSE(manager.isInList(layer.get())); | |
167 } | |
168 | |
169 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman
ds) | |
170 { | |
171 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
172 layer->contextAcquired(); | |
173 layer->storageAllocatedForRecordingChanged(1); | |
174 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); | |
175 if (skipCommands) { | |
176 layer->contextAcquired(); | |
177 layer->storageAllocatedForRecordingChanged(0); | |
178 layer->skippedPendingDrawCommands(); | |
179 } | |
180 blink::Platform::current()->currentThread()->exitRunLoop(); | |
181 } | |
182 | |
183 class DeferredFrameTestTask : public blink::WebThread::Task { | |
184 public: | |
185 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB
ridge* layer, bool skipCommands) | |
186 { | |
187 m_test = test; | |
188 m_layer = layer; | |
189 m_skipCommands = skipCommands; | |
190 } | |
191 | |
192 virtual void run() OVERRIDE | |
193 { | |
194 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); | |
195 } | |
196 private: | |
197 Canvas2DLayerManagerTest* m_test; | |
198 FakeCanvas2DLayerBridge* m_layer; | |
199 bool m_skipCommands; | |
200 }; | |
201 | |
202 void deferredFrameTest() | |
203 { | |
204 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | |
205 Canvas2DLayerManager::get().init(10, 10); | |
206 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | |
207 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex
t, canvas.release()))); | |
208 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT
estTask(this, fake(layer), true)); | |
209 blink::Platform::current()->currentThread()->enterRunLoop(); | |
210 // Verify that didProcessTask was called upon completion | |
211 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
212 // Verify that no flush was performed because frame is fresh | |
213 EXPECT_EQ(0, fake(layer)->m_flushCount); | |
214 | |
215 // Verify that no flushes are triggered as long as frame are fresh | |
216 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT
estTask(this, fake(layer), true)); | |
217 blink::Platform::current()->currentThread()->enterRunLoop(); | |
218 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
219 EXPECT_EQ(0, fake(layer)->m_flushCount); | |
220 | |
221 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT
estTask(this, fake(layer), true)); | |
222 blink::Platform::current()->currentThread()->enterRunLoop(); | |
223 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
224 EXPECT_EQ(0, fake(layer)->m_flushCount); | |
225 | |
226 // Verify that a flush is triggered when queue is accumulating a multi-f
rame backlog. | |
227 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT
estTask(this, fake(layer), false)); | |
228 blink::Platform::current()->currentThread()->enterRunLoop(); | |
229 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
230 EXPECT_EQ(1, fake(layer)->m_flushCount); | |
231 | |
232 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT
estTask(this, fake(layer), false)); | |
233 blink::Platform::current()->currentThread()->enterRunLoop(); | |
234 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | |
235 EXPECT_EQ(2, fake(layer)->m_flushCount); | |
236 } | |
237 }; | |
238 | |
239 namespace { | |
240 | |
241 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) | |
242 { | |
243 storageAllocationTrackingTest(); | |
244 } | |
245 | |
246 TEST_F(Canvas2DLayerManagerTest, testEviction) | |
247 { | |
248 evictionTest(); | |
249 } | |
250 | |
251 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) | |
252 { | |
253 flushEvictionTest(); | |
254 } | |
255 | |
256 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) | |
257 { | |
258 deferredFrameTest(); | |
259 } | |
260 | |
261 } // namespace | |
262 | |
OLD | NEW |