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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
|
// Copyright 2011 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 "cc/thread_proxy.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "cc/delay_based_time_source.h"
#include "cc/draw_quad.h"
#include "cc/frame_rate_controller.h"
#include "cc/graphics_context.h"
#include "cc/input_handler.h"
#include "cc/layer_tree_host.h"
#include "cc/scheduler.h"
#include "cc/scoped_thread_proxy.h"
#include <public/WebSharedGraphicsContext3D.h>
using WebKit::WebSharedGraphicsContext3D;
namespace {
// Measured in seconds.
const double contextRecreationTickRate = 0.03;
} // namespace
namespace cc {
scoped_ptr<Proxy> ThreadProxy::create(LayerTreeHost* layerTreeHost)
{
return make_scoped_ptr(new ThreadProxy(layerTreeHost)).PassAs<Proxy>();
}
ThreadProxy::ThreadProxy(LayerTreeHost* layerTreeHost)
: m_animateRequested(false)
, m_commitRequested(false)
, m_commitRequestSentToImplThread(false)
, m_forcedCommitRequested(false)
, m_layerTreeHost(layerTreeHost)
, m_rendererInitialized(false)
, m_started(false)
, m_texturesAcquired(true)
, m_inCompositeAndReadback(false)
, m_mainThreadProxy(ScopedThreadProxy::create(Proxy::mainThread()))
, m_beginFrameCompletionEventOnImplThread(0)
, m_readbackRequestOnImplThread(0)
, m_commitCompletionEventOnImplThread(0)
, m_textureAcquisitionCompletionEventOnImplThread(0)
, m_nextFrameIsNewlyCommittedFrameOnImplThread(false)
, m_renderVSyncEnabled(layerTreeHost->settings().renderVSyncEnabled)
, m_totalCommitCount(0)
, m_deferCommits(false)
, m_deferredCommitPending(false)
{
TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy");
DCHECK(isMainThread());
}
ThreadProxy::~ThreadProxy()
{
TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy");
DCHECK(isMainThread());
DCHECK(!m_started);
}
bool ThreadProxy::compositeAndReadback(void *pixels, const gfx::Rect& rect)
{
TRACE_EVENT0("cc", "ThreadPRoxy::compositeAndReadback");
DCHECK(isMainThread());
DCHECK(m_layerTreeHost);
DCHECK(!m_deferCommits);
if (!m_layerTreeHost->initializeRendererIfNeeded()) {
TRACE_EVENT0("cc", "compositeAndReadback_EarlyOut_LR_Uninitialized");
return false;
}
// Perform a synchronous commit.
{
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent beginFrameCompletion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::forceBeginFrameOnImplThread, base::Unretained(this), &beginFrameCompletion));
beginFrameCompletion.wait();
}
m_inCompositeAndReadback = true;
beginFrame();
m_inCompositeAndReadback = false;
// Perform a synchronous readback.
ReadbackRequest request;
request.rect = rect;
request.pixels = pixels;
{
DebugScopedSetMainThreadBlocked mainThreadBlocked;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::requestReadbackOnImplThread, base::Unretained(this), &request));
request.completion.wait();
}
return request.success;
}
void ThreadProxy::requestReadbackOnImplThread(ReadbackRequest* request)
{
DCHECK(Proxy::isImplThread());
DCHECK(!m_readbackRequestOnImplThread);
if (!m_layerTreeHostImpl.get()) {
request->success = false;
request->completion.signal();
return;
}
m_readbackRequestOnImplThread = request;
m_schedulerOnImplThread->setNeedsRedraw();
m_schedulerOnImplThread->setNeedsForcedRedraw();
}
void ThreadProxy::startPageScaleAnimation(const IntSize& targetPosition, bool useAnchor, float scale, base::TimeDelta duration)
{
DCHECK(Proxy::isMainThread());
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::requestStartPageScaleAnimationOnImplThread, base::Unretained(this), targetPosition, useAnchor, scale, duration));
}
void ThreadProxy::requestStartPageScaleAnimationOnImplThread(IntSize targetPosition, bool useAnchor, float scale, base::TimeDelta duration)
{
DCHECK(Proxy::isImplThread());
if (m_layerTreeHostImpl.get())
m_layerTreeHostImpl->startPageScaleAnimation(targetPosition, useAnchor, scale, base::TimeTicks::Now(), duration);
}
void ThreadProxy::finishAllRendering()
{
DCHECK(Proxy::isMainThread());
DCHECK(!m_deferCommits);
// Make sure all GL drawing is finished on the impl thread.
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::finishAllRenderingOnImplThread, base::Unretained(this), &completion));
completion.wait();
}
bool ThreadProxy::isStarted() const
{
DCHECK(Proxy::isMainThread());
return m_started;
}
bool ThreadProxy::initializeContext()
{
TRACE_EVENT0("cc", "ThreadProxy::initializeContext");
scoped_ptr<GraphicsContext> context = m_layerTreeHost->createContext();
if (!context.get())
return false;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::initializeContextOnImplThread, base::Unretained(this), base::Passed(context.Pass())));
return true;
}
void ThreadProxy::setSurfaceReady()
{
TRACE_EVENT0("cc", "ThreadProxy::setSurfaceReady");
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setSurfaceReadyOnImplThread, base::Unretained(this)));
}
void ThreadProxy::setSurfaceReadyOnImplThread()
{
TRACE_EVENT0("cc", "ThreadProxy::setSurfaceReadyOnImplThread");
m_schedulerOnImplThread->setCanBeginFrame(true);
}
void ThreadProxy::setVisible(bool visible)
{
TRACE_EVENT0("cc", "ThreadProxy::setVisible");
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setVisibleOnImplThread, base::Unretained(this), &completion, visible));
completion.wait();
}
void ThreadProxy::setVisibleOnImplThread(CompletionEvent* completion, bool visible)
{
TRACE_EVENT0("cc", "ThreadProxy::setVisibleOnImplThread");
m_layerTreeHostImpl->setVisible(visible);
m_schedulerOnImplThread->setVisible(visible);
completion->signal();
}
bool ThreadProxy::initializeRenderer()
{
TRACE_EVENT0("cc", "ThreadProxy::initializeRenderer");
// Make a blocking call to initializeRendererOnImplThread. The results of that call
// are pushed into the initializeSucceeded and capabilities local variables.
CompletionEvent completion;
bool initializeSucceeded = false;
RendererCapabilities capabilities;
DebugScopedSetMainThreadBlocked mainThreadBlocked;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::initializeRendererOnImplThread,
base::Unretained(this),
&completion,
&initializeSucceeded,
&capabilities));
completion.wait();
if (initializeSucceeded) {
m_rendererInitialized = true;
m_RendererCapabilitiesMainThreadCopy = capabilities;
}
return initializeSucceeded;
}
bool ThreadProxy::recreateContext()
{
TRACE_EVENT0("cc", "ThreadProxy::recreateContext");
DCHECK(isMainThread());
// Try to create the context.
scoped_ptr<GraphicsContext> context = m_layerTreeHost->createContext();
if (!context.get())
return false;
if (m_layerTreeHost->needsSharedContext())
if (!WebSharedGraphicsContext3D::createCompositorThreadContext())
return false;
// Make a blocking call to recreateContextOnImplThread. The results of that
// call are pushed into the recreateSucceeded and capabilities local
// variables.
CompletionEvent completion;
bool recreateSucceeded = false;
RendererCapabilities capabilities;
DebugScopedSetMainThreadBlocked mainThreadBlocked;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::recreateContextOnImplThread,
base::Unretained(this),
&completion,
base::Passed(context.Pass()),
&recreateSucceeded,
&capabilities));
completion.wait();
if (recreateSucceeded)
m_RendererCapabilitiesMainThreadCopy = capabilities;
return recreateSucceeded;
}
void ThreadProxy::renderingStats(RenderingStats* stats)
{
DCHECK(isMainThread());
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::renderingStatsOnImplThread,
base::Unretained(this), &completion, stats));
stats->totalCommitTimeInSeconds = m_totalCommitTime.InSecondsF();
stats->totalCommitCount = m_totalCommitCount;
completion.wait();
}
const RendererCapabilities& ThreadProxy::rendererCapabilities() const
{
DCHECK(m_rendererInitialized);
return m_RendererCapabilitiesMainThreadCopy;
}
void ThreadProxy::loseContext()
{
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::didLoseContextOnImplThread, base::Unretained(this)));
}
void ThreadProxy::setNeedsAnimate()
{
DCHECK(isMainThread());
if (m_animateRequested)
return;
TRACE_EVENT0("cc", "ThreadProxy::setNeedsAnimate");
m_animateRequested = true;
if (m_commitRequestSentToImplThread)
return;
m_commitRequestSentToImplThread = true;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setNeedsCommitOnImplThread, base::Unretained(this)));
}
void ThreadProxy::setNeedsCommit()
{
DCHECK(isMainThread());
if (m_commitRequested)
return;
TRACE_EVENT0("cc", "ThreadProxy::setNeedsCommit");
m_commitRequested = true;
if (m_commitRequestSentToImplThread)
return;
m_commitRequestSentToImplThread = true;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setNeedsCommitOnImplThread, base::Unretained(this)));
}
void ThreadProxy::didLoseContextOnImplThread()
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::didLoseContextOnImplThread");
m_schedulerOnImplThread->didLoseContext();
}
void ThreadProxy::onSwapBuffersCompleteOnImplThread()
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::onSwapBuffersCompleteOnImplThread");
m_schedulerOnImplThread->didSwapBuffersComplete();
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::didCompleteSwapBuffers, base::Unretained(this)));
}
void ThreadProxy::onVSyncParametersChanged(base::TimeTicks timebase, base::TimeDelta interval)
{
DCHECK(isImplThread());
TRACE_EVENT2("cc", "ThreadProxy::onVSyncParametersChanged", "timebase", (timebase - base::TimeTicks()).InMilliseconds(), "interval", interval.InMilliseconds());
m_schedulerOnImplThread->setTimebaseAndInterval(timebase, interval);
}
void ThreadProxy::onCanDrawStateChanged(bool canDraw)
{
DCHECK(isImplThread());
TRACE_EVENT1("cc", "ThreadProxy::onCanDrawStateChanged", "canDraw", canDraw);
m_schedulerOnImplThread->setCanDraw(canDraw);
}
void ThreadProxy::setNeedsCommitOnImplThread()
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::setNeedsCommitOnImplThread");
m_schedulerOnImplThread->setNeedsCommit();
}
void ThreadProxy::setNeedsForcedCommitOnImplThread()
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::setNeedsForcedCommitOnImplThread");
m_schedulerOnImplThread->setNeedsCommit();
m_schedulerOnImplThread->setNeedsForcedCommit();
}
void ThreadProxy::postAnimationEventsToMainThreadOnImplThread(scoped_ptr<AnimationEventsVector> events, base::Time wallClockTime)
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::postAnimationEventsToMainThreadOnImplThread");
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::setAnimationEvents, base::Unretained(this), base::Passed(events.Pass()), wallClockTime));
}
bool ThreadProxy::reduceContentsTextureMemoryOnImplThread(size_t limitBytes, int priorityCutoff)
{
DCHECK(isImplThread());
if (!m_layerTreeHost->contentsTextureManager())
return false;
ResourceProvider::debugNotifyEnterZone(0x1000000);
bool reduceResult = m_layerTreeHost->contentsTextureManager()->reduceMemoryOnImplThread(limitBytes, priorityCutoff, m_layerTreeHostImpl->resourceProvider());
ResourceProvider::debugNotifyLeaveZone();
if (!reduceResult)
return false;
// The texture upload queue may reference textures that were just purged, clear
// them from the queue.
if (m_currentResourceUpdateControllerOnImplThread.get())
m_currentResourceUpdateControllerOnImplThread->discardUploadsToEvictedResources();
return true;
}
void ThreadProxy::sendManagedMemoryStats()
{
DCHECK(isImplThread());
if (!m_layerTreeHostImpl.get())
return;
if (!m_layerTreeHostImpl->renderer())
return;
if (!m_layerTreeHost->contentsTextureManager())
return;
m_layerTreeHostImpl->renderer()->sendManagedMemoryStats(
m_layerTreeHost->contentsTextureManager()->memoryVisibleBytes(),
m_layerTreeHost->contentsTextureManager()->memoryVisibleAndNearbyBytes(),
m_layerTreeHost->contentsTextureManager()->memoryUseBytes());
}
void ThreadProxy::setNeedsRedraw()
{
DCHECK(isMainThread());
TRACE_EVENT0("cc", "ThreadProxy::setNeedsRedraw");
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setFullRootLayerDamageOnImplThread, base::Unretained(this)));
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::setNeedsRedrawOnImplThread, base::Unretained(this)));
}
void ThreadProxy::setDeferCommits(bool deferCommits)
{
DCHECK(isMainThread());
DCHECK_NE(m_deferCommits, deferCommits);
m_deferCommits = deferCommits;
if (m_deferCommits)
TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::setDeferCommits", this);
else
TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::setDeferCommits", this);
if (!m_deferCommits && m_deferredCommitPending) {
m_deferredCommitPending = false;
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::beginFrame, base::Unretained(this)));
}
}
bool ThreadProxy::commitRequested() const
{
DCHECK(isMainThread());
return m_commitRequested;
}
void ThreadProxy::setNeedsRedrawOnImplThread()
{
DCHECK(isImplThread());
TRACE_EVENT0("cc", "ThreadProxy::setNeedsRedrawOnImplThread");
m_schedulerOnImplThread->setNeedsRedraw();
}
void ThreadProxy::start()
{
DCHECK(isMainThread());
DCHECK(Proxy::implThread());
// Create LayerTreeHostImpl.
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
scoped_ptr<InputHandler> handler = m_layerTreeHost->createInputHandler();
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::initializeImplOnImplThread, base::Unretained(this), &completion, handler.release()));
completion.wait();
m_started = true;
}
void ThreadProxy::stop()
{
TRACE_EVENT0("cc", "ThreadProxy::stop");
DCHECK(isMainThread());
DCHECK(m_started);
// Synchronously deletes the impl.
{
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::layerTreeHostClosedOnImplThread, base::Unretained(this), &completion));
completion.wait();
}
m_mainThreadProxy->shutdown(); // Stop running tasks posted to us.
DCHECK(!m_layerTreeHostImpl.get()); // verify that the impl deleted.
m_layerTreeHost = 0;
m_started = false;
}
void ThreadProxy::forceSerializeOnSwapBuffers()
{
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::forceSerializeOnSwapBuffersOnImplThread, base::Unretained(this), &completion));
completion.wait();
}
void ThreadProxy::forceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion)
{
if (m_rendererInitialized)
m_layerTreeHostImpl->renderer()->doNoOp();
completion->signal();
}
void ThreadProxy::finishAllRenderingOnImplThread(CompletionEvent* completion)
{
TRACE_EVENT0("cc", "ThreadProxy::finishAllRenderingOnImplThread");
DCHECK(isImplThread());
m_layerTreeHostImpl->finishAllRendering();
completion->signal();
}
void ThreadProxy::forceBeginFrameOnImplThread(CompletionEvent* completion)
{
TRACE_EVENT0("cc", "ThreadProxy::forceBeginFrameOnImplThread");
DCHECK(!m_beginFrameCompletionEventOnImplThread);
if (m_schedulerOnImplThread->commitPending()) {
completion->signal();
return;
}
m_beginFrameCompletionEventOnImplThread = completion;
setNeedsForcedCommitOnImplThread();
}
void ThreadProxy::scheduledActionBeginFrame()
{
TRACE_EVENT0("cc", "ThreadProxy::scheduledActionBeginFrame");
DCHECK(!m_pendingBeginFrameRequest);
m_pendingBeginFrameRequest = make_scoped_ptr(new BeginFrameAndCommitState());
m_pendingBeginFrameRequest->monotonicFrameBeginTime = base::TimeTicks::Now();
m_pendingBeginFrameRequest->scrollInfo = m_layerTreeHostImpl->processScrollDeltas();
m_pendingBeginFrameRequest->implTransform = m_layerTreeHostImpl->implTransform();
m_pendingBeginFrameRequest->memoryAllocationLimitBytes = m_layerTreeHostImpl->memoryAllocationLimitBytes();
if (m_layerTreeHost->contentsTextureManager())
m_layerTreeHost->contentsTextureManager()->getEvictedBackings(m_pendingBeginFrameRequest->evictedContentsTexturesBackings);
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::beginFrame, base::Unretained(this)));
if (m_beginFrameCompletionEventOnImplThread) {
m_beginFrameCompletionEventOnImplThread->signal();
m_beginFrameCompletionEventOnImplThread = 0;
}
}
void ThreadProxy::beginFrame()
{
TRACE_EVENT0("cc", "ThreadProxy::beginFrame");
DCHECK(isMainThread());
if (!m_layerTreeHost)
return;
if (m_deferCommits) {
m_deferredCommitPending = true;
m_layerTreeHost->didDeferCommit();
TRACE_EVENT0("cc", "EarlyOut_DeferCommits");
return;
}
if (!m_pendingBeginFrameRequest) {
TRACE_EVENT0("cc", "EarlyOut_StaleBeginFrameMessage");
return;
}
if (m_layerTreeHost->needsSharedContext() && !WebSharedGraphicsContext3D::haveCompositorThreadContext())
WebSharedGraphicsContext3D::createCompositorThreadContext();
scoped_ptr<BeginFrameAndCommitState> request(m_pendingBeginFrameRequest.Pass());
// Do not notify the impl thread of commit requests that occur during
// the apply/animate/layout part of the beginFrameAndCommit process since
// those commit requests will get painted immediately. Once we have done
// the paint, m_commitRequested will be set to false to allow new commit
// requests to be scheduled.
m_commitRequested = true;
m_commitRequestSentToImplThread = true;
// On the other hand, the animationRequested flag needs to be cleared
// here so that any animation requests generated by the apply or animate
// callbacks will trigger another frame.
m_animateRequested = false;
// FIXME: technically, scroll deltas need to be applied for dropped commits as well.
// Re-do the commit flow so that we don't send the scrollInfo on the BFAC message.
m_layerTreeHost->applyScrollAndScale(*request->scrollInfo);
m_layerTreeHost->setImplTransform(request->implTransform);
if (!m_inCompositeAndReadback && !m_layerTreeHost->visible()) {
m_commitRequested = false;
m_commitRequestSentToImplThread = false;
m_forcedCommitRequested = false;
TRACE_EVENT0("cc", "EarlyOut_NotVisible");
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::beginFrameAbortedOnImplThread, base::Unretained(this)));
return;
}
m_layerTreeHost->willBeginFrame();
m_layerTreeHost->updateAnimations(request->monotonicFrameBeginTime);
m_layerTreeHost->layout();
// Clear the commit flag after updating animations and layout here --- objects that only
// layout when painted will trigger another setNeedsCommit inside
// updateLayers.
m_commitRequested = false;
m_commitRequestSentToImplThread = false;
m_forcedCommitRequested = false;
if (!m_layerTreeHost->initializeRendererIfNeeded()) {
TRACE_EVENT0("cc", "EarlyOut_InitializeFailed");
return;
}
m_layerTreeHost->contentsTextureManager()->unlinkEvictedBackings(request->evictedContentsTexturesBackings);
scoped_ptr<ResourceUpdateQueue> queue = make_scoped_ptr(new ResourceUpdateQueue);
m_layerTreeHost->updateLayers(*(queue.get()), request->memoryAllocationLimitBytes);
// Once single buffered layers are committed, they cannot be modified until
// they are drawn by the impl thread.
m_texturesAcquired = false;
m_layerTreeHost->willCommit();
// Before applying scrolls and calling animate, we set m_animateRequested to
// false. If it is true now, it means setNeedAnimate was called again, but
// during a state when m_commitRequestSentToImplThread = true. We need to
// force that call to happen again now so that the commit request is sent to
// the impl thread.
if (m_animateRequested) {
// Forces setNeedsAnimate to consider posting a commit task.
m_animateRequested = false;
setNeedsAnimate();
}
// Notify the impl thread that the beginFrame has completed. This will
// begin the commit process, which is blocking from the main thread's
// point of view, but asynchronously performed on the impl thread,
// coordinated by the Scheduler.
{
TRACE_EVENT0("cc", "commit");
DebugScopedSetMainThreadBlocked mainThreadBlocked;
base::TimeTicks startTime = base::TimeTicks::HighResNow();
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::beginFrameCompleteOnImplThread, base::Unretained(this), &completion, queue.release()));
completion.wait();
base::TimeTicks endTime = base::TimeTicks::HighResNow();
m_totalCommitTime += endTime - startTime;
m_totalCommitCount++;
}
m_layerTreeHost->commitComplete();
m_layerTreeHost->didBeginFrame();
}
void ThreadProxy::beginFrameCompleteOnImplThread(CompletionEvent* completion, ResourceUpdateQueue* rawQueue)
{
scoped_ptr<ResourceUpdateQueue> queue(rawQueue);
TRACE_EVENT0("cc", "ThreadProxy::beginFrameCompleteOnImplThread");
DCHECK(!m_commitCompletionEventOnImplThread);
DCHECK(isImplThread() && isMainThreadBlocked());
DCHECK(m_schedulerOnImplThread);
DCHECK(m_schedulerOnImplThread->commitPending());
if (!m_layerTreeHostImpl.get()) {
TRACE_EVENT0("cc", "EarlyOut_NoLayerTree");
completion->signal();
return;
}
if (m_layerTreeHost->contentsTextureManager()->linkedEvictedBackingsExist()) {
// Clear any uploads we were making to textures linked to evicted
// resources
queue->clearUploadsToEvictedResources();
// Some textures in the layer tree are invalid. Kick off another commit
// to fill them again.
setNeedsCommitOnImplThread();
}
m_layerTreeHost->contentsTextureManager()->pushTexturePrioritiesToBackings();
m_currentResourceUpdateControllerOnImplThread = ResourceUpdateController::create(this, Proxy::implThread(), queue.Pass(), m_layerTreeHostImpl->resourceProvider());
ResourceProvider::debugNotifyEnterZone(0x2000000);
m_currentResourceUpdateControllerOnImplThread->performMoreUpdates(
m_schedulerOnImplThread->anticipatedDrawTime());
ResourceProvider::debugNotifyLeaveZone();
m_commitCompletionEventOnImplThread = completion;
}
void ThreadProxy::beginFrameAbortedOnImplThread()
{
TRACE_EVENT0("cc", "ThreadProxy::beginFrameAbortedOnImplThread");
DCHECK(isImplThread());
DCHECK(m_schedulerOnImplThread);
DCHECK(m_schedulerOnImplThread->commitPending());
m_schedulerOnImplThread->beginFrameAborted();
}
void ThreadProxy::scheduledActionCommit()
{
TRACE_EVENT0("cc", "ThreadProxy::scheduledActionCommit");
DCHECK(isImplThread());
DCHECK(m_commitCompletionEventOnImplThread);
DCHECK(m_currentResourceUpdateControllerOnImplThread);
// Complete all remaining texture updates.
ResourceProvider::debugNotifyEnterZone(0x3000000);
m_currentResourceUpdateControllerOnImplThread->finalize();
ResourceProvider::debugNotifyLeaveZone();
m_currentResourceUpdateControllerOnImplThread.reset();
// If there are linked evicted backings, these backings' resources may be put into the
// impl tree, so we can't draw yet. Determine this before clearing all evicted backings.
bool newImplTreeHasNoEvictedResources = !m_layerTreeHost->contentsTextureManager()->linkedEvictedBackingsExist();
m_layerTreeHostImpl->beginCommit();
m_layerTreeHost->beginCommitOnImplThread(m_layerTreeHostImpl.get());
m_layerTreeHost->finishCommitOnImplThread(m_layerTreeHostImpl.get());
if (newImplTreeHasNoEvictedResources) {
if (m_layerTreeHostImpl->contentsTexturesPurged())
m_layerTreeHostImpl->resetContentsTexturesPurged();
}
m_layerTreeHostImpl->commitComplete();
m_nextFrameIsNewlyCommittedFrameOnImplThread = true;
m_commitCompletionEventOnImplThread->signal();
m_commitCompletionEventOnImplThread = 0;
// SetVisible kicks off the next scheduler action, so this must be last.
m_schedulerOnImplThread->setVisible(m_layerTreeHostImpl->visible());
}
void ThreadProxy::scheduledActionBeginContextRecreation()
{
DCHECK(isImplThread());
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::beginContextRecreation, base::Unretained(this)));
}
ScheduledActionDrawAndSwapResult ThreadProxy::scheduledActionDrawAndSwapInternal(bool forcedDraw)
{
TRACE_EVENT0("cc", "ThreadProxy::scheduledActionDrawAndSwap");
ScheduledActionDrawAndSwapResult result;
result.didDraw = false;
result.didSwap = false;
DCHECK(isImplThread());
DCHECK(m_layerTreeHostImpl.get());
if (!m_layerTreeHostImpl.get())
return result;
DCHECK(m_layerTreeHostImpl->renderer());
if (!m_layerTreeHostImpl->renderer())
return result;
// FIXME: compute the frame display time more intelligently
base::TimeTicks monotonicTime = base::TimeTicks::Now();
base::Time wallClockTime = base::Time::Now();
if (m_inputHandlerOnImplThread.get())
m_inputHandlerOnImplThread->animate(monotonicTime);
m_layerTreeHostImpl->animate(monotonicTime, wallClockTime);
// This method is called on a forced draw, regardless of whether we are able to produce a frame,
// as the calling site on main thread is blocked until its request completes, and we signal
// completion here. If canDraw() is false, we will indicate success=false to the caller, but we
// must still signal completion to avoid deadlock.
// We guard prepareToDraw() with canDraw() because it always returns a valid frame, so can only
// be used when such a frame is possible. Since drawLayers() depends on the result of
// prepareToDraw(), it is guarded on canDraw() as well.
LayerTreeHostImpl::FrameData frame;
ResourceProvider::debugNotifyEnterZone(0x4000000);
bool drawFrame = m_layerTreeHostImpl->canDraw() && (m_layerTreeHostImpl->prepareToDraw(frame) || forcedDraw);
ResourceProvider::debugNotifyLeaveZone();
if (drawFrame) {
m_layerTreeHostImpl->drawLayers(frame);
result.didDraw = true;
}
m_layerTreeHostImpl->didDrawAllLayers(frame);
// Check for a pending compositeAndReadback.
if (m_readbackRequestOnImplThread) {
m_readbackRequestOnImplThread->success = false;
if (drawFrame) {
m_layerTreeHostImpl->readback(m_readbackRequestOnImplThread->pixels, m_readbackRequestOnImplThread->rect);
m_readbackRequestOnImplThread->success = !m_layerTreeHostImpl->isContextLost();
}
m_readbackRequestOnImplThread->completion.signal();
m_readbackRequestOnImplThread = 0;
} else if (drawFrame)
result.didSwap = m_layerTreeHostImpl->swapBuffers();
// Tell the main thread that the the newly-commited frame was drawn.
if (m_nextFrameIsNewlyCommittedFrameOnImplThread) {
m_nextFrameIsNewlyCommittedFrameOnImplThread = false;
m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadProxy::didCommitAndDrawFrame, base::Unretained(this)));
}
return result;
}
void ThreadProxy::acquireLayerTextures()
{
// Called when the main thread needs to modify a layer texture that is used
// directly by the compositor.
// This method will block until the next compositor draw if there is a
// previously committed frame that is still undrawn. This is necessary to
// ensure that the main thread does not monopolize access to the textures.
DCHECK(isMainThread());
if (m_texturesAcquired)
return;
TRACE_EVENT0("cc", "ThreadProxy::acquireLayerTextures");
DebugScopedSetMainThreadBlocked mainThreadBlocked;
CompletionEvent completion;
Proxy::implThread()->postTask(base::Bind(&ThreadProxy::acquireLayerTexturesForMainThreadOnImplThread, base::Unretained(this), &completion));
completion.wait(); // Block until it is safe to write to layer textures from the main thread.
m_texturesAcquired = true;
}
void ThreadProxy::acquireLayerTexturesForMainThreadOnImplThread(CompletionEvent* completion)
{
DCHECK(isImplThread());
DCHECK(!m_textureAcquisitionCompletionEventOnImplThread);
m_textureAcquisitionCompletionEventOnImplThread = completion;
m_schedulerOnImplThread->setMainThreadNeedsLayerTextures();
}
void ThreadProxy::scheduledActionAcquireLayerTexturesForMainThread()
{
DCHECK(m_textureAcquisitionCompletionEventOnImplThread);
m_textureAcquisitionCompletionEventOnImplThread->signal();
m_textureAcquisitionCompletionEventOnImplThread = 0;
}
ScheduledActionDrawAndSwapResult ThreadProxy::scheduledActionDrawAndSwapIfPossible()
{
return scheduledActionDrawAndSwapInternal(false);
}
ScheduledActionDrawAndSwapResult ThreadProxy::scheduledActionDrawAndSwapForced()
{
return scheduledActionDrawAndSwapInternal(true);
}
void ThreadProxy::didAnticipatedDrawTimeChange(base::TimeTicks time)
{
if (!m_currentResourceUpdateControllerOnImplThread)
return;
ResourceProvider::debugNotifyEnterZone(0x5000000);
m_currentResourceUpdateControllerOnImplThread->performMoreUpdates(time);
ResourceProvider::debugNotifyLeaveZone();
}
void ThreadProxy::readyToFinalizeTextureUpdates()
{
DCHECK(isImplThread());
m_schedulerOnImplThread->beginFrameComplete();
}
void ThreadProxy::didCommitAndDrawFrame()
{
DCHECK(isMainThread());
if (!m_layerTreeHost)
return;
m_layerTreeHost->didCommitAndDrawFrame();
}
void ThreadProxy::didCompleteSwapBuffers()
{
DCHECK(isMainThread());
if (!m_layerTreeHost)
return;
m_layerTreeHost->didCompleteSwapBuffers();
}
void ThreadProxy::setAnimationEvents(scoped_ptr<AnimationEventsVector> events, base::Time wallClockTime)
{
TRACE_EVENT0("cc", "ThreadProxy::setAnimationEvents");
DCHECK(isMainThread());
if (!m_layerTreeHost)
return;
m_layerTreeHost->setAnimationEvents(events.Pass(), wallClockTime);
}
void ThreadProxy::beginContextRecreation()
{
TRACE_EVENT0("cc", "ThreadProxy::beginContextRecreation");
DCHECK(isMainThread());
m_layerTreeHost->didLoseContext();
m_contextRecreationCallback.Reset(base::Bind(&ThreadProxy::tryToRecreateContext, base::Unretained(this)));
Proxy::mainThread()->postTask(m_contextRecreationCallback.callback());
}
void ThreadProxy::tryToRecreateContext()
{
DCHECK(isMainThread());
DCHECK(m_layerTreeHost);
LayerTreeHost::RecreateResult result = m_layerTreeHost->recreateContext();
if (result == LayerTreeHost::RecreateFailedButTryAgain)
Proxy::mainThread()->postTask(m_contextRecreationCallback.callback());
else if (result == LayerTreeHost::RecreateSucceeded)
m_contextRecreationCallback.Cancel();
}
void ThreadProxy::initializeImplOnImplThread(CompletionEvent* completion, InputHandler* handler)
{
TRACE_EVENT0("cc", "ThreadProxy::initializeImplOnImplThread");
DCHECK(isImplThread());
m_layerTreeHostImpl = m_layerTreeHost->createLayerTreeHostImpl(this);
const base::TimeDelta displayRefreshInterval = base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond / 60);
scoped_ptr<FrameRateController> frameRateController;
if (m_renderVSyncEnabled)
frameRateController.reset(new FrameRateController(DelayBasedTimeSource::create(displayRefreshInterval, Proxy::implThread())));
else
frameRateController.reset(new FrameRateController(Proxy::implThread()));
m_schedulerOnImplThread = Scheduler::create(this, frameRateController.Pass());
m_schedulerOnImplThread->setVisible(m_layerTreeHostImpl->visible());
m_inputHandlerOnImplThread = scoped_ptr<InputHandler>(handler);
if (m_inputHandlerOnImplThread.get())
m_inputHandlerOnImplThread->bindToClient(m_layerTreeHostImpl.get());
completion->signal();
}
void ThreadProxy::initializeContextOnImplThread(scoped_ptr<GraphicsContext> context)
{
TRACE_EVENT0("cc", "ThreadProxy::initializeContextOnImplThread");
DCHECK(isImplThread());
m_contextBeforeInitializationOnImplThread = context.Pass();
}
void ThreadProxy::initializeRendererOnImplThread(CompletionEvent* completion, bool* initializeSucceeded, RendererCapabilities* capabilities)
{
TRACE_EVENT0("cc", "ThreadProxy::initializeRendererOnImplThread");
DCHECK(isImplThread());
DCHECK(m_contextBeforeInitializationOnImplThread.get());
*initializeSucceeded = m_layerTreeHostImpl->initializeRenderer(m_contextBeforeInitializationOnImplThread.Pass());
if (*initializeSucceeded) {
*capabilities = m_layerTreeHostImpl->rendererCapabilities();
m_schedulerOnImplThread->setSwapBuffersCompleteSupported(
capabilities->usingSwapCompleteCallback);
}
completion->signal();
}
void ThreadProxy::layerTreeHostClosedOnImplThread(CompletionEvent* completion)
{
TRACE_EVENT0("cc", "ThreadProxy::layerTreeHostClosedOnImplThread");
DCHECK(isImplThread());
ResourceProvider::debugNotifyEnterZone(0x6000000);
m_layerTreeHost->deleteContentsTexturesOnImplThread(m_layerTreeHostImpl->resourceProvider());
ResourceProvider::debugNotifyLeaveZone();
m_inputHandlerOnImplThread.reset();
m_layerTreeHostImpl.reset();
m_schedulerOnImplThread.reset();
completion->signal();
}
void ThreadProxy::setFullRootLayerDamageOnImplThread()
{
DCHECK(isImplThread());
m_layerTreeHostImpl->setFullRootLayerDamage();
}
size_t ThreadProxy::maxPartialTextureUpdates() const
{
return ResourceUpdateController::maxPartialTextureUpdates();
}
void ThreadProxy::recreateContextOnImplThread(CompletionEvent* completion, scoped_ptr<GraphicsContext> context, bool* recreateSucceeded, RendererCapabilities* capabilities)
{
TRACE_EVENT0("cc", "ThreadProxy::recreateContextOnImplThread");
DCHECK(isImplThread());
ResourceProvider::debugNotifyEnterZone(0x7000000);
m_layerTreeHost->deleteContentsTexturesOnImplThread(m_layerTreeHostImpl->resourceProvider());
ResourceProvider::debugNotifyLeaveZone();
*recreateSucceeded = m_layerTreeHostImpl->initializeRenderer(context.Pass());
if (*recreateSucceeded) {
*capabilities = m_layerTreeHostImpl->rendererCapabilities();
m_schedulerOnImplThread->didRecreateContext();
}
completion->signal();
}
void ThreadProxy::renderingStatsOnImplThread(CompletionEvent* completion, RenderingStats* stats)
{
DCHECK(isImplThread());
m_layerTreeHostImpl->renderingStats(stats);
completion->signal();
}
ThreadProxy::BeginFrameAndCommitState::BeginFrameAndCommitState()
{
}
ThreadProxy::BeginFrameAndCommitState::~BeginFrameAndCommitState()
{
}
} // namespace cc
|