1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#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 blink;
using testing::InSequence;
using testing::Return;
using testing::Test;
namespace {
class MockWebGraphicsContext3DProvider : public WebGraphicsContext3DProvider {
public:
MockWebGraphicsContext3DProvider(WebGraphicsContext3D* context3d)
: m_context3d(context3d) { }
WebGraphicsContext3D* context3d()
{
return m_context3d;
}
GrContext* grContext()
{
return 0;
}
private:
WebGraphicsContext3D* m_context3d;
};
class FakeCanvas2DLayerBridge : public Canvas2DLayerBridge {
public:
FakeCanvas2DLayerBridge(WebGraphicsContext3D* context, PassOwnPtr<SkDeferredCanvas> canvas, PassRefPtr<SkSurface> surface)
: Canvas2DLayerBridge(adoptPtr(new MockWebGraphicsContext3DProvider(context)), canvas, surface, 0, NonOpaque)
, m_freeableBytes(0)
, m_freeMemoryIfPossibleCount(0)
, m_flushCount(0)
{
}
virtual size_t storageAllocatedForRecording() override
{
// Because the fake layer has no canvas to query, just
// return status quo. Allocation changes that would normally be
// initiated by the canvas can be faked by invoking
// storageAllocatedForRecordingChanged directly from the test code.
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;
};
} // unnamed namespace
class Canvas2DLayerManagerTest : public Test {
protected:
void storageAllocationTrackingTest()
{
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 10);
{
OwnPtr<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
RefPtr<SkSurface> surface1 = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas1 = adoptPtr(SkDeferredCanvas::Create(surface1.get()));
FakeCanvas2DLayerBridgePtr layer1(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas1.release(), surface1.release())));
EXPECT_EQ((size_t)0, manager.m_bytesAllocated);
layer1->storageAllocatedForRecordingChanged(1);
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
// Test allocation increase
layer1->storageAllocatedForRecordingChanged(2);
EXPECT_EQ((size_t)2, manager.m_bytesAllocated);
// Test allocation decrease
layer1->storageAllocatedForRecordingChanged(1);
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
{
RefPtr<SkSurface> surface2 = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas2 = adoptPtr(SkDeferredCanvas::Create(surface2.get()));
FakeCanvas2DLayerBridgePtr layer2(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas2.release(), surface2.release())));
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
// verify multi-layer allocation tracking
layer2->storageAllocatedForRecordingChanged(2);
EXPECT_EQ((size_t)3, manager.m_bytesAllocated);
}
// Verify tracking after destruction
EXPECT_EQ((size_t)1, manager.m_bytesAllocated);
}
}
void evictionTest()
{
OwnPtr<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release(), surface.release())));
layer->fakeFreeableBytes(10);
layer->storageAllocatedForRecordingChanged(8); // under the max
EXPECT_EQ(0, layer->m_freeMemoryIfPossibleCount);
layer->storageAllocatedForRecordingChanged(12); // over the max
EXPECT_EQ(1, layer->m_freeMemoryIfPossibleCount);
EXPECT_EQ((size_t)3, layer->m_freeableBytes);
EXPECT_EQ(0, layer->m_flushCount); // eviction succeeded without triggering a flush
EXPECT_EQ((size_t)5, layer->bytesAllocated());
}
void hiddenCanvasTest()
{
OwnPtr<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(20, 5);
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release(), surface.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<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release(), surface.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<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
Canvas2DLayerManager& manager = Canvas2DLayerManager::get();
manager.init(10, 5);
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release(), surface.release())));
layer->fakeFreeableBytes(1); // Not enough freeable bytes, will cause aggressive eviction by flushing
layer->storageAllocatedForRecordingChanged(8); // under the max
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)5, layer->m_freeableBytes);
EXPECT_EQ(1, layer->m_flushCount); // flush was attempted
EXPECT_EQ((size_t)5, layer->bytesAllocated());
EXPECT_TRUE(manager.isInList(layer.get()));
}
void doDeferredFrameTestTask(FakeCanvas2DLayerBridge* layer, bool skipCommands)
{
FloatRect invalidationRect(0, 0, 1, 1);
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
layer->finalizeFrame(invalidationRect);
layer->storageAllocatedForRecordingChanged(1);
EXPECT_TRUE(Canvas2DLayerManager::get().m_taskObserverActive);
if (skipCommands) {
layer->finalizeFrame(invalidationRect);
layer->skippedPendingDrawCommands();
}
Platform::current()->currentThread()->exitRunLoop();
}
class DeferredFrameTestTask : public 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<MockWebGraphicsContext3D> webContext = adoptPtr(new MockWebGraphicsContext3D);
Canvas2DLayerManager::get().init(10, 10);
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(1, 1));
OwnPtr<SkDeferredCanvas> canvas = adoptPtr(SkDeferredCanvas::Create(surface.get()));
FakeCanvas2DLayerBridgePtr layer(adoptRef(new FakeCanvas2DLayerBridge(webContext.get(), canvas.release(), surface.release())));
Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
Platform::current()->currentThread()->enterRunLoop();
// Verify that didProcessTask was called upon completion
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
// Verify that no flush was performed because frame is fresh
EXPECT_EQ(0, layer->m_flushCount);
// Verify that no flushes are triggered as long as frame are fresh
Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(0, layer->m_flushCount);
Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), true));
Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(0, layer->m_flushCount);
// Verify that a flush is triggered when queue is accumulating a multi-frame backlog.
Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
Platform::current()->currentThread()->enterRunLoop();
EXPECT_FALSE(Canvas2DLayerManager::get().m_taskObserverActive);
EXPECT_EQ(1, layer->m_flushCount);
Platform::current()->currentThread()->postTask(new DeferredFrameTestTask(this, layer.get(), false));
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();
}
} // unnamed namespace
|