This source file includes following definitions.
- context3d
- grContext
- m_flushCount
- storageAllocatedForRecording
- fakeFreeableBytes
- freeMemoryIfPossible
- flush
- get
- createCanvas
- storageAllocationTrackingTest
- evictionTest
- hiddenCanvasTest
- addRemoveLayerTest
- flushEvictionTest
- doDeferredFrameTestTask
- run
- deferredFrameTest
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "platform/graphics/Canvas2DLayerManager.h"
#include "SkDevice.h"
#include "SkSurface.h"
#include "platform/graphics/test/MockWebGraphicsContext3D.h"
#include "public/platform/Platform.h"
#include "public/platform/WebGraphicsContext3DProvider.h"
#include "public/platform/WebThread.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace WebCore;
using testing::InSequence;
using testing::Return;
using testing::Test;
namespace {
class MockWebGraphicsContext3DProvider : public blink::WebGraphicsContext3DProvider {
public:
MockWebGraphicsContext3DProvider(blink::WebGraphicsContext3D* context3d)
: m_context3d(context3d) { }
blink::WebGraphicsContext3D* context3d()
{
return m_context3d;
}
GrContext* grContext()
{
return 0;
}
private:
blink::WebGraphicsContext3D* m_context3d;
};
class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
public:
FakeCanvas2DLayerBridge(blink::WebGraphicsContext3D* context, PassOwnPtr<SkDeferredCanvas> canvas)
: Canvas2DLayerBridge(adoptPtr(new MockWebGraphicsContext3DProvider(context)), canvas, 0, NonOpaque)
, m_freeableBytes(0)
, m_freeMemoryIfPossibleCount(0)
, m_flushCount(0)
{
}
virtual size_t storageAllocatedForRecording() OVERRIDE
{
return m_bytesAllocated;
}
void fakeFreeableBytes(size_t size)
{
m_freeableBytes = size;
}
virtual size_t freeMemoryIfPossible(size_t size) OVERRIDE
{
m_freeMemoryIfPossibleCount++;
size_t bytesFreed = size < m_freeableBytes ? size : m_freeableBytes;
m_freeableBytes -= bytesFreed;
if (bytesFreed)
storageAllocatedForRecordingChanged(m_bytesAllocated - bytesFreed);
return bytesFreed;
}
virtual void flush() OVERRIDE
{
flushedDrawCommands();
m_freeableBytes = bytesAllocated();
m_flushCount++;
}
public:
size_t m_freeableBytes;
int m_freeMemoryIfPossibleCount;
int m_flushCount;
};
class FakeCanvas2DLayerBridgePtr {
public:
FakeCanvas2DLayerBridgePtr(PassRefPtr<FakeCanvas2DLayerBridge> layerBridge)
: m_layerBridge(layerBridge) { }
~FakeCanvas2DLayerBridgePtr()
{
m_layerBridge->beginDestruction();
}
FakeCanvas2DLayerBridge* operator->() { return m_layerBridge.get(); }
FakeCanvas2DLayerBridge* get() { return m_layerBridge.get(); }
private:
RefPtr<FakeCanvas2DLayerBridge> m_layerBridge;
};
static PassOwnPtr<SkDeferredCanvas> createCanvas()
{
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterPMColor(1, 1));
return adoptPtr(SkDeferredCanvas::Create(surface.get()));
}
}
class Canvas2DLayerManagerTest : public Test {
protected:
void storageAllocationTrackingTest()
{
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 10);
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
OwnPtr<SkDeferredCanvas> canvas1 = createCanvas();
FakeCanvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas1.release())));
EXPECT_EQ((size_t)0, manager.m_bytesAllocated);
layer1->storageAllocatedForRecordingChanged(1);
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
layer1->storageAllocatedForRecordingChanged(2);
EXPECT_EQ((size_t)2, manager.m_bytesAllocated);
layer1->storageAllocatedForRecordingChanged(1);
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
{
OwnPtr<SkDeferredCanvas> canvas2 = createCanvas();
FakeCanvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas2.release())));
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
layer2->storageAllocatedForRecordingChanged(2);
EXPECT_EQ((size_t)3, manager.m_bytesAllocated);
}
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
}
}
void evictionTest()
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
OwnPtr<SkDeferredCanvas> canvas = createCanvas();
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
layer->fakeFreeableBytes(10);
layer->storageAllocatedForRecordingChanged(8);
EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
layer->storageAllocatedForRecordingChanged(12);
EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
EXPECT_EQ((size_t)3, layer->m_freeableBytes);
EXPECT_EQ(0, layer->m_flushCount);
EXPECT_EQ((size_t)5, layer->bytesAllocated());
}
void hiddenCanvasTest()
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(20, 5);
OwnPtr<SkDeferredCanvas> canvas = createCanvas();
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
layer->fakeFreeableBytes(5);
layer->storageAllocatedForRecordingChanged(10);
EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
EXPECT_EQ(0, layer->m_flushCount);
EXPECT_EQ((size_t)10, layer->bytesAllocated());
layer->setIsHidden(true);
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()
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
OwnPtr<SkDeferredCanvas> canvas = createCanvas();
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), 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()
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
OwnPtr<SkDeferredCanvas> canvas = createCanvas();
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
layer->fakeFreeableBytes(1);
layer->storageAllocatedForRecordingChanged(8);
EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
layer->storageAllocatedForRecordingChanged(12);
EXPECT_EQ(2, layer->m_freeMemoryIfPossibleCount);
EXPECT_EQ((size_t)5, layer->m_freeableBytes);
EXPECT_EQ(1, layer->m_flushCount);
EXPECT_EQ((size_t)5, layer->bytesAllocated());
EXPECT_TRUE(manager.isInList(layer.get()));
}
void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipCommands)
{
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
layer->willUse();
layer->storageAllocatedForRecordingChanged(1);
EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive);
if (skipCommands) {
layer->willUse();
layer->skippedPendingDrawCommands();
}
blink::Platform::current()->currentThread()->exitRunLoop();
}
class DeferredFrameTestTask : public blink::WebThread::Task {
public:
DeferredFrameTestTask(Canvas2DLayerManagerTest* test, FakeCanvas2DLayerBridge* layer, bool skipCommands)
{
m_test = test;
m_layer = layer;
m_skipCommands = skipCommands;
}
virtual void run() OVERRIDE
{
m_test->doDeferredFrameTestTask(m_layer, m_skipCommands);
}
private:
Canvas2DLayerManagerTest* m_test;
FakeCanvas2DLayerBridge* m_layer;
bool m_skipCommands;
};
void deferredFrameTest()
{
OwnPtr<blink::MockWebGraphicsContext3D> webContext = adoptPtr(new blink::MockWebGraphicsContext3D);
Canvas2DLayerManager::get().init(10, 10);
OwnPtr<SkDeferredCanvas> canvas = createCanvas();
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release())));
blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
blink::Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(0, layer->m_flushCount);
blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
blink::Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(0, layer->m_flushCount);
blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
blink::Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(0, layer->m_flushCount);
blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
blink::Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(1, layer->m_flushCount);
blink::Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
blink::Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(2, layer->m_flushCount);
}
};
namespace {
TEST_F(Canvas2DLayerManagerTest, testStorageAllocationTracking)
{
storageAllocationTrackingTest();
}
TEST_F(Canvas2DLayerManagerTest, testEviction)
{
evictionTest();
}
TEST_F(Canvas2DLayerManagerTest, testFlushEviction)
{
flushEvictionTest();
}
TEST_F(Canvas2DLayerManagerTest, testDeferredFrame)
{
deferredFrameTest();
}
TEST_F(Canvas2DLayerManagerTest, testHiddenCanvas)
{
hiddenCanvasTest();
}
TEST_F(Canvas2DLayerManagerTest, testAddRemoveLayer)
{
addRemoveLayerTest();
}
}