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 23 matching lines...) Expand all Loading... | |
34 #include "web/tests/MockWebGraphicsContext3D.h" | 34 #include "web/tests/MockWebGraphicsContext3D.h" |
35 | 35 |
36 #include <gmock/gmock.h> | 36 #include <gmock/gmock.h> |
37 #include <gtest/gtest.h> | 37 #include <gtest/gtest.h> |
38 | 38 |
39 using namespace WebCore; | 39 using namespace WebCore; |
40 using testing::InSequence; | 40 using testing::InSequence; |
41 using testing::Return; | 41 using testing::Return; |
42 using testing::Test; | 42 using testing::Test; |
43 | 43 |
44 namespace { | |
44 | 45 |
45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { | 46 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { |
46 public: | 47 public: |
47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassOwnPtr<Sk DeferredCanvas> canvas) | 48 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassOwnPtr<Sk DeferredCanvas> canvas) |
48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) | 49 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) |
49 , m_freeableBytes(0) | 50 , m_freeableBytes(0) |
50 , m_freeMemoryIfPossibleCount(0) | 51 , m_freeMemoryIfPossibleCount(0) |
51 , m_flushCount(0) | 52 , m_flushCount(0) |
52 { | 53 { |
53 } | 54 } |
(...skipping 11 matching lines...) Expand all Loading... | |
65 { | 66 { |
66 m_freeableBytes = size; | 67 m_freeableBytes = size; |
67 } | 68 } |
68 | 69 |
69 virtual size_t freeMemoryIfPossible(size_t size) OVERRIDE | 70 virtual size_t freeMemoryIfPossible(size_t size) OVERRIDE |
70 { | 71 { |
71 m_freeMemoryIfPossibleCount++; | 72 m_freeMemoryIfPossibleCount++; |
72 size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes; | 73 size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes; |
73 m_freeableBytes -= bytesFreed; | 74 m_freeableBytes -= bytesFreed; |
74 if (bytesFreed) | 75 if (bytesFreed) |
75 Canvas2DLayerManager::get().layerAllocatedStorageChanged(this, -((in tptr_t)bytesFreed)); | 76 storageAllocatedForRecordingChanged(m_bytesAllocated - bytesFreed); |
76 m_bytesAllocated -= bytesFreed; | |
77 return bytesFreed; | 77 return bytesFreed; |
78 } | 78 } |
79 | 79 |
80 virtual void flush() OVERRIDE | 80 virtual void flush() OVERRIDE |
81 { | 81 { |
82 flushedDrawCommands(); | 82 flushedDrawCommands(); |
83 m_freeableBytes = bytesAllocated(); | |
83 m_flushCount++; | 84 m_flushCount++; |
84 } | 85 } |
85 | 86 |
86 public: | 87 public: |
87 size_t m_freeableBytes; | 88 size_t m_freeableBytes; |
88 int m_freeMemoryIfPossibleCount; | 89 int m_freeMemoryIfPossibleCount; |
89 int m_flushCount; | 90 int m_flushCount; |
90 }; | 91 }; |
91 | 92 |
92 class FakeCanvas2DLayerBridgePtr { | 93 class FakeCanvas2DLayerBridgePtr { |
(...skipping 11 matching lines...) Expand all Loading... | |
104 | 105 |
105 private: | 106 private: |
106 RefPtr<FakeCanvas2DLayerBridge> m_layerBridge; | 107 RefPtr<FakeCanvas2DLayerBridge> m_layerBridge; |
107 }; | 108 }; |
108 | 109 |
109 static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) | 110 static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) |
110 { | 111 { |
111 return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ; | 112 return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ; |
112 } | 113 } |
113 | 114 |
115 } // unnamed namespace | |
116 | |
114 class Canvas2DLayerManagerTest : public Test { | 117 class Canvas2DLayerManagerTest : public Test { |
115 protected: | 118 protected: |
116 void storageAllocationTrackingTest() | 119 void storageAllocationTrackingTest() |
117 { | 120 { |
118 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 121 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
119 manager.init(10, 10); | 122 manager.init(10, 10); |
120 { | 123 { |
121 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new bl ink::MockWebGraphicsContext3D); | 124 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new bl ink::MockWebGraphicsContext3D); |
122 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContext Support(webContext.get()); | 125 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContext Support(webContext.get()); |
123 OwnPtr<SkDeferredCanvas> canvas1 = createCanvas(context.get()); | 126 OwnPtr<SkDeferredCanvas> canvas1 = createCanvas(context.get()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 layer->fakeFreeableBytes(10); | 158 layer->fakeFreeableBytes(10); |
156 layer->storageAllocatedForRecordingChanged(8); // under the max | 159 layer->storageAllocatedForRecordingChanged(8); // under the max |
157 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); | 160 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
158 layer->storageAllocatedForRecordingChanged(12); // over the max | 161 layer->storageAllocatedForRecordingChanged(12); // over the max |
159 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); | 162 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); |
160 EXPECT_EQ((size_t)3, layer->m_freeableBytes); | 163 EXPECT_EQ((size_t)3, layer->m_freeableBytes); |
161 EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without trigger ing a flush | 164 EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without trigger ing a flush |
162 EXPECT_EQ((size_t)5, layer->bytesAllocated()); | 165 EXPECT_EQ((size_t)5, layer->bytesAllocated()); |
163 } | 166 } |
164 | 167 |
168 void hiddenCanvasTest() | |
169 { | |
170 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink: :MockWebGraphicsContext3D); | |
171 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp ort(webContext.get()); | |
172 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | |
173 manager.init(20, 5); | |
174 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | |
175 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release()))); | |
176 layer->fakeFreeableBytes(5); | |
177 layer->storageAllocatedForRecordingChanged(10); | |
178 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); | |
179 EXPECT_EQ(0, layer->m_flushCount); | |
180 layer->setIsHidden(true); | |
181 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); | |
182 EXPECT_EQ((size_t)0, layer->m_freeableBytes); | |
183 EXPECT_EQ((size_t)0, layer->bytesAllocated()); | |
Stephen White
2014/01/24 18:52:35
Could we expect that bytesAllocated() is non-zero
| |
184 EXPECT_EQ(1, layer->m_flushCount); | |
185 } | |
186 | |
187 void addRemoveLayerTest() | |
188 { | |
189 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink: :MockWebGraphicsContext3D); | |
190 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp ort(webContext.get()); | |
191 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | |
192 manager.init(10, 5); | |
193 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | |
194 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release()))); | |
195 EXPECT_FALSE(manager.isInList(layer.get())); | |
196 layer->storageAllocatedForRecordingChanged(5); | |
197 EXPECT_TRUE(manager.isInList(layer.get())); | |
198 layer->storageAllocatedForRecordingChanged(0); | |
199 EXPECT_FALSE(manager.isInList(layer.get())); | |
200 } | |
201 | |
165 void flushEvictionTest() | 202 void flushEvictionTest() |
166 { | 203 { |
167 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink: :MockWebGraphicsContext3D); | 204 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink: :MockWebGraphicsContext3D); |
168 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp ort(webContext.get()); | 205 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp ort(webContext.get()); |
169 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 206 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
170 manager.init(10, 5); | 207 manager.init(10, 5); |
171 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | 208 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
172 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release()))); | 209 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co ntext, canvas.release()))); |
173 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag gressive eviction by flushing | 210 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag gressive eviction by flushing |
174 layer->storageAllocatedForRecordingChanged(8); // under the max | 211 layer->storageAllocatedForRecordingChanged(8); // under the max |
175 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); | 212 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
176 layer->storageAllocatedForRecordingChanged(12); // over the max | 213 layer->storageAllocatedForRecordingChanged(12); // over the max |
177 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo re flush, one after flush | 214 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo re flush, one after flush |
178 EXPECT_EQ((size_t)0, layer->m_freeableBytes); | 215 EXPECT_EQ((size_t)5, layer->m_freeableBytes); |
179 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted | 216 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted |
180 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list | 217 EXPECT_EQ((size_t)5, layer->bytesAllocated()); |
181 EXPECT_FALSE(manager.isInList(layer.get())); | 218 EXPECT_TRUE(manager.isInList(layer.get())); |
182 } | 219 } |
183 | 220 |
184 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds) | 221 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds) |
185 { | 222 { |
186 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 223 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
187 layer->willUse(); | 224 layer->willUse(); |
188 layer->storageAllocatedForRecordingChanged(1); | 225 layer->storageAllocatedForRecordingChanged(1); |
189 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); | 226 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); |
190 if (skipCommands) { | 227 if (skipCommands) { |
191 layer->willUse(); | 228 layer->willUse(); |
192 layer->storageAllocatedForRecordingChanged(0); | |
193 layer->skippedPendingDrawCommands(); | 229 layer->skippedPendingDrawCommands(); |
194 } | 230 } |
195 blink::Platform::current()->currentThread()->exitRunLoop(); | 231 blink::Platform::current()->currentThread()->exitRunLoop(); |
196 } | 232 } |
197 | 233 |
198 class DeferredFrameTestTask : public blink::WebThread::Task { | 234 class DeferredFrameTestTask : public blink::WebThread::Task { |
199 public: | 235 public: |
200 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands) | 236 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands) |
201 { | 237 { |
202 m_test = test; | 238 m_test = test; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) | 303 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) |
268 { | 304 { |
269 flushEvictionTest(); | 305 flushEvictionTest(); |
270 } | 306 } |
271 | 307 |
272 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) | 308 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) |
273 { | 309 { |
274 deferredFrameTest(); | 310 deferredFrameTest(); |
275 } | 311 } |
276 | 312 |
277 } // namespace | 313 TEST_F(Canvas2DLayerManagerTest, testHiddenCanvas) |
314 { | |
315 hiddenCanvasTest(); | |
316 } | |
278 | 317 |
318 TEST_F(Canvas2DLayerManagerTest, testAddRemoveLayer) | |
319 { | |
320 addRemoveLayerTest(); | |
321 } | |
322 | |
323 } // unnamed namespace | |
324 | |
OLD | NEW |