Chromium Code Reviews| 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 26 matching lines...) Expand all Loading... | |
| 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 | 44 |
| 45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { | 45 class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge { |
| 46 public: | 46 public: |
| 47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassRefPtr<Sk DeferredCanvas> canvas) | 47 FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassOwnPtr<Sk DeferredCanvas> canvas) |
| 48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) | 48 : Canvas2DLayerBridge(context, canvas, 0, NonOpaque) |
| 49 , m_freeableBytes(0) | 49 , m_freeableBytes(0) |
| 50 , m_freeMemoryIfPossibleCount(0) | 50 , m_freeMemoryIfPossibleCount(0) |
| 51 , m_flushCount(0) | 51 , m_flushCount(0) |
| 52 { | 52 { |
| 53 } | 53 } |
| 54 | 54 |
| 55 virtual size_t storageAllocatedForRecording() OVERRIDE | 55 virtual size_t storageAllocatedForRecording() OVERRIDE |
| 56 { | 56 { |
| 57 // Because the fake layer has no canvas to query, just | 57 // Because the fake layer has no canvas to query, just |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 82 flushedDrawCommands(); | 82 flushedDrawCommands(); |
| 83 m_flushCount++; | 83 m_flushCount++; |
| 84 } | 84 } |
| 85 | 85 |
| 86 public: | 86 public: |
| 87 size_t m_freeableBytes; | 87 size_t m_freeableBytes; |
| 88 int m_freeMemoryIfPossibleCount; | 88 int m_freeMemoryIfPossibleCount; |
| 89 int m_flushCount; | 89 int m_flushCount; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 static PassRefPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) | 92 static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context) |
| 93 { | 93 { |
| 94 return adoptRef(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ; | 94 return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1))) ; |
| 95 } | |
| 96 | |
| 97 FakeCanvas2DLayerBridge* fake(const Canvas2DLayerBridgePtr& layer) | |
| 98 { | |
| 99 return static_cast<FakeCanvas2DLayerBridge*>(layer.get()); | |
| 100 } | 95 } |
| 101 | 96 |
| 102 class Canvas2DLayerManagerTest : public Test { | 97 class Canvas2DLayerManagerTest : public Test { |
| 103 protected: | 98 protected: |
| 104 void storageAllocationTrackingTest() | 99 void storageAllocationTrackingTest() |
| 105 { | 100 { |
| 106 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 101 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
| 107 manager.init(10, 10); | 102 manager.init(10, 10); |
| 108 { | 103 { |
| 109 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic sContextFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | 104 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphic sContextFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); |
| 110 RefPtr<SkDeferredCanvas> canvas1(createCanvas(context.get())); | 105 OwnPtr<SkDeferredCanvas> canvas1 = createCanvas(context.get()); |
| 111 Canvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(c ontext, canvas1.release()))); | 106 RefPtr<FakeCanvas2DLayerBridge> layer1 = adoptRef(new FakeCanvas2DLa yerBridge(context, canvas1.release())); |
| 112 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); | 107 EXPECT_EQ((size_t)0, manager.m_bytesAllocated); |
| 113 layer1->storageAllocatedForRecordingChanged(1); | 108 layer1->storageAllocatedForRecordingChanged(1); |
| 114 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 109 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
| 115 // Test allocation increase | 110 // Test allocation increase |
| 116 layer1->storageAllocatedForRecordingChanged(2); | 111 layer1->storageAllocatedForRecordingChanged(2); |
| 117 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); | 112 EXPECT_EQ((size_t)2, manager.m_bytesAllocated); |
| 118 // Test allocation decrease | 113 // Test allocation decrease |
| 119 layer1->storageAllocatedForRecordingChanged(1); | 114 layer1->storageAllocatedForRecordingChanged(1); |
| 120 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 115 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
| 121 { | 116 { |
| 122 RefPtr<SkDeferredCanvas> canvas2(createCanvas(context.get())); | 117 OwnPtr<SkDeferredCanvas> canvas2 = createCanvas(context.get()); |
| 123 Canvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBrid ge(context, canvas2.release()))); | 118 RefPtr<FakeCanvas2DLayerBridge> layer2 = adoptRef(new FakeCanvas 2DLayerBridge(context, canvas2.release())); |
| 124 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 119 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
| 125 // verify multi-layer allocation tracking | 120 // verify multi-layer allocation tracking |
| 126 layer2->storageAllocatedForRecordingChanged(2); | 121 layer2->storageAllocatedForRecordingChanged(2); |
| 127 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); | 122 EXPECT_EQ((size_t)3, manager.m_bytesAllocated); |
| 123 layer2->destroy(); | |
| 128 } | 124 } |
| 129 // Verify tracking after destruction | 125 // Verify tracking after destruction |
| 130 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); | 126 EXPECT_EQ((size_t)1, manager.m_bytesAllocated); |
| 127 layer1->destroy(); | |
| 131 } | 128 } |
| 132 } | 129 } |
| 133 | 130 |
| 134 void evictionTest() | 131 void evictionTest() |
| 135 { | 132 { |
| 136 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | 133 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); |
| 137 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 134 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
| 138 manager.init(10, 5); | 135 manager.init(10, 5); |
| 139 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | 136 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
| 140 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); | 137 RefPtr<FakeCanvas2DLayerBridge> layer = adoptRef(new FakeCanvas2DLayerBr idge(context, canvas.release())); |
| 141 fake(layer)->fakeFreeableBytes(10); | 138 layer->fakeFreeableBytes(10); |
| 142 layer->storageAllocatedForRecordingChanged(8); // under the max | 139 layer->storageAllocatedForRecordingChanged(8); // under the max |
| 143 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); | 140 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
| 144 layer->storageAllocatedForRecordingChanged(12); // over the max | 141 layer->storageAllocatedForRecordingChanged(12); // over the max |
| 145 EXPECT_EQ(1, fake(layer)->m_freeMemoryIfPossibleCount); | 142 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); |
| 146 EXPECT_EQ((size_t)3, fake(layer)->m_freeableBytes); | 143 EXPECT_EQ((size_t)3, layer->m_freeableBytes); |
| 147 EXPECT_EQ(0, fake(layer)->m_flushCount); // eviction succeeded without t riggering a flush | 144 EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without trigger ing a flush |
| 148 EXPECT_EQ((size_t)5, layer->bytesAllocated()); | 145 EXPECT_EQ((size_t)5, layer->bytesAllocated()); |
| 146 layer->destroy(); | |
| 149 } | 147 } |
| 150 | 148 |
| 151 void flushEvictionTest() | 149 void flushEvictionTest() |
| 152 { | 150 { |
| 153 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | 151 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); |
| 154 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 152 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
| 155 manager.init(10, 5); | 153 manager.init(10, 5); |
| 156 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | 154 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
| 157 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); | 155 RefPtr<FakeCanvas2DLayerBridge> layer = adoptRef(new FakeCanvas2DLayerBr idge(context, canvas.release())); |
| 158 fake(layer)->fakeFreeableBytes(1); // Not enough freeable bytes, will ca use aggressive eviction by flushing | 156 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag gressive eviction by flushing |
| 159 layer->storageAllocatedForRecordingChanged(8); // under the max | 157 layer->storageAllocatedForRecordingChanged(8); // under the max |
| 160 EXPECT_EQ(0, fake(layer)->m_freeMemoryIfPossibleCount); | 158 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
| 161 layer->storageAllocatedForRecordingChanged(12); // over the max | 159 layer->storageAllocatedForRecordingChanged(12); // over the max |
| 162 EXPECT_EQ(2, fake(layer)->m_freeMemoryIfPossibleCount); // Two tries, on e before flush, one after flush | 160 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo re flush, one after flush |
| 163 EXPECT_EQ((size_t)0, fake(layer)->m_freeableBytes); | 161 EXPECT_EQ((size_t)0, layer->m_freeableBytes); |
| 164 EXPECT_EQ(1, fake(layer)->m_flushCount); // flush was attempted | 162 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted |
| 165 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list | 163 EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list |
| 166 EXPECT_FALSE(manager.isInList(layer.get())); | 164 EXPECT_FALSE(manager.isInList(layer.get())); |
| 165 layer->destroy(); | |
| 167 } | 166 } |
| 168 | 167 |
| 169 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds) | 168 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman ds) |
| 170 { | 169 { |
| 171 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 170 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 172 layer->contextAcquired(); | 171 layer->aboutToUse(); |
| 173 layer->storageAllocatedForRecordingChanged(1); | 172 layer->storageAllocatedForRecordingChanged(1); |
| 174 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); | 173 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 175 if (skipCommands) { | 174 if (skipCommands) { |
| 176 layer->contextAcquired(); | 175 layer->aboutToUse(); |
| 177 layer->storageAllocatedForRecordingChanged(0); | 176 layer->storageAllocatedForRecordingChanged(0); |
| 178 layer->skippedPendingDrawCommands(); | 177 layer->skippedPendingDrawCommands(); |
| 179 } | 178 } |
| 180 blink::Platform::current()->currentThread()->exitRunLoop(); | 179 blink::Platform::current()->currentThread()->exitRunLoop(); |
| 181 } | 180 } |
| 182 | 181 |
| 183 class DeferredFrameTestTask : public blink::WebThread::Task { | 182 class DeferredFrameTestTask : public blink::WebThread::Task { |
| 184 public: | 183 public: |
| 185 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands) | 184 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB ridge* layer, bool skipCommands) |
| 186 { | 185 { |
| 187 m_test = test; | 186 m_test = test; |
| 188 m_layer = layer; | 187 m_layer = layer; |
| 189 m_skipCommands = skipCommands; | 188 m_skipCommands = skipCommands; |
| 190 } | 189 } |
| 191 | 190 |
| 192 virtual void run() OVERRIDE | 191 virtual void run() OVERRIDE |
| 193 { | 192 { |
| 194 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); | 193 m_test->doDeferredFrameTestTask(m_layer, m_skipCommands); |
| 195 } | 194 } |
| 196 private: | 195 private: |
| 197 Canvas2DLayerManagerTest* m_test; | 196 Canvas2DLayerManagerTest* m_test; |
| 198 FakeCanvas2DLayerBridge* m_layer; | 197 FakeCanvas2DLayerBridge* m_layer; |
| 199 bool m_skipCommands; | 198 bool m_skipCommands; |
| 200 }; | 199 }; |
| 201 | 200 |
| 202 void deferredFrameTest() | 201 void deferredFrameTest() |
| 203 { | 202 { |
| 204 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); | 203 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsCon textFromWebContext(adoptPtr(new blink::FakeWebGraphicsContext3D)); |
| 205 Canvas2DLayerManager::get().init(10, 10); | 204 Canvas2DLayerManager::get().init(10, 10); |
| 206 RefPtr<SkDeferredCanvas> canvas(createCanvas(context.get())); | 205 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
| 207 Canvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(contex t, canvas.release()))); | 206 RefPtr<FakeCanvas2DLayerBridge> layer = adoptRef(new FakeCanvas2DLayerBr idge(context, canvas.release())); |
| 208 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), true)); | 207 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true)); |
| 209 blink::Platform::current()->currentThread()->enterRunLoop(); | 208 blink::Platform::current()->currentThread()->enterRunLoop(); |
| 210 // Verify that didProcessTask was called upon completion | 209 // Verify that didProcessTask was called upon completion |
| 211 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 210 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 212 // Verify that no flush was performed because frame is fresh | 211 // Verify that no flush was performed because frame is fresh |
| 213 EXPECT_EQ(0, fake(layer)->m_flushCount); | 212 EXPECT_EQ(0, layer->m_flushCount); |
| 214 | 213 |
| 215 // Verify that no flushes are triggered as long as frame are fresh | 214 // 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)); | 215 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true)); |
| 217 blink::Platform::current()->currentThread()->enterRunLoop(); | 216 blink::Platform::current()->currentThread()->enterRunLoop(); |
| 218 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 217 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 219 EXPECT_EQ(0, fake(layer)->m_flushCount); | 218 EXPECT_EQ(0, layer->m_flushCount); |
| 220 | 219 |
| 221 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), true)); | 220 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), true)); |
| 222 blink::Platform::current()->currentThread()->enterRunLoop(); | 221 blink::Platform::current()->currentThread()->enterRunLoop(); |
| 223 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 222 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 224 EXPECT_EQ(0, fake(layer)->m_flushCount); | 223 EXPECT_EQ(0, layer->m_flushCount); |
| 225 | 224 |
| 226 // Verify that a flush is triggered when queue is accumulating a multi-f rame backlog. | 225 // 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)); | 226 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), false)); |
| 228 blink::Platform::current()->currentThread()->enterRunLoop(); | 227 blink::Platform::current()->currentThread()->enterRunLoop(); |
| 229 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 228 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 230 EXPECT_EQ(1, fake(layer)->m_flushCount); | 229 EXPECT_EQ(1, layer->m_flushCount); |
| 231 | 230 |
| 232 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, fake(layer), false)); | 231 blink::Platform::current()->currentThread()->postTask(new DeferredFrameT estTask(this, layer.get(), false)); |
| 233 blink::Platform::current()->currentThread()->enterRunLoop(); | 232 blink::Platform::current()->currentThread()->enterRunLoop(); |
| 234 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 233 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 235 EXPECT_EQ(2, fake(layer)->m_flushCount); | 234 EXPECT_EQ(2, layer->m_flushCount); |
| 235 | |
| 236 layer->destroy(); | |
|
Stephen White
2013/12/04 21:18:40
As discussed, let's mock out the ImageBufferSurfac
| |
| 236 } | 237 } |
| 237 }; | 238 }; |
| 238 | 239 |
| 239 namespace { | 240 namespace { |
| 240 | 241 |
| 241 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) | 242 TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking) |
| 242 { | 243 { |
| 243 storageAllocationTrackingTest(); | 244 storageAllocationTrackingTest(); |
| 244 } | 245 } |
| 245 | 246 |
| 246 TEST_F(Canvas2DLayerManagerTest, testEviction) | 247 TEST_F(Canvas2DLayerManagerTest, testEviction) |
| 247 { | 248 { |
| 248 evictionTest(); | 249 evictionTest(); |
| 249 } | 250 } |
| 250 | 251 |
| 251 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) | 252 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) |
| 252 { | 253 { |
| 253 flushEvictionTest(); | 254 flushEvictionTest(); |
| 254 } | 255 } |
| 255 | 256 |
| 256 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) | 257 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) |
| 257 { | 258 { |
| 258 deferredFrameTest(); | 259 deferredFrameTest(); |
| 259 } | 260 } |
| 260 | 261 |
| 261 } // namespace | 262 } // namespace |
| 262 | 263 |
| OLD | NEW |