| 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 EXPECT_EQ((size_t)10, layer->bytesAllocated()); |
| 181 layer->setIsHidden(true); |
| 182 EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount); |
| 183 EXPECT_EQ((size_t)0, layer->m_freeableBytes); |
| 184 EXPECT_EQ((size_t)0, layer->bytesAllocated()); |
| 185 EXPECT_EQ(1, layer->m_flushCount); |
| 186 } |
| 187 |
| 188 void addRemoveLayerTest() |
| 189 { |
| 190 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink:
:MockWebGraphicsContext3D); |
| 191 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp
ort(webContext.get()); |
| 192 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
| 193 manager.init(10, 5); |
| 194 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
| 195 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co
ntext, canvas.release()))); |
| 196 EXPECT_FALSE(manager.isInList(layer.get())); |
| 197 layer->storageAllocatedForRecordingChanged(5); |
| 198 EXPECT_TRUE(manager.isInList(layer.get())); |
| 199 layer->storageAllocatedForRecordingChanged(0); |
| 200 EXPECT_FALSE(manager.isInList(layer.get())); |
| 201 } |
| 202 |
| 165 void flushEvictionTest() | 203 void flushEvictionTest() |
| 166 { | 204 { |
| 167 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink:
:MockWebGraphicsContext3D); | 205 OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink:
:MockWebGraphicsContext3D); |
| 168 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp
ort(webContext.get()); | 206 RefPtr<GraphicsContext3D> context = GraphicsContext3D::createContextSupp
ort(webContext.get()); |
| 169 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); | 207 Canvas2DLayerManager& manager = Canvas2DLayerManager::get(); |
| 170 manager.init(10, 5); | 208 manager.init(10, 5); |
| 171 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); | 209 OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get()); |
| 172 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co
ntext, canvas.release()))); | 210 FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(co
ntext, canvas.release()))); |
| 173 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag
gressive eviction by flushing | 211 layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause ag
gressive eviction by flushing |
| 174 layer->storageAllocatedForRecordingChanged(8); // under the max | 212 layer->storageAllocatedForRecordingChanged(8); // under the max |
| 175 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); | 213 EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount); |
| 176 layer->storageAllocatedForRecordingChanged(12); // over the max | 214 layer->storageAllocatedForRecordingChanged(12); // over the max |
| 177 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo
re flush, one after flush | 215 EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one befo
re flush, one after flush |
| 178 EXPECT_EQ((size_t)0, layer->m_freeableBytes); | 216 EXPECT_EQ((size_t)5, layer->m_freeableBytes); |
| 179 EXPECT_EQ(1, layer->m_flushCount); // flush was attempted | 217 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 | 218 EXPECT_EQ((size_t)5, layer->bytesAllocated()); |
| 181 EXPECT_FALSE(manager.isInList(layer.get())); | 219 EXPECT_TRUE(manager.isInList(layer.get())); |
| 182 } | 220 } |
| 183 | 221 |
| 184 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman
ds) | 222 void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipComman
ds) |
| 185 { | 223 { |
| 186 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); | 224 EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 187 layer->willUse(); | 225 layer->willUse(); |
| 188 layer->storageAllocatedForRecordingChanged(1); | 226 layer->storageAllocatedForRecordingChanged(1); |
| 189 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); | 227 EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive); |
| 190 if (skipCommands) { | 228 if (skipCommands) { |
| 191 layer->willUse(); | 229 layer->willUse(); |
| 192 layer->storageAllocatedForRecordingChanged(0); | |
| 193 layer->skippedPendingDrawCommands(); | 230 layer->skippedPendingDrawCommands(); |
| 194 } | 231 } |
| 195 blink::Platform::current()->currentThread()->exitRunLoop(); | 232 blink::Platform::current()->currentThread()->exitRunLoop(); |
| 196 } | 233 } |
| 197 | 234 |
| 198 class DeferredFrameTestTask : public blink::WebThread::Task { | 235 class DeferredFrameTestTask : public blink::WebThread::Task { |
| 199 public: | 236 public: |
| 200 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB
ridge* layer, bool skipCommands) | 237 DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerB
ridge* layer, bool skipCommands) |
| 201 { | 238 { |
| 202 m_test = test; | 239 m_test = test; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) | 304 TEST_F(Canvas2DLayerManagerTest, testFlushEviction) |
| 268 { | 305 { |
| 269 flushEvictionTest(); | 306 flushEvictionTest(); |
| 270 } | 307 } |
| 271 | 308 |
| 272 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) | 309 TEST_F(Canvas2DLayerManagerTest, testDeferredFrame) |
| 273 { | 310 { |
| 274 deferredFrameTest(); | 311 deferredFrameTest(); |
| 275 } | 312 } |
| 276 | 313 |
| 277 } // namespace | 314 TEST_F(Canvas2DLayerManagerTest, testHiddenCanvas) |
| 315 { |
| 316 hiddenCanvasTest(); |
| 317 } |
| 278 | 318 |
| 319 TEST_F(Canvas2DLayerManagerTest, testAddRemoveLayer) |
| 320 { |
| 321 addRemoveLayerTest(); |
| 322 } |
| 323 |
| 324 } // unnamed namespace |
| 325 |
| OLD | NEW |