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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
/*
* Copyright (C) 2011 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 "CCThreadedTest.h"
#include "CCAnimationTestCommon.h"
#include "CCOcclusionTrackerTestCommon.h"
#include "CCTiledLayerTestCommon.h"
#include "ContentLayerChromium.h"
#include "FakeWebCompositorOutputSurface.h"
#include "FakeWebGraphicsContext3D.h"
#include "LayerChromium.h"
#include "cc/CCActiveAnimation.h"
#include "cc/CCLayerAnimationController.h"
#include "cc/CCLayerImpl.h"
#include "cc/CCLayerTreeHostImpl.h"
#include "cc/CCScopedThreadProxy.h"
#include "cc/CCSingleThreadProxy.h"
#include "cc/CCTextureUpdateQueue.h"
#include "cc/CCThreadTask.h"
#include "cc/CCTimingFunction.h"
#include <gmock/gmock.h>
#include <public/Platform.h>
#include <public/WebCompositor.h>
#include <public/WebFilterOperation.h>
#include <public/WebFilterOperations.h>
#include <public/WebThread.h>
#include <wtf/Locker.h>
#include <wtf/MainThread.h>
#include <wtf/PassRefPtr.h>
#include <wtf/ThreadingPrimitives.h>
#include <wtf/Vector.h>
using namespace WebCore;
using namespace WebKit;
using namespace WTF;
namespace WebKitTests {
PassOwnPtr<CompositorFakeWebGraphicsContext3DWithTextureTracking> CompositorFakeWebGraphicsContext3DWithTextureTracking::create(Attributes attrs)
{
return adoptPtr(new CompositorFakeWebGraphicsContext3DWithTextureTracking(attrs));
}
WebGLId CompositorFakeWebGraphicsContext3DWithTextureTracking::createTexture()
{
WebGLId texture = m_textures.size() + 1;
m_textures.append(texture);
return texture;
}
void CompositorFakeWebGraphicsContext3DWithTextureTracking::deleteTexture(WebGLId texture)
{
for (size_t i = 0; i < m_textures.size(); i++) {
if (m_textures[i] == texture) {
m_textures.remove(i);
break;
}
}
}
void CompositorFakeWebGraphicsContext3DWithTextureTracking::bindTexture(WGC3Denum /* target */, WebGLId texture)
{
m_usedTextures.add(texture);
}
int CompositorFakeWebGraphicsContext3DWithTextureTracking::numTextures() const { return static_cast<int>(m_textures.size()); }
int CompositorFakeWebGraphicsContext3DWithTextureTracking::texture(int i) const { return m_textures[i]; }
void CompositorFakeWebGraphicsContext3DWithTextureTracking::resetTextures() { m_textures.clear(); }
int CompositorFakeWebGraphicsContext3DWithTextureTracking::numUsedTextures() const { return static_cast<int>(m_usedTextures.size()); }
bool CompositorFakeWebGraphicsContext3DWithTextureTracking::usedTexture(int texture) const { return m_usedTextures.find(texture) != m_usedTextures.end(); }
void CompositorFakeWebGraphicsContext3DWithTextureTracking::resetUsedTextures() { m_usedTextures.clear(); }
CompositorFakeWebGraphicsContext3DWithTextureTracking::CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes attrs) : CompositorFakeWebGraphicsContext3D(attrs)
{
}
PassOwnPtr<WebCompositorOutputSurface> TestHooks::createOutputSurface()
{
return FakeWebCompositorOutputSurface::create(CompositorFakeWebGraphicsContext3DWithTextureTracking::create(WebGraphicsContext3D::Attributes()));
}
PassOwnPtr<MockLayerTreeHostImpl> MockLayerTreeHostImpl::create(TestHooks* testHooks, const CCLayerTreeSettings& settings, CCLayerTreeHostImplClient* client)
{
return adoptPtr(new MockLayerTreeHostImpl(testHooks, settings, client));
}
void MockLayerTreeHostImpl::beginCommit()
{
CCLayerTreeHostImpl::beginCommit();
m_testHooks->beginCommitOnCCThread(this);
}
void MockLayerTreeHostImpl::commitComplete()
{
CCLayerTreeHostImpl::commitComplete();
m_testHooks->commitCompleteOnCCThread(this);
}
bool MockLayerTreeHostImpl::prepareToDraw(FrameData& frame)
{
bool result = CCLayerTreeHostImpl::prepareToDraw(frame);
if (!m_testHooks->prepareToDrawOnCCThread(this))
result = false;
return result;
}
void MockLayerTreeHostImpl::drawLayers(const FrameData& frame)
{
CCLayerTreeHostImpl::drawLayers(frame);
m_testHooks->drawLayersOnCCThread(this);
}
void MockLayerTreeHostImpl::animateLayers(double monotonicTime, double wallClockTime)
{
m_testHooks->willAnimateLayers(this, monotonicTime);
CCLayerTreeHostImpl::animateLayers(monotonicTime, wallClockTime);
m_testHooks->animateLayers(this, monotonicTime);
}
double MockLayerTreeHostImpl::lowFrequencyAnimationInterval() const
{
return 1.0 / 60;
}
MockLayerTreeHostImpl::MockLayerTreeHostImpl(TestHooks* testHooks, const CCLayerTreeSettings& settings, CCLayerTreeHostImplClient* client)
: CCLayerTreeHostImpl(settings, client)
, m_testHooks(testHooks)
{
}
// Adapts CCLayerTreeHost for test. Injects MockLayerTreeHostImpl.
class MockLayerTreeHost : public WebCore::CCLayerTreeHost {
public:
static PassOwnPtr<MockLayerTreeHost> create(TestHooks* testHooks, WebCore::CCLayerTreeHostClient* client, PassRefPtr<WebCore::LayerChromium> rootLayer, const WebCore::CCLayerTreeSettings& settings)
{
OwnPtr<MockLayerTreeHost> layerTreeHost(adoptPtr(new MockLayerTreeHost(testHooks, client, settings)));
bool success = layerTreeHost->initialize();
EXPECT_TRUE(success);
layerTreeHost->setRootLayer(rootLayer);
// LayerTreeHostImpl won't draw if it has 1x1 viewport.
layerTreeHost->setViewportSize(IntSize(1, 1), IntSize(1, 1));
layerTreeHost->rootLayer()->setLayerAnimationDelegate(testHooks);
return layerTreeHost.release();
}
virtual PassOwnPtr<WebCore::CCLayerTreeHostImpl> createLayerTreeHostImpl(WebCore::CCLayerTreeHostImplClient* client)
{
return MockLayerTreeHostImpl::create(m_testHooks, settings(), client);
}
virtual void didAddAnimation() OVERRIDE
{
CCLayerTreeHost::didAddAnimation();
m_testHooks->didAddAnimation();
}
private:
MockLayerTreeHost(TestHooks* testHooks, WebCore::CCLayerTreeHostClient* client, const WebCore::CCLayerTreeSettings& settings)
: CCLayerTreeHost(client, settings)
, m_testHooks(testHooks)
{
}
TestHooks* m_testHooks;
};
// Implementation of CCLayerTreeHost callback interface.
class MockLayerTreeHostClient : public MockCCLayerTreeHostClient {
public:
static PassOwnPtr<MockLayerTreeHostClient> create(TestHooks* testHooks)
{
return adoptPtr(new MockLayerTreeHostClient(testHooks));
}
virtual void willBeginFrame() OVERRIDE
{
}
virtual void didBeginFrame() OVERRIDE
{
}
virtual void updateAnimations(double monotonicTime) OVERRIDE
{
m_testHooks->updateAnimations(monotonicTime);
}
virtual void layout() OVERRIDE
{
m_testHooks->layout();
}
virtual void applyScrollAndScale(const IntSize& scrollDelta, float scale) OVERRIDE
{
m_testHooks->applyScrollAndScale(scrollDelta, scale);
}
virtual PassOwnPtr<WebCompositorOutputSurface> createOutputSurface() OVERRIDE
{
return m_testHooks->createOutputSurface();
}
virtual void willCommit() OVERRIDE
{
}
virtual void didCommit() OVERRIDE
{
m_testHooks->didCommit();
}
virtual void didCommitAndDrawFrame() OVERRIDE
{
m_testHooks->didCommitAndDrawFrame();
}
virtual void didCompleteSwapBuffers() OVERRIDE
{
}
virtual void didRecreateOutputSurface(bool succeeded) OVERRIDE
{
m_testHooks->didRecreateOutputSurface(succeeded);
}
virtual void scheduleComposite() OVERRIDE
{
m_testHooks->scheduleComposite();
}
private:
explicit MockLayerTreeHostClient(TestHooks* testHooks) : m_testHooks(testHooks) { }
TestHooks* m_testHooks;
};
class TimeoutTask : public WebThread::Task {
public:
explicit TimeoutTask(CCThreadedTest* test)
: m_test(test)
{
}
void clearTest()
{
m_test = 0;
}
virtual ~TimeoutTask()
{
if (m_test)
m_test->clearTimeout();
}
virtual void run()
{
if (m_test)
m_test->timeout();
}
private:
CCThreadedTest* m_test;
};
class BeginTask : public WebThread::Task {
public:
explicit BeginTask(CCThreadedTest* test)
: m_test(test)
{
}
virtual ~BeginTask() { }
virtual void run()
{
m_test->doBeginTest();
}
private:
CCThreadedTest* m_test;
};
class EndTestTask : public WebThread::Task {
public:
explicit EndTestTask(CCThreadedTest* test)
: m_test(test)
{
}
virtual ~EndTestTask()
{
if (m_test)
m_test->clearEndTestTask();
}
void clearTest()
{
m_test = 0;
}
virtual void run()
{
if (m_test)
m_test->endTest();
}
private:
CCThreadedTest* m_test;
};
CCThreadedTest::CCThreadedTest()
: m_beginning(false)
, m_endWhenBeginReturns(false)
, m_timedOut(false)
, m_finished(false)
, m_scheduled(false)
, m_started(false)
, m_endTestTask(0)
{ }
void CCThreadedTest::endTest()
{
m_finished = true;
// If we are called from the CCThread, re-call endTest on the main thread.
if (!isMainThread())
m_mainThreadProxy->postTask(createCCThreadTask(this, &CCThreadedTest::endTest));
else {
// For the case where we endTest during beginTest(), set a flag to indicate that
// the test should end the second beginTest regains control.
if (m_beginning)
m_endWhenBeginReturns = true;
else
onEndTest(static_cast<void*>(this));
}
}
void CCThreadedTest::endTestAfterDelay(int delayMilliseconds)
{
// If we are called from the CCThread, re-call endTest on the main thread.
if (!isMainThread())
m_mainThreadProxy->postTask(createCCThreadTask(this, &CCThreadedTest::endTestAfterDelay, delayMilliseconds));
else {
m_endTestTask = new EndTestTask(this);
WebKit::Platform::current()->currentThread()->postDelayedTask(m_endTestTask, delayMilliseconds);
}
}
void CCThreadedTest::postSetNeedsAnimateToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchSetNeedsAnimate, this);
}
void CCThreadedTest::postAddAnimationToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchAddAnimation, this);
}
void CCThreadedTest::postAddInstantAnimationToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchAddInstantAnimation, this);
}
void CCThreadedTest::postSetNeedsCommitToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchSetNeedsCommit, this);
}
void CCThreadedTest::postAcquireLayerTextures()
{
callOnMainThread(CCThreadedTest::dispatchAcquireLayerTextures, this);
}
void CCThreadedTest::postSetNeedsRedrawToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchSetNeedsRedraw, this);
}
void CCThreadedTest::postSetNeedsAnimateAndCommitToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchSetNeedsAnimateAndCommit, this);
}
void CCThreadedTest::postSetVisibleToMainThread(bool visible)
{
callOnMainThread(visible ? CCThreadedTest::dispatchSetVisible : CCThreadedTest::dispatchSetInvisible, this);
}
void CCThreadedTest::postDidAddAnimationToMainThread()
{
callOnMainThread(CCThreadedTest::dispatchDidAddAnimation, this);
}
void CCThreadedTest::doBeginTest()
{
ASSERT(isMainThread());
m_client = MockLayerTreeHostClient::create(this);
RefPtr<LayerChromium> rootLayer = LayerChromium::create();
m_layerTreeHost = MockLayerTreeHost::create(this, m_client.get(), rootLayer, m_settings);
ASSERT_TRUE(m_layerTreeHost);
rootLayer->setLayerTreeHost(m_layerTreeHost.get());
m_layerTreeHost->setSurfaceReady();
m_started = true;
m_beginning = true;
beginTest();
m_beginning = false;
if (m_endWhenBeginReturns)
onEndTest(static_cast<void*>(this));
}
void CCThreadedTest::timeout()
{
m_timedOut = true;
endTest();
}
void CCThreadedTest::scheduleComposite()
{
if (!m_started || m_scheduled || m_finished)
return;
m_scheduled = true;
callOnMainThread(&CCThreadedTest::dispatchComposite, this);
}
void CCThreadedTest::onEndTest(void* self)
{
ASSERT(isMainThread());
WebKit::Platform::current()->currentThread()->exitRunLoop();
}
void CCThreadedTest::dispatchSetNeedsAnimate(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->setNeedsAnimate();
}
void CCThreadedTest::dispatchAddInstantAnimation(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost && test->m_layerTreeHost->rootLayer())
addOpacityTransitionToLayer(*test->m_layerTreeHost->rootLayer(), 0, 0, 0.5, false);
}
void CCThreadedTest::dispatchAddAnimation(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost && test->m_layerTreeHost->rootLayer())
addOpacityTransitionToLayer(*test->m_layerTreeHost->rootLayer(), 10, 0, 0.5, true);
}
void CCThreadedTest::dispatchSetNeedsAnimateAndCommit(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost) {
test->m_layerTreeHost->setNeedsAnimate();
test->m_layerTreeHost->setNeedsCommit();
}
}
void CCThreadedTest::dispatchSetNeedsCommit(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT_TRUE(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->setNeedsCommit();
}
void CCThreadedTest::dispatchAcquireLayerTextures(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT_TRUE(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->acquireLayerTextures();
}
void CCThreadedTest::dispatchSetNeedsRedraw(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT_TRUE(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->setNeedsRedraw();
}
void CCThreadedTest::dispatchSetVisible(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->setVisible(true);
}
void CCThreadedTest::dispatchSetInvisible(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->setVisible(false);
}
void CCThreadedTest::dispatchComposite(void* self)
{
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(isMainThread());
ASSERT(test);
test->m_scheduled = false;
if (test->m_layerTreeHost && !test->m_finished)
test->m_layerTreeHost->composite();
}
void CCThreadedTest::dispatchDidAddAnimation(void* self)
{
ASSERT(isMainThread());
CCThreadedTest* test = static_cast<CCThreadedTest*>(self);
ASSERT(test);
if (test->m_finished)
return;
if (test->m_layerTreeHost)
test->m_layerTreeHost->didAddAnimation();
}
void CCThreadedTest::runTest(bool threaded)
{
// For these tests, we will enable threaded animations.
WebCompositor::setAcceleratedAnimationEnabled(true);
if (threaded) {
m_webThread = adoptPtr(WebKit::Platform::current()->createThread("CCThreadedTest"));
WebCompositor::initialize(m_webThread.get());
} else
WebCompositor::initialize(0);
ASSERT(CCProxy::isMainThread());
m_mainThreadProxy = CCScopedThreadProxy::create(CCProxy::mainThread());
m_beginTask = new BeginTask(this);
WebKit::Platform::current()->currentThread()->postDelayedTask(m_beginTask, 0); // postDelayedTask takes ownership of the task
m_timeoutTask = new TimeoutTask(this);
WebKit::Platform::current()->currentThread()->postDelayedTask(m_timeoutTask, 5000);
WebKit::Platform::current()->currentThread()->enterRunLoop();
if (m_layerTreeHost && m_layerTreeHost->rootLayer())
m_layerTreeHost->rootLayer()->setLayerTreeHost(0);
m_layerTreeHost.clear();
if (m_timeoutTask)
m_timeoutTask->clearTest();
if (m_endTestTask)
m_endTestTask->clearTest();
ASSERT_FALSE(m_layerTreeHost.get());
m_client.clear();
if (m_timedOut) {
FAIL() << "Test timed out";
WebCompositor::shutdown();
return;
}
afterTest();
WebCompositor::shutdown();
}
} // namespace WebKitTests
|