OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 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. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 | 26 |
27 #include "core/platform/graphics/chromium/Canvas2DLayerManager.h" | 27 #include "core/platform/graphics/chromium/Canvas2DLayerManager.h" |
28 | 28 |
29 #include "SkDevice.h" | 29 #include "SkDevice.h" |
30 #include "core/platform/graphics/GraphicsContext3D.h" | 30 #include "core/platform/graphics/GraphicsContext3D.h" |
| 31 #include "core/platform/graphics/chromium/Canvas2DLayerBridgeTestHelper.h" |
31 #include "core/tests/FakeWebGraphicsContext3D.h" | 32 #include "core/tests/FakeWebGraphicsContext3D.h" |
32 #include "public/platform/Platform.h" | 33 #include "public/platform/Platform.h" |
33 #include "public/platform/WebThread.h" | 34 #include "public/platform/WebThread.h" |
34 | 35 |
35 #include <gmock/gmock.h> | 36 #include <gmock/gmock.h> |
36 #include <gtest/gtest.h> | 37 #include <gtest/gtest.h> |
37 | 38 |
38 using namespace WebCore; | 39 using namespace WebCore; |
39 using testing::InSequence; | 40 using testing::InSequence; |
40 using testing::Return; | 41 using testing::Return; |
41 using testing::Test; | 42 using testing::Test; |
42 | 43 |
43 | 44 |
44 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { | 45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { |
45 public: | 46 public: |
46 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, SkDeferredCan
vas* canvas) | |
47 : Canvas2DLayerBridge(context, canvas, NonOpaque) | |
48 , m_freeableBytes(0) | |
49 , m_freeMemoryIfPossibleCount(0) | |
50 , m_flushCount(0) | |
51 { | |
52 } | |
53 | |
54 virtual size_t storageAllocatedForRecording() OVERRIDE | 47 virtual size_t storageAllocatedForRecording() OVERRIDE |
55 { | 48 { |
56 // Because the fake layer has no canvas to query, just | 49 // Because the fake layer has no canvas to query, just |
57 // return status quo. Allocation changes that would normally be | 50 // return status quo. Allocation changes that would normally be |
58 // initiated by the canvas can be faked by invoking | 51 // initiated by the canvas can be faked by invoking |
59 // storageAllocatedForRecordingChanged directly from the test code. | 52 // storageAllocatedForRecordingChanged directly from the test code. |
60 return m_bytesAllocated; | 53 return m_bytesAllocated; |
61 } | 54 } |
62 | 55 |
63 void fakeFreeableBytes(size_t size) | 56 void fakeFreeableBytes(size_t size) |
(...skipping 11 matching lines...) Expand all Loading... |
75 m_bytesAllocated -= bytesFreed; | 68 m_bytesAllocated -= bytesFreed; |
76 return bytesFreed; | 69 return bytesFreed; |
77 } | 70 } |
78 | 71 |
79 virtual void flush() OVERRIDE | 72 virtual void flush() OVERRIDE |
80 { | 73 { |
81 flushedDrawCommands(); | 74 flushedDrawCommands(); |
82 m_flushCount++; | 75 m_flushCount++; |
83 } | 76 } |
84 | 77 |
| 78 static PassOwnPtr<FakeCanvas2DLayerBridge> create(const IntSize& size) |
| 79 { |
| 80 OwnPtr<Canvas2DLayerBridgeTestHelper> helper = adoptPtr(new Canvas2DLaye
rBridgeTestHelper); |
| 81 bool success; |
| 82 OwnPtr<FakeCanvas2DLayerBridge> bridge = adoptPtr(new FakeCanvas2DLayerB
ridge(helper.release(), size, &success)); |
| 83 if (!success) |
| 84 bridge.clear(); |
| 85 return bridge.release(); |
| 86 } |
| 87 |
85 public: | 88 public: |
86 size_t m_freeableBytes; | 89 size_t m_freeableBytes; |
87 int m_freeMemoryIfPossibleCount; | 90 int m_freeMemoryIfPossibleCount; |
88 int m_flushCount; | 91 int m_flushCount; |
| 92 |
| 93 private: |
| 94 FakeCanvas2DLayerBridge(PassOwnPtr<Canvas2DLayerBridge::Helper> helper, cons
t IntSize& size, bool* success) |
| 95 : Canvas2DLayerBridge(helper, size, NonOpaque, success) |
| 96 , m_freeableBytes(0) |
| 97 , m_freeMemoryIfPossibleCount(0) |
| 98 , m_flushCount(0) |
| 99 { |
| 100 } |
89 }; | 101 }; |
90 | 102 |
91 static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) | |
92 { | |
93 SkAutoTUnref<SkDevice> device(new SkDevice(SkBitmap::kARGB_8888_Config, 1, 1
)); | |
94 return adoptPtr(new SkDeferredCanvas(device.get())); | |
95 } | |
96 | |
97 class Canvas2DLayerManagerTest : public Test { | 103 class Canvas2DLayerManagerTest : public Test { |
98 protected: | 104 protected: |
99 void storageAllocationTrackingTest() | 105 void storageAllocationTrackingTest() |
100 { | 106 { |
101 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 107 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
102 manager.init(10, 10); | 108 manager.init(10, 10); |
103 { | 109 { |
104 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic
sContextFromWebContext(adoptPtr(new WebKit::FakeWebGraphicsContext3D)); | 110 OwnPtr<FakeCanvas2DLayerBridge> layer1 = FakeCanvas2DLayerBridge::cr
eate(IntSize(1, 1)); |
105 OwnPtr<SkDeferredCanvas> canvas1 = createCanvas(context.get()); | |
106 FakeCanvas2DLayerBridge layer1(context, canvas1.get()); | |
107 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); | 111 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); |
108 layer1.storageAllocatedForRecordingChanged(1); | 112 layer1->storageAllocatedForRecordingChanged(1); |
109 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 113 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
110 // Test allocation increase | 114 // Test allocation increase |
111 layer1.storageAllocatedForRecordingChanged(2); | 115 layer1->storageAllocatedForRecordingChanged(2); |
112 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); | 116 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); |
113 // Test allocation decrease | 117 // Test allocation decrease |
114 layer1.storageAllocatedForRecordingChanged(1); | 118 layer1->storageAllocatedForRecordingChanged(1); |
115 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 119 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
116 { | 120 { |
117 OwnPtr<SkDeferredCanvas> canvas2 = createCanvas(context.get()); | 121 OwnPtr<FakeCanvas2DLayerBridge> layer2 = FakeCanvas2DLayerBridge
::create(IntSize(1, 1)); |
118 FakeCanvas2DLayerBridge layer2(context, canvas2.get()); | |
119 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 122 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
120 // verify multi-layer allocation tracking | 123 // verify multi-layer allocation tracking |
121 layer2.storageAllocatedForRecordingChanged(2); | 124 layer2->storageAllocatedForRecordingChanged(2); |
122 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); | 125 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); |
123 } | 126 } |
124 // Verify tracking after destruction | 127 // Verify tracking after destruction |
125 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 128 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
126 } | 129 } |
127 } | 130 } |
128 | 131 |
129 void evictionTest() | 132 void evictionTest() |
130 { | 133 { |
131 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new WebKit::FakeWebGraphicsContext3D)); | |
132 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 134 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
133 manager.init(10, 5); | 135 manager.init(10, 5); |
134 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | 136 OwnPtr<FakeCanvas2DLayerBridge> layer = FakeCanvas2DLayerBridge::create(
IntSize(1, 1)); |
135 FakeCanvas2DLayerBridge layer(context, canvas.get()); | 137 layer->fakeFreeableBytes(10); |
136 layer.fakeFreeableBytes(10); | 138 layer->storageAllocatedForRecordingChanged(8); // under the max |
137 layer.storageAllocatedForRecordingChanged(8); // under the max | 139 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
138 EXPECT_EQ(0, layer.m_freeMemoryIfPossibleCount); | 140 layer->storageAllocatedForRecordingChanged(12); // over the max |
139 layer.storageAllocatedForRecordingChanged(12); // over the max | 141 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); |
140 EXPECT_EQ(1, layer.m_freeMemoryIfPossibleCount); | 142 EXPECT_EQ((size_t)3, layer->m_freeableBytes); |
141 EXPECT_EQ((size_t)3, layer.m_freeableBytes); | 143 EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without trigger
ing a flush |
142 EXPECT_EQ(0, layer.m_flushCount); // eviction succeeded without triggeri
ng a flush | 144 EXPECT_EQ((size_t)5, layer->bytesAllocated()); |
143 EXPECT_EQ((size_t)5, layer.bytesAllocated()); | |
144 } | 145 } |
145 | 146 |
146 void flushEvictionTest() | 147 void flushEvictionTest() |
147 { | 148 { |
148 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new WebKit::FakeWebGraphicsContext3D)); | |
149 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 149 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
150 manager.init(10, 5); | 150 manager.init(10, 5); |
151 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | 151 OwnPtr<FakeCanvas2DLayerBridge> layer = FakeCanvas2DLayerBridge::create(
IntSize(1, 1)); |
152 FakeCanvas2DLayerBridge layer(context, canvas.get()); | 152 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag
gressive eviction by flushing |
153 layer.fakeFreeableBytes(1); // Not enough freeable bytes, will cause agg
ressive eviction by flushing | 153 layer->storageAllocatedForRecordingChanged(8); // under the max |
154 layer.storageAllocatedForRecordingChanged(8); // under the max | 154 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
155 EXPECT_EQ(0, layer.m_freeMemoryIfPossibleCount); | 155 layer->storageAllocatedForRecordingChanged(12); // over the max |
156 layer.storageAllocatedForRecordingChanged(12); // over the max | 156 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo
re flush, one after flush |
157 EXPECT_EQ(2, layer.m_freeMemoryIfPossibleCount); // Two tries, one befor
e flush, one after flush | 157 EXPECT_EQ((size_t)0, layer->m_freeableBytes); |
158 EXPECT_EQ((size_t)0, layer.m_freeableBytes); | 158 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted |
159 EXPECT_EQ(1, layer.m_flushCount); // flush was attempted | 159 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer
from manager's tracking list |
160 EXPECT_EQ((size_t)11, layer.bytesAllocated()); // flush drops the layer
from manager's tracking list | 160 EXPECT_FALSE(manager.isInList(layer.get())); |
161 EXPECT_FALSE(manager.isInList(&layer)); | |
162 } | 161 } |
163 | 162 |
164 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman
ds) | 163 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman
ds) |
165 { | 164 { |
166 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 165 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
167 layer->contextAcquired(); | 166 layer->contextAcquired(); |
168 layer->storageAllocatedForRecordingChanged(1); | 167 layer->storageAllocatedForRecordingChanged(1); |
169 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); | 168 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); |
170 if (skipCommands) { | 169 if (skipCommands) { |
171 layer->contextAcquired(); | 170 layer->contextAcquired(); |
(...skipping 17 matching lines...) Expand all Loading... |
189 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); | 188 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); |
190 } | 189 } |
191 private: | 190 private: |
192 Canvas2DLayerManagerTest* m_test; | 191 Canvas2DLayerManagerTest* m_test; |
193 FakeCanvas2DLayerBridge* m_layer; | 192 FakeCanvas2DLayerBridge* m_layer; |
194 bool m_skipCommands; | 193 bool m_skipCommands; |
195 }; | 194 }; |
196 | 195 |
197 void deferredFrameTest() | 196 void deferredFrameTest() |
198 { | 197 { |
199 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon
textFromWebContext(adoptPtr(new WebKit::FakeWebGraphicsContext3D)); | |
200 Canvas2DLayerManager::get().init(10, 10); | 198 Canvas2DLayerManager::get().init(10, 10); |
201 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | 199 OwnPtr<FakeCanvas2DLayerBridge> fakeLayer = FakeCanvas2DLayerBridge::cre
ate(IntSize(1, 1)); |
202 FakeCanvas2DLayerBridge fakeLayer(context, canvas.get()); | 200 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, fakeLayer.get(), true)); |
203 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, &fakeLayer, true)); | |
204 WebKit::Platform::current()->currentThread()->enterRunLoop(); | 201 WebKit::Platform::current()->currentThread()->enterRunLoop(); |
205 // Verify that didProcessTask was called upon completion | 202 // Verify that didProcessTask was called upon completion |
206 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 203 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
207 // Verify that no flush was performed because frame is fresh | 204 // Verify that no flush was performed because frame is fresh |
208 EXPECT_EQ(0, fakeLayer.m_flushCount); | 205 EXPECT_EQ(0, fakeLayer->m_flushCount); |
209 | 206 |
210 // Verify that no flushes are triggered as long as frame are fresh | 207 // Verify that no flushes are triggered as long as frame are fresh |
211 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, &fakeLayer, true)); | 208 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, fakeLayer.get(), true)); |
212 WebKit::Platform::current()->currentThread()->enterRunLoop(); | 209 WebKit::Platform::current()->currentThread()->enterRunLoop(); |
213 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 210 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
214 EXPECT_EQ(0, fakeLayer.m_flushCount); | 211 EXPECT_EQ(0, fakeLayer->m_flushCount); |
215 | 212 |
216 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, &fakeLayer, true)); | 213 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, fakeLayer.get(), true)); |
217 WebKit::Platform::current()->currentThread()->enterRunLoop(); | 214 WebKit::Platform::current()->currentThread()->enterRunLoop(); |
218 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 215 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
219 EXPECT_EQ(0, fakeLayer.m_flushCount); | 216 EXPECT_EQ(0, fakeLayer->m_flushCount); |
220 | 217 |
221 // Verify that a flush is triggered when queue is accumulating a multi-f
rame backlog. | 218 // Verify that a flush is triggered when queue is accumulating a multi-f
rame backlog. |
222 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, &fakeLayer, false)); | 219 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, fakeLayer.get(), false)); |
223 WebKit::Platform::current()->currentThread()->enterRunLoop(); | 220 WebKit::Platform::current()->currentThread()->enterRunLoop(); |
224 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 221 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
225 EXPECT_EQ(1, fakeLayer.m_flushCount); | 222 EXPECT_EQ(1, fakeLayer->m_flushCount); |
226 | 223 |
227 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, &fakeLayer, false)); | 224 WebKit::Platform::current()->currentThread()->postTask(new DeferredFrame
TestTask(this, fakeLayer.get(), false)); |
228 WebKit::Platform::current()->currentThread()->enterRunLoop(); | 225 WebKit::Platform::current()->currentThread()->enterRunLoop(); |
229 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 226 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
230 EXPECT_EQ(2, fakeLayer.m_flushCount); | 227 EXPECT_EQ(2, fakeLayer->m_flushCount); |
231 } | 228 } |
232 }; | 229 }; |
233 | 230 |
234 namespace { | 231 namespace { |
235 | 232 |
236 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) | 233 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) |
237 { | 234 { |
238 storageAllocationTrackingTest(); | 235 storageAllocationTrackingTest(); |
239 } | 236 } |
240 | 237 |
241 TEST_F(Canvas2DLayerManagerTest, testEviction) | 238 TEST_F(Canvas2DLayerManagerTest, testEviction) |
242 { | 239 { |
243 evictionTest(); | 240 evictionTest(); |
244 } | 241 } |
245 | 242 |
246 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) | 243 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) |
247 { | 244 { |
248 flushEvictionTest(); | 245 flushEvictionTest(); |
249 } | 246 } |
250 | 247 |
251 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) | 248 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) |
252 { | 249 { |
253 deferredFrameTest(); | 250 deferredFrameTest(); |
254 } | 251 } |
255 | 252 |
256 } // namespace | 253 } // namespace |
257 | 254 |
OLD | NEW |