File: surface_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (417 lines) | stat: -rw-r--r-- 17,771 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "cc/test/scheduler_test_common.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/common/surfaces/subtree_capture_id.h"
#include "components/viz/service/frame_sinks/compositor_frame_sink_support.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/surfaces/pending_copy_output_request.h"
#include "components/viz/service/surfaces/surface.h"
#include "components/viz/test/begin_frame_args_test.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "components/viz/test/fake_external_begin_frame_source.h"
#include "components/viz/test/mock_compositor_frame_sink_client.h"
#include "components/viz/test/test_surface_id_allocator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

namespace viz {
namespace {

constexpr FrameSinkId kArbitraryFrameSinkId(1, 1);
constexpr bool kIsRoot = true;
const uint64_t kBeginFrameSourceId = 1337;

class SurfaceTest : public testing::Test {
 public:
  SurfaceTest() : frame_sink_manager_(FrameSinkManagerImpl::InitParams()) {}

 protected:
  std::unique_ptr<base::SimpleTestTickClock> now_src_;
  FrameSinkManagerImpl frame_sink_manager_;
};

TEST_F(SurfaceTest, PresentationCallback) {
  constexpr gfx::Size kSurfaceSize(300, 300);
  constexpr gfx::Rect kDamageRect(0, 0);
  const LocalSurfaceId local_surface_id(6, base::UnguessableToken::Create());

  MockCompositorFrameSinkClient client;
  auto support = std::make_unique<CompositorFrameSinkSupport>(
      &client, &frame_sink_manager_, kArbitraryFrameSinkId, kIsRoot);
  uint32_t frame_token = kInvalidFrameToken;
  {
    CompositorFrame frame =
        CompositorFrameBuilder()
            .AddRenderPass(gfx::Rect(kSurfaceSize), kDamageRect)
            .SetBeginFrameSourceId(kBeginFrameSourceId)
            .Build();
    frame_token = frame.metadata.frame_token;
    ASSERT_NE(frame_token, kInvalidFrameToken);
    EXPECT_CALL(client, DidReceiveCompositorFrameAck(testing::_)).Times(1);
    support->SubmitCompositorFrame(local_surface_id, std::move(frame));
    testing::Mock::VerifyAndClearExpectations(&client);
  }

  {
    // Replaces previous frame. The previous frame with token 1 will be
    // discarded.
    CompositorFrame frame =
        CompositorFrameBuilder()
            .AddRenderPass(gfx::Rect(kSurfaceSize), kDamageRect)
            .SetBeginFrameSourceId(kBeginFrameSourceId)
            .Build();
    EXPECT_CALL(client, DidReceiveCompositorFrameAck(testing::_)).Times(1);
    support->SubmitCompositorFrame(local_surface_id, std::move(frame));
    ASSERT_EQ(1u, support->timing_details().size());
    EXPECT_EQ(frame_token, support->timing_details().begin()->first);
    testing::Mock::VerifyAndClearExpectations(&client);
  }
}

TEST_F(SurfaceTest, SurfaceIds) {
  for (size_t i = 0; i < 3; ++i) {
    ParentLocalSurfaceIdAllocator allocator;
    allocator.GenerateId();
    LocalSurfaceId id1 = allocator.GetCurrentLocalSurfaceId();
    allocator.GenerateId();
    LocalSurfaceId id2 = allocator.GetCurrentLocalSurfaceId();
    EXPECT_NE(id1, id2);
  }
}

void TestCopyResultCallback(bool* called,
                            base::OnceClosure finished,
                            std::unique_ptr<CopyOutputResult> result) {
  *called = true;
  std::move(finished).Run();
}

// Test that CopyOutputRequests can outlive the current frame and be
// aggregated on the next frame.
TEST_F(SurfaceTest, CopyRequestLifetime) {
  SurfaceManager* surface_manager = frame_sink_manager_.surface_manager();
  auto support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, kArbitraryFrameSinkId, kIsRoot);

  LocalSurfaceId local_surface_id(6, base::UnguessableToken::Create());
  SurfaceId surface_id(kArbitraryFrameSinkId, local_surface_id);
  CompositorFrame frame = MakeDefaultCompositorFrame(kBeginFrameSourceId);
  support->SubmitCompositorFrame(local_surface_id, std::move(frame));
  Surface* surface = surface_manager->GetSurfaceForId(surface_id);
  ASSERT_TRUE(surface);

  bool copy_called = false;
  base::RunLoop copy_runloop;
  support->RequestCopyOfOutput(PendingCopyOutputRequest{
      local_surface_id, SubtreeCaptureId(),
      std::make_unique<CopyOutputRequest>(
          CopyOutputRequest::ResultFormat::RGBA,
          CopyOutputRequest::ResultDestination::kSystemMemory,
          base::BindOnce(&TestCopyResultCallback, &copy_called,
                         copy_runloop.QuitClosure()))});
  surface->TakeCopyOutputRequestsFromClient();
  EXPECT_TRUE(surface_manager->GetSurfaceForId(surface_id));
  EXPECT_FALSE(copy_called);

  uint64_t max_frame = 3, start_id = 200;
  for (uint64_t i = 0; i < max_frame; ++i) {
    frame = CompositorFrameBuilder().Build();
    frame.render_pass_list.push_back(CompositorRenderPass::Create());
    frame.render_pass_list.back()->id =
        CompositorRenderPassId{i * 3 + start_id};
    frame.render_pass_list.push_back(CompositorRenderPass::Create());
    frame.render_pass_list.back()->id =
        CompositorRenderPassId{i * 3 + start_id + 1};
    frame.render_pass_list.push_back(CompositorRenderPass::Create());
    frame.render_pass_list.back()->SetNew(
        CompositorRenderPassId{i * 3 + start_id + 2}, gfx::Rect(0, 0, 20, 20),
        gfx::Rect(), gfx::Transform());
    support->SubmitCompositorFrame(local_surface_id, std::move(frame));
  }

  CompositorRenderPassId last_pass_id{(max_frame - 1) * 3 + start_id + 2};

  // The copy request should stay on the Surface until TakeCopyOutputRequests
  // is called.
  EXPECT_FALSE(copy_called);
  EXPECT_EQ(
      1u,
      surface->GetActiveFrame().render_pass_list.back()->copy_requests.size());

  Surface::CopyRequestsMap copy_requests;
  surface->TakeCopyOutputRequests(&copy_requests);
  EXPECT_EQ(1u, copy_requests.size());
  // Last (root) pass should receive copy request.
  ASSERT_EQ(1u, copy_requests.count(last_pass_id));
  EXPECT_FALSE(copy_called);
  copy_requests.clear();  // Deleted requests will auto-send an empty result.
  copy_runloop.Run();
  EXPECT_TRUE(copy_called);
}

// Verify activate referenced surfaces is correct when there are two surface
// references to overlapping surface ranges. In particular the two surface
// ranges are (S1, S1) and (S1, S2). When both S1 and S2 activate active
// referenced surfaces should include both S1 and S2. See
// https://crbug.com/1275605 for more context.
TEST_F(SurfaceTest, ActiveSurfaceReferencesWithOverlappingReferences) {
  constexpr gfx::Rect output_rect(100, 100);
  SurfaceManager* surface_manager = frame_sink_manager_.surface_manager();

  auto root_support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, kArbitraryFrameSinkId,
      /*is_root=*/true);
  TestSurfaceIdAllocator root_surface_id(kArbitraryFrameSinkId);

  auto child_support1 = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, FrameSinkId(2, 1), /*is_root=*/false);
  TestSurfaceIdAllocator child_surface_id1(child_support1->frame_sink_id());

  auto child_support2 = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, FrameSinkId(3, 1), /*is_root=*/false);
  TestSurfaceIdAllocator child_surface_id2(child_support2->frame_sink_id());

  // Submit a root frame with two SurfaceDrawQuads. The first SurfaceDrawQuad
  // embeds |child_support1|. The second SurfaceDrawQuad embeds |child_support2|
  // but has |child_support1| as a fallback which mimics a navigating renderer.
  // Note that |old_surface_range| and |navigation_surface_range| overlap.
  SurfaceRange old_surface_range(child_surface_id1, child_surface_id1);
  SurfaceRange navigation_surface_range(child_surface_id1, child_surface_id2);
  auto root_render_pass =
      RenderPassBuilder(CompositorRenderPassId{1}, output_rect)
          .AddSurfaceQuad(output_rect, old_surface_range)
          .AddSurfaceQuad(output_rect, navigation_surface_range)
          .Build();

  {
    CompositorFrame frame = MakeCompositorFrame(root_render_pass->DeepCopy());
    EXPECT_THAT(
        frame.metadata.referenced_surfaces,
        testing::ElementsAre(old_surface_range, navigation_surface_range));

    root_support->SubmitCompositorFrame(root_surface_id.local_surface_id(),
                                        std::move(frame));
  }

  Surface* root_surface = surface_manager->GetSurfaceForId(root_surface_id);
  ASSERT_TRUE(root_surface);

  // No active references yet because no child surfaces have been submitted yet.
  EXPECT_THAT(root_surface->active_referenced_surfaces(), testing::IsEmpty());

  // Submit something to the second child surface and verify it's now included
  // in active referenced surfaces.
  child_support2->SubmitCompositorFrame(
      child_surface_id2.local_surface_id(),
      MakeDefaultCompositorFrame(kBeginFrameSourceId));
  EXPECT_TRUE(surface_manager->GetSurfaceForId(child_surface_id2));
  EXPECT_THAT(root_surface->active_referenced_surfaces(),
              testing::ElementsAre(child_surface_id2));

  // Submit something to the first child surface and verify both are in active
  // surface references. Note this order of activation is "backwards" as
  // normally the |child_surface_id1| would have activated first if the browser
  // is navigating away from it but if the first renderer is slow to produce
  // content the order can be reversed.
  child_support1->SubmitCompositorFrame(
      child_surface_id1.local_surface_id(),
      MakeDefaultCompositorFrame(kBeginFrameSourceId));
  EXPECT_TRUE(surface_manager->GetSurfaceForId(child_surface_id1));
  EXPECT_THAT(root_surface->active_referenced_surfaces(),
              testing::ElementsAre(child_surface_id1, child_surface_id2));

  // Resubmit root frame with the same SurfaceDrawQuads and verify active
  // surface references are unchanged.
  root_support->SubmitCompositorFrame(
      root_surface_id.local_surface_id(),
      MakeCompositorFrame(std::move(root_render_pass)));
  EXPECT_THAT(root_surface->active_referenced_surfaces(),
              testing::ElementsAre(child_surface_id1, child_surface_id2));

  // Submit a new root frame without the reference to the first child surface
  // and verify |child_surface_id1| is no longer part of active referenced
  // surfaces.
  {
    SurfaceRange post_navigation_surface_range(child_surface_id2,
                                               child_surface_id2);
    CompositorFrame frame =
        CompositorFrameBuilder()
            .AddRenderPass(
                RenderPassBuilder(CompositorRenderPassId{1}, output_rect)
                    .AddSurfaceQuad(output_rect, post_navigation_surface_range))
            .Build();

    root_support->SubmitCompositorFrame(root_surface_id.local_surface_id(),
                                        std::move(frame));
  }

  EXPECT_THAT(root_surface->active_referenced_surfaces(),
              testing::ElementsAre(child_surface_id2));
}

TEST_F(SurfaceTest, PendingCopySurfaceIncludedInActiveReferencedSurfaces) {
  SurfaceManager* surface_manager = frame_sink_manager_.surface_manager();

  gfx::Rect rect(5, 5);

  auto support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, kArbitraryFrameSinkId,
      /*is_root=*/false);

  TestSurfaceIdAllocator allocator(kArbitraryFrameSinkId);
  SurfaceId prev_id = allocator.Get();
  allocator.Increment();
  SurfaceId curr_id = allocator.Get();

  {
    CompositorFrame frame =
        MakeCompositorFrame(RenderPassBuilder(CompositorRenderPassId{1}, rect)
                                .AddSolidColorQuad(rect, SkColors::kBlue)
                                .AddSolidColorQuad(rect, SkColors::kBlue)
                                .Build());
    support->SubmitCompositorFrame(prev_id.local_surface_id(),
                                   std::move(frame));
  }
  {
    CompositorFrame frame =
        MakeCompositorFrame(RenderPassBuilder(CompositorRenderPassId{2}, rect)
                                .AddSolidColorQuad(rect, SkColors::kBlue)
                                .AddSolidColorQuad(rect, SkColors::kBlue)
                                .Build());
    frame.metadata.screenshot_destination =
        blink::SameDocNavigationScreenshotDestinationToken(
            base::UnguessableToken::Create());
    support->SubmitCompositorFrame(curr_id.local_surface_id(),
                                   std::move(frame));
  }

  auto* curr_surface = surface_manager->GetSurfaceForId(curr_id);
  ASSERT_TRUE(curr_surface);
  ASSERT_THAT(curr_surface->active_referenced_surfaces(),
              ::testing::UnorderedElementsAre(prev_id));

  curr_surface->ResetPendingCopySurfaceId();
  ASSERT_TRUE(curr_surface->active_referenced_surfaces().empty());
}

// Parameterized by whether we should enable kDrawImmediatelyWhenInteractive.
class ImmediateActivationSurfaceTest
    : public SurfaceTest,
      public testing::WithParamInterface<bool> {
 public:
  ImmediateActivationSurfaceTest() {
    if (GetParam()) {
      scoped_feature_list_.InitAndEnableFeature(
          features::kDrawImmediatelyWhenInteractive);
    } else {
      scoped_feature_list_.InitAndDisableFeature(
          features::kDrawImmediatelyWhenInteractive);
    }
  }

  void SetUp() override {
    SurfaceTest::SetUp();
    now_src_ = std::make_unique<base::SimpleTestTickClock>();
    frame_sink_manager_.surface_manager()->SetTickClockForTesting(
        now_src_.get());
  }

  base::TimeTicks Now() { return now_src_->NowTicks(); }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(All, ImmediateActivationSurfaceTest, testing::Bool());

// Checks that submitting a compositor frame with a dependency always results in
// activation dependencies if we have no interaction.
TEST_P(ImmediateActivationSurfaceTest, WithNoInteraction) {
  constexpr gfx::Rect output_rect(100, 100);
  SurfaceManager* surface_manager = frame_sink_manager_.surface_manager();

  auto root_support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, kArbitraryFrameSinkId,
      /*is_root=*/true);
  TestSurfaceIdAllocator root_surface_id(kArbitraryFrameSinkId);

  auto child_support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, FrameSinkId(2, 1), /*is_root=*/false);
  TestSurfaceIdAllocator child_surface_id(child_support->frame_sink_id());

  // Submit a root frame with one SurfaceDrawQuad. The SurfaceDrawQuad embeds
  // |child_support| as it would with an OOPIF.
  SurfaceRange surface_range(child_surface_id);
  auto root_render_pass =
      RenderPassBuilder(CompositorRenderPassId{1}, output_rect)
          .AddSurfaceQuad(output_rect, surface_range)
          .Build();

  {
    CompositorFrame frame = MakeCompositorFrame(root_render_pass->DeepCopy());
    frame.metadata.activation_dependencies.push_back(child_surface_id);
    frame.metadata.deadline =
        FrameDeadline(Now(), 4u, BeginFrameArgs::DefaultInterval(), false);
    EXPECT_THAT(frame.metadata.referenced_surfaces,
                testing::ElementsAre(surface_range));
    root_support->SubmitCompositorFrame(root_surface_id.local_surface_id(),
                                        std::move(frame));
  }

  Surface* surface = surface_manager->GetSurfaceForId(root_surface_id);
  EXPECT_FALSE(surface->activation_dependencies().empty());
}

// Checks that submitting a compositor frame with a dependency only results in
// activation dependencies if we haven't enabled immediate activation.
TEST_P(ImmediateActivationSurfaceTest, WithInteraction) {
  constexpr gfx::Rect output_rect(100, 100);
  SurfaceManager* surface_manager = frame_sink_manager_.surface_manager();

  auto root_support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, kArbitraryFrameSinkId,
      /*is_root=*/true);
  TestSurfaceIdAllocator root_surface_id(kArbitraryFrameSinkId);

  auto child_support = std::make_unique<CompositorFrameSinkSupport>(
      nullptr, &frame_sink_manager_, FrameSinkId(2, 1), /*is_root=*/false);
  TestSurfaceIdAllocator child_surface_id(child_support->frame_sink_id());

  // Submit a root frame with one SurfaceDrawQuad. The SurfaceDrawQuad embeds
  // |child_support| as it would with an OOPIF.
  SurfaceRange surface_range(child_surface_id);
  auto root_render_pass =
      RenderPassBuilder(CompositorRenderPassId{1}, output_rect)
          .AddSurfaceQuad(output_rect, surface_range)
          .Build();

  {
    CompositorFrame frame = MakeCompositorFrame(root_render_pass->DeepCopy());
    frame.metadata.activation_dependencies.push_back(child_surface_id);
    frame.metadata.deadline =
        FrameDeadline(Now(), 4u, BeginFrameArgs::DefaultInterval(), false);
    frame.metadata.is_handling_interaction = true;
    EXPECT_THAT(frame.metadata.referenced_surfaces,
                testing::ElementsAre(surface_range));
    root_support->SubmitCompositorFrame(root_surface_id.local_surface_id(),
                                        std::move(frame));
  }

  Surface* surface = surface_manager->GetSurfaceForId(root_surface_id);
  EXPECT_EQ(surface->activation_dependencies().empty(), GetParam());
}

}  // namespace
}  // namespace viz