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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "platform/graphics/RecordingImageBufferSurface.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/ImageBuffer.h"
#include "platform/graphics/ImageBufferClient.h"
#include "platform/graphics/UnacceleratedImageBufferSurface.h"
#include "public/platform/Platform.h"
#include "public/platform/WebThread.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/RefPtr.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace blink;
using testing::Test;
namespace {
class FakeImageBufferClient : public ImageBufferClient, public WebThread::TaskObserver {
public:
FakeImageBufferClient(ImageBuffer* imageBuffer)
: m_isDirty(false)
, m_imageBuffer(imageBuffer)
, m_frameCount(0)
{ }
virtual ~FakeImageBufferClient() { }
// ImageBufferClient implementation
virtual void notifySurfaceInvalid() override { }
virtual bool isDirty() override { return m_isDirty; };
virtual void didFinalizeFrame() override
{
if (m_isDirty) {
Platform::current()->currentThread()->removeTaskObserver(this);
m_isDirty = false;
}
++m_frameCount;
}
// TaskObserver implementation
virtual void willProcessTask() override { ASSERT_NOT_REACHED(); }
virtual void didProcessTask() override
{
ASSERT_TRUE(m_isDirty);
FloatRect dirtyRect(0, 0, 1, 1);
m_imageBuffer->finalizeFrame(dirtyRect);
ASSERT_FALSE(m_isDirty);
}
virtual void restoreCanvasMatrixClipStack() override { };
void fakeDraw()
{
if (m_isDirty)
return;
m_isDirty = true;
Platform::current()->currentThread()->addTaskObserver(this);
}
int frameCount() { return m_frameCount; }
private:
bool m_isDirty;
ImageBuffer* m_imageBuffer;
int m_frameCount;
};
class MockSurfaceFactory : public RecordingImageBufferFallbackSurfaceFactory {
public:
MockSurfaceFactory() : m_createSurfaceCount(0) { }
virtual PassOwnPtr<ImageBufferSurface> createSurface(const IntSize& size, OpacityMode opacityMode)
{
m_createSurfaceCount++;
return adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode));
}
virtual ~MockSurfaceFactory() { }
int createSurfaceCount() { return m_createSurfaceCount; }
private:
int m_createSurfaceCount;
};
} // unnamed namespace
class RecordingImageBufferSurfaceTest : public Test {
protected:
RecordingImageBufferSurfaceTest()
{
OwnPtr<MockSurfaceFactory> surfaceFactory = adoptPtr(new MockSurfaceFactory());
m_surfaceFactory = surfaceFactory.get();
OwnPtr<RecordingImageBufferSurface> testSurface = adoptPtr(new RecordingImageBufferSurface(IntSize(10, 10), surfaceFactory.release()));
m_testSurface = testSurface.get();
// We create an ImageBuffer in order for the testSurface to be
// properly initialized with a GraphicsContext
m_imageBuffer = ImageBuffer::create(testSurface.release());
EXPECT_FALSE(!m_imageBuffer);
m_fakeImageBufferClient = adoptPtr(new FakeImageBufferClient(m_imageBuffer.get()));
m_imageBuffer->setClient(m_fakeImageBufferClient.get());
}
public:
void testEmptyPicture()
{
m_testSurface->initializeCurrentFrame();
RefPtr<SkPicture> picture = m_testSurface->getPicture();
EXPECT_TRUE((bool)picture.get());
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
}
void testNoFallbackWithClear()
{
m_testSurface->initializeCurrentFrame();
m_testSurface->didClearCanvas();
m_testSurface->getPicture();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
}
void testNonAnimatedCanvasUpdate()
{
m_testSurface->initializeCurrentFrame();
// acquire picture twice to simulate a static canvas: nothing drawn between updates
m_fakeImageBufferClient->fakeDraw();
m_testSurface->getPicture();
m_testSurface->getPicture();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
}
void testAnimatedWithoutClear()
{
m_testSurface->initializeCurrentFrame();
m_fakeImageBufferClient->fakeDraw();
m_testSurface->getPicture();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
EXPECT_EQ(0, m_surfaceFactory->createSurfaceCount());
expectDisplayListEnabled(true); // first frame has an implicit clear
m_fakeImageBufferClient->fakeDraw();
m_testSurface->getPicture();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(false);
}
void testFrameFinalizedByTaskObserver1()
{
m_testSurface->initializeCurrentFrame();
expectDisplayListEnabled(true);
m_testSurface->getPicture();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
m_fakeImageBufferClient->fakeDraw();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
m_testSurface->getPicture();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
m_fakeImageBufferClient->fakeDraw();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
// Display list will be disabled only after exiting the runLoop
}
void testFrameFinalizedByTaskObserver2()
{
EXPECT_EQ(3, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(false);
m_testSurface->getPicture();
EXPECT_EQ(3, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(false);
m_fakeImageBufferClient->fakeDraw();
EXPECT_EQ(3, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(false);
}
void testAnimatedWithClear()
{
m_testSurface->initializeCurrentFrame();
m_testSurface->getPicture();
m_testSurface->didClearCanvas();
m_fakeImageBufferClient->fakeDraw();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
m_testSurface->getPicture();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
// clear after use
m_fakeImageBufferClient->fakeDraw();
m_testSurface->didClearCanvas();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
m_testSurface->getPicture();
EXPECT_EQ(3, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
}
void testClearRect()
{
m_testSurface->initializeCurrentFrame();
m_testSurface->getPicture();
m_imageBuffer->context()->clearRect(FloatRect(FloatPoint(0, 0), FloatSize(m_testSurface->size())));
m_fakeImageBufferClient->fakeDraw();
EXPECT_EQ(1, m_fakeImageBufferClient->frameCount());
m_testSurface->getPicture();
EXPECT_EQ(2, m_fakeImageBufferClient->frameCount());
expectDisplayListEnabled(true);
}
void expectDisplayListEnabled(bool displayListEnabled)
{
EXPECT_EQ(displayListEnabled, (bool)m_testSurface->m_currentFrame.get());
EXPECT_EQ(!displayListEnabled, (bool)m_testSurface->m_fallbackSurface.get());
int expectedSurfaceCreationCount = displayListEnabled ? 0 : 1;
EXPECT_EQ(expectedSurfaceCreationCount, m_surfaceFactory->createSurfaceCount());
}
private:
MockSurfaceFactory* m_surfaceFactory;
RecordingImageBufferSurface* m_testSurface;
OwnPtr<FakeImageBufferClient> m_fakeImageBufferClient;
OwnPtr<ImageBuffer> m_imageBuffer;
};
namespace {
// The following test helper class installs a mock platform that provides a mock WebThread
// for the current thread. The Mock thread is capable of queuing a single non-delayed task
// and registering a single task observer. The run loop exits immediately after running
// the single task.
class AutoInstallCurrentThreadPlatformMock {
public:
AutoInstallCurrentThreadPlatformMock()
{
m_oldPlatform = Platform::current();
Platform::initialize(&m_mockPlatform);
}
~AutoInstallCurrentThreadPlatformMock()
{
Platform::initialize(m_oldPlatform);
}
private:
class CurrentThreadMock : public WebThread {
public:
CurrentThreadMock() : m_taskObserver(0), m_task(0) { }
virtual ~CurrentThreadMock()
{
EXPECT_EQ((Task*)0, m_task);
}
virtual void postTask(Task* task)
{
EXPECT_EQ((Task*)0, m_task);
m_task = task;
}
virtual void postDelayedTask(Task*, long long delayMs) override { ASSERT_NOT_REACHED(); };
virtual bool isCurrentThread() const override { return true; }
virtual PlatformThreadId threadId() const override
{
ASSERT_NOT_REACHED();
return 0;
}
virtual void addTaskObserver(TaskObserver* taskObserver) override
{
EXPECT_EQ((TaskObserver*)0, m_taskObserver);
m_taskObserver = taskObserver;
}
virtual void removeTaskObserver(TaskObserver* taskObserver) override
{
EXPECT_EQ(m_taskObserver, taskObserver);
m_taskObserver = 0;
}
virtual void enterRunLoop() override
{
if (m_taskObserver)
m_taskObserver->willProcessTask();
if (m_task) {
m_task->run();
delete m_task;
m_task = 0;
}
if (m_taskObserver)
m_taskObserver->didProcessTask();
}
virtual void exitRunLoop() override { ASSERT_NOT_REACHED(); }
private:
TaskObserver* m_taskObserver;
Task* m_task;
};
class CurrentThreadPlatformMock : public Platform {
public:
CurrentThreadPlatformMock() { }
virtual void cryptographicallyRandomValues(unsigned char* buffer, size_t length) { ASSERT_NOT_REACHED(); }
virtual WebThread* currentThread() override { return &m_currentThread; }
private:
CurrentThreadMock m_currentThread;
};
CurrentThreadPlatformMock m_mockPlatform;
Platform* m_oldPlatform;
};
#define DEFINE_TEST_TASK_WRAPPER_CLASS(TEST_METHOD) \
class TestWrapperTask_ ## TEST_METHOD : public WebThread::Task { \
public: \
TestWrapperTask_ ## TEST_METHOD(RecordingImageBufferSurfaceTest* test) : m_test(test) { } \
virtual void run() override { m_test->TEST_METHOD(); } \
private: \
RecordingImageBufferSurfaceTest* m_test; \
};
#define CALL_TEST_TASK_WRAPPER(TEST_METHOD) \
{ \
AutoInstallCurrentThreadPlatformMock ctpm; \
Platform::current()->currentThread()->postTask(new TestWrapperTask_ ## TEST_METHOD(this)); \
Platform::current()->currentThread()->enterRunLoop(); \
}
TEST_F(RecordingImageBufferSurfaceTest, testEmptyPicture)
{
testEmptyPicture();
}
TEST_F(RecordingImageBufferSurfaceTest, testNoFallbackWithClear)
{
testNoFallbackWithClear();
}
DEFINE_TEST_TASK_WRAPPER_CLASS(testNonAnimatedCanvasUpdate)
TEST_F(RecordingImageBufferSurfaceTest, testNonAnimatedCanvasUpdate)
{
CALL_TEST_TASK_WRAPPER(testNonAnimatedCanvasUpdate)
expectDisplayListEnabled(true);
}
DEFINE_TEST_TASK_WRAPPER_CLASS(testAnimatedWithoutClear)
TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithoutClear)
{
CALL_TEST_TASK_WRAPPER(testAnimatedWithoutClear)
expectDisplayListEnabled(false);
}
DEFINE_TEST_TASK_WRAPPER_CLASS(testFrameFinalizedByTaskObserver1)
DEFINE_TEST_TASK_WRAPPER_CLASS(testFrameFinalizedByTaskObserver2)
TEST_F(RecordingImageBufferSurfaceTest, testFrameFinalizedByTaskObserver)
{
CALL_TEST_TASK_WRAPPER(testFrameFinalizedByTaskObserver1)
expectDisplayListEnabled(false);
CALL_TEST_TASK_WRAPPER(testFrameFinalizedByTaskObserver2)
expectDisplayListEnabled(false);
}
DEFINE_TEST_TASK_WRAPPER_CLASS(testAnimatedWithClear)
TEST_F(RecordingImageBufferSurfaceTest, testAnimatedWithClear)
{
CALL_TEST_TASK_WRAPPER(testAnimatedWithClear)
expectDisplayListEnabled(true);
}
DEFINE_TEST_TASK_WRAPPER_CLASS(testClearRect)
TEST_F(RecordingImageBufferSurfaceTest, testClearRect)
{
CALL_TEST_TASK_WRAPPER(testClearRect);
expectDisplayListEnabled(true);
}
} // namespace
|