Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2706)

Unified Diff: Source/web/tests/Canvas2DLayerManagerTest.cpp

Issue 117703004: Free temporary GPU and memory resources held by inactive or hidden 2D canvases (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fixed upstream git branch Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/web/tests/Canvas2DLayerManagerTest.cpp
diff --git a/Source/web/tests/Canvas2DLayerManagerTest.cpp b/Source/web/tests/Canvas2DLayerManagerTest.cpp
index 8607ca5e2c55b6a223bb107214d48981b6853c9d..50400fc5c4778240b00c0972ad62106a2bc920e6 100644
--- a/Source/web/tests/Canvas2DLayerManagerTest.cpp
+++ b/Source/web/tests/Canvas2DLayerManagerTest.cpp
@@ -41,11 +41,22 @@ using testing::InSequence;
using testing::Return;
using testing::Test;
+namespace {
+
+class FakeCanvas2DLayerBridgeClient : public Canvas2DLayerBridgeClient {
+public:
+ FakeCanvas2DLayerBridgeClient() { m_hidden = false; }
+ virtual ~FakeCanvas2DLayerBridgeClient() { }
+ virtual bool hidden() const OVERRIDE { return m_hidden; }
+ void fakeHidden(bool value) { m_hidden = value; }
+private:
+ bool m_hidden;
+};
class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
public:
FakeCanvas2DLayerBridge(PassRefPtr<GraphicsContext3D> context, PassOwnPtr<SkDeferredCanvas> canvas)
- : Canvas2DLayerBridge(context, canvas, 0, NonOpaque)
+ : Canvas2DLayerBridge(&m_client, context, canvas, 0, NonOpaque)
, m_freeableBytes(0)
, m_freeMemoryIfPossibleCount(0)
, m_flushCount(0)
@@ -72,18 +83,19 @@ public:
size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes;
m_freeableBytes -= bytesFreed;
if (bytesFreed)
- Canvas2DLayerManager::get().layerAllocatedStorageChanged(this, -((intptr_t)bytesFreed));
- m_bytesAllocated -= bytesFreed;
+ storageAllocatedForRecordingChanged(m_bytesAllocated - bytesFreed);
return bytesFreed;
}
virtual void flush() OVERRIDE
{
flushedDrawCommands();
+ m_freeableBytes = bytesAllocated();
m_flushCount++;
}
public:
+ FakeCanvas2DLayerBridgeClient m_client;
size_t m_freeableBytes;
int m_freeMemoryIfPossibleCount;
int m_flushCount;
@@ -111,6 +123,8 @@ static PassOwnPtr<SkDeferredCanvas> createCanvas(GraphicsContext3D* context)
return adoptPtr(SkDeferredCanvas::Create(SkSurface::NewRasterPMColor(1, 1)));
}
+} // unnamed namespace
+
class Canvas2DLayerManagerTest : public Test {
protected:
void storageAllocationTrackingTest()
@@ -160,6 +174,40 @@ protected:
EXPECT_EQ((size_t)5, layer->bytesAllocated());
}
+ void hiddenCanvasTest()
+ {
+ RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsContextFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
+ Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
+ manager.init(20, 5);
+ OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get());
+ FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(context, canvas.release())));
+ layer->fakeFreeableBytes(5);
+ layer->storageAllocatedForRecordingChanged(10);
+ manager.didHidePage(); // does nothing because no canvases are hidden
+ EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
+ EXPECT_EQ(0, layer->m_flushCount);
+ layer->m_client.fakeHidden(true);
+ manager.didHidePage();
+ EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
+ EXPECT_EQ((size_t)0, layer->m_freeableBytes);
+ EXPECT_EQ((size_t)0, layer->bytesAllocated());
+ EXPECT_EQ(1, layer->m_flushCount);
+ }
+
+ void addRemoveLayerTest()
+ {
+ RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsContextFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
+ Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
+ manager.init(10, 5);
+ OwnPtr<SkDeferredCanvas> canvas = createCanvas(context.get());
+ FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(context, canvas.release())));
+ EXPECT_FALSE(manager.isInList(layer.get()));
+ layer->storageAllocatedForRecordingChanged(5);
+ EXPECT_TRUE(manager.isInList(layer.get()));
+ layer->storageAllocatedForRecordingChanged(0);
+ EXPECT_FALSE(manager.isInList(layer.get()));
+ }
+
void flushEvictionTest()
{
RefPtr<GraphicsContext3D> context = GraphicsContext3D::createGraphicsContextFromWebContext(adoptPtr(new blink::MockWebGraphicsContext3D));
@@ -172,10 +220,10 @@ protected:
EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
layer->storageAllocatedForRecordingChanged(12); // over the max
EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount); // Two tries, one before flush, one after flush
- EXPECT_EQ((size_t)0, layer->m_freeableBytes);
+ EXPECT_EQ((size_t)5, layer->m_freeableBytes);
EXPECT_EQ(1, layer->m_flushCount); // flush was attempted
- EXPECT_EQ((size_t)11, layer->bytesAllocated()); // flush drops the layer from manager's tracking list
- EXPECT_FALSE(manager.isInList(layer.get()));
+ EXPECT_EQ((size_t)5, layer->bytesAllocated());
+ EXPECT_TRUE(manager.isInList(layer.get()));
}
void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipCommands)
@@ -186,7 +234,6 @@ protected:
EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive);
if (skipCommands) {
layer->willUse();
- layer->storageAllocatedForRecordingChanged(0);
layer->skippedPendingDrawCommands();
}
blink::Platform::current()->currentThread()->exitRunLoop();
@@ -270,5 +317,15 @@ TEST_F(Canvas2DLayerManagerTest, testDeferredFrame)
deferredFrameTest();
}
-} // namespace
+TEST_F(Canvas2DLayerManagerTest, testHiddenCanvas)
+{
+ hiddenCanvasTest();
+}
+
+TEST_F(Canvas2DLayerManagerTest, testAddRemoveLayer)
+{
+ addRemoveLayerTest();
+}
+
+} // unnamed namespace

Powered by Google App Engine
This is Rietveld 408576698