1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/testing/sim/sim_compositor.h"
6 
7 #include "cc/test/fake_layer_tree_frame_sink.h"
8 #include "cc/trees/render_frame_metadata_observer.h"
9 #include "third_party/blink/public/platform/web_rect.h"
10 #include "third_party/blink/renderer/core/exported/web_view_impl.h"
11 #include "third_party/blink/renderer/core/frame/local_frame.h"
12 #include "third_party/blink/renderer/core/frame/local_frame_view.h"
13 #include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
14 #include "third_party/blink/renderer/core/layout/layout_view.h"
15 #include "third_party/blink/renderer/core/paint/compositing/composited_layer_mapping.h"
16 #include "third_party/blink/renderer/core/paint/compositing/paint_layer_compositor.h"
17 #include "third_party/blink/renderer/core/paint/paint_layer.h"
18 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
19 #include "third_party/blink/renderer/platform/graphics/paint/cull_rect.h"
20 #include "third_party/blink/renderer/platform/graphics/paint/paint_record_builder.h"
21 
22 namespace blink {
23 
SimCompositor()24 SimCompositor::SimCompositor() {
25   last_frame_time_ = base::TimeTicks::Now();
26 }
27 
28 SimCompositor::~SimCompositor() = default;
29 
SetWebView(WebViewImpl & web_view,frame_test_helpers::TestWebViewClient & view_client)30 void SimCompositor::SetWebView(
31     WebViewImpl& web_view,
32     frame_test_helpers::TestWebViewClient& view_client) {
33   web_view_ = &web_view;
34   test_web_view_client_ = &view_client;
35 }
36 
BeginFrame(double time_delta_in_seconds,bool raster)37 SimCanvas::Commands SimCompositor::BeginFrame(double time_delta_in_seconds,
38                                               bool raster) {
39   DCHECK(web_view_);
40   DCHECK(!layer_tree_host()->defer_main_frame_update());
41   // Verify that the need for a BeginMainFrame has been registered, and would
42   // have caused the compositor to schedule one if we were using its scheduler.
43   DCHECK(NeedsBeginFrame());
44   DCHECK_GT(time_delta_in_seconds, 0);
45 
46   ClearAnimationScheduled();
47 
48   last_frame_time_ += base::TimeDelta::FromSecondsD(time_delta_in_seconds);
49 
50   SimCanvas::Commands commands;
51   paint_commands_ = &commands;
52 
53   layer_tree_host()->CompositeForTest(last_frame_time_, raster);
54 
55   paint_commands_ = nullptr;
56   return commands;
57 }
58 
PaintFrame()59 SimCanvas::Commands SimCompositor::PaintFrame() {
60   DCHECK(web_view_);
61 
62   if (!web_view_->MainFrameImpl())
63     return SimCanvas::Commands();
64 
65   auto* frame = web_view_->MainFrameImpl()->GetFrame();
66   PaintRecordBuilder builder;
67   frame->View()->PaintOutsideOfLifecycle(builder.Context(),
68                                          kGlobalPaintFlattenCompositingLayers);
69 
70   auto infinite_rect = LayoutRect::InfiniteIntRect();
71   SimCanvas canvas(infinite_rect.Width(), infinite_rect.Height());
72   builder.EndRecording()->Playback(&canvas);
73   return canvas.GetCommands();
74 }
75 
DidBeginMainFrame()76 void SimCompositor::DidBeginMainFrame() {
77   // Note that this will run *before* LocalFrameView::RunPostLifecycleSteps,
78   // due to the calling conventions of PageWidgetDelegate::DidBeginFrame(). As a
79   // result, frame throttling status has not been updated yet, and will be in
80   // the same state as when the lifecycle steps ran.
81   *paint_commands_ = PaintFrame();
82 }
83 
84 }  // namespace blink
85