summaryrefslogtreecommitdiffstats
path: root/chromium/ui/android/delegated_frame_host_android_unittest.cc
blob: 8e27a479380ee80a6cd6a24f77936192165f94d7 (plain)
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
// Copyright 2018 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 "ui/android/delegated_frame_host_android.h"
#include "base/android/build_info.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_mock_time_task_runner.h"
#include "cc/layers/layer.h"
#include "cc/layers/solid_color_layer.h"
#include "cc/layers/surface_layer.h"
#include "cc/trees/layer_tree_host.h"
#include "components/viz/common/features.h"
#include "components/viz/common/hit_test/hit_test_region_list.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/host/host_frame_sink_manager.h"
#include "components/viz/service/display_embedder/server_shared_bitmap_manager.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/android/view_android.h"
#include "ui/android/window_android_compositor.h"

namespace ui {
namespace {

using ::testing::Return;
using ::testing::_;
using ::testing::NiceMock;
using ::testing::Invoke;

class MockDelegatedFrameHostAndroidClient
    : public DelegatedFrameHostAndroid::Client {
 public:
  MOCK_METHOD1(SetBeginFrameSource, void(viz::BeginFrameSource*));
  MOCK_METHOD1(DidReceiveCompositorFrameAck,
               void(const std::vector<viz::ReturnedResource>&));
  MOCK_METHOD1(ReclaimResources,
               void(const std::vector<viz::ReturnedResource>&));
  MOCK_METHOD1(
      DidPresentCompositorFrames,
      void(const base::flat_map<uint32_t, gfx::PresentationFeedback>&));
  MOCK_METHOD1(OnFrameTokenChanged, void(uint32_t));
  MOCK_METHOD0(WasEvicted, void());
};

class MockWindowAndroidCompositor : public WindowAndroidCompositor {
 public:
  MOCK_METHOD1(AttachLayerForReadback, void(scoped_refptr<cc::Layer>));
  MOCK_METHOD1(DoRequestCopyOfOutputOnRootLayer, void(viz::CopyOutputRequest*));
  MOCK_METHOD0(SetNeedsAnimate, void());
  MOCK_METHOD0(GetResourceManager, ResourceManager&());
  MOCK_METHOD0(GetFrameSinkId, viz::FrameSinkId());
  MOCK_METHOD1(AddChildFrameSink, void(const viz::FrameSinkId&));
  MOCK_METHOD1(RemoveChildFrameSink, void(const viz::FrameSinkId&));
  MOCK_METHOD2(DoGetCompositorLock,
               CompositorLock*(CompositorLockClient*, base::TimeDelta));
  MOCK_CONST_METHOD0(IsDrawingFirstVisibleFrame, bool());
  MOCK_METHOD1(SetVSyncPaused, void(bool));

  // Helpers for move-only types:
  void RequestCopyOfOutputOnRootLayer(
      std::unique_ptr<viz::CopyOutputRequest> request) override {
    return DoRequestCopyOfOutputOnRootLayer(request.get());
  }

  std::unique_ptr<CompositorLock> GetCompositorLock(
      CompositorLockClient* client,
      base::TimeDelta time_delta) override {
    return std::unique_ptr<CompositorLock>(
        DoGetCompositorLock(client, time_delta));
  }
};

class DelegatedFrameHostAndroidTest : public testing::Test {
 public:
  DelegatedFrameHostAndroidTest()
      : frame_sink_manager_impl_(&shared_bitmap_manager_),
        frame_sink_id_(1, 1),
        task_runner_(new base::TestMockTimeTaskRunner()),
        lock_manager_(task_runner_) {
    host_frame_sink_manager_.SetLocalManager(&frame_sink_manager_impl_);
    frame_sink_manager_impl_.SetLocalClient(&host_frame_sink_manager_);
  }

  void SetUp() override {
    view_.SetLayer(cc::SolidColorLayer::Create());
    frame_host_ = std::make_unique<DelegatedFrameHostAndroid>(
        &view_, &host_frame_sink_manager_, &client_, frame_sink_id_,
        features::IsSurfaceSynchronizationEnabled());
  }

  void TearDown() override { frame_host_.reset(); }

  ui::CompositorLock* GetLock(CompositorLockClient* client,
                              base::TimeDelta time_delta) {
    return lock_manager_.GetCompositorLock(client, time_delta, nullptr)
        .release();
  }

  bool IsLocked() const { return lock_manager_.IsLocked(); }

  void SubmitCompositorFrame(const gfx::Size& frame_size = gfx::Size(10, 10)) {
    viz::CompositorFrame frame =
        viz::CompositorFrameBuilder()
            .AddRenderPass(gfx::Rect(frame_size), gfx::Rect(frame_size))
            .Build();
    allocator_.GenerateId();
    frame_host_->SubmitCompositorFrame(
        allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
        std::move(frame), base::nullopt);
  }

  void SetUpValidFrame(const gfx::Size& frame_size) {
    EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame())
        .WillOnce(Return(true));
    EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
        .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
    frame_host_->AttachToCompositor(&compositor_);
    EXPECT_TRUE(IsLocked());

    SubmitCompositorFrame(frame_size);
    EXPECT_FALSE(IsLocked());
  }

 protected:
  MockWindowAndroidCompositor compositor_;
  ui::ViewAndroid view_;
  viz::ServerSharedBitmapManager shared_bitmap_manager_;
  viz::FrameSinkManagerImpl frame_sink_manager_impl_;
  viz::HostFrameSinkManager host_frame_sink_manager_;
  MockDelegatedFrameHostAndroidClient client_;
  viz::FrameSinkId frame_sink_id_;
  viz::ParentLocalSurfaceIdAllocator allocator_;
  std::unique_ptr<DelegatedFrameHostAndroid> frame_host_;
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  CompositorLockManager lock_manager_;
};

class DelegatedFrameHostAndroidVizTest : public DelegatedFrameHostAndroidTest {
 public:
  DelegatedFrameHostAndroidVizTest() = default;
  ~DelegatedFrameHostAndroidVizTest() override = default;

  void SetUp() override {
    // Enable both Viz and SurfaceSync.
    scoped_feature_list_.InitWithFeatures(
        {features::kVizDisplayCompositor,
         features::kEnableSurfaceSynchronization},
        {});

    DelegatedFrameHostAndroidTest::SetUp();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// TODO(ericrk): Remove these tests once Viz OOP-D has launched.
class DelegatedFrameHostAndroidSurfaceSynchronizationOnlyTest
    : public DelegatedFrameHostAndroidTest {
 public:
  DelegatedFrameHostAndroidSurfaceSynchronizationOnlyTest() = default;
  ~DelegatedFrameHostAndroidSurfaceSynchronizationOnlyTest() override = default;

  void SetUp() override {
    // Enable SurfaceSync without Viz.
    scoped_feature_list_.InitWithFeatures(
        {features::kEnableSurfaceSynchronization},
        {features::kVizDisplayCompositor});

    DelegatedFrameHostAndroidTest::SetUp();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// TODO(ericrk): Remove these tests once Viz OOP-D has launched.
class DelegatedFrameHostAndroidLegacyNonVizTest
    : public DelegatedFrameHostAndroidTest {
 public:
  DelegatedFrameHostAndroidLegacyNonVizTest() = default;
  ~DelegatedFrameHostAndroidLegacyNonVizTest() override = default;

  void SetUp() override {
    // Disable both Viz and SurfaceSync.
    scoped_feature_list_.InitWithFeatures(
        {}, {features::kVizDisplayCompositor,
             features::kEnableSurfaceSynchronization});

    DelegatedFrameHostAndroidTest::SetUp();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Resize lock is only enabled on O+.
static bool IsResizeLockEnabled() {
  return base::android::BuildInfo::GetInstance()->sdk_int() >=
         base::android::SDK_VERSION_OREO;
}

// If OOP-D is enabled then we should not be acquiring a compositor lock on
// attach.
TEST_F(DelegatedFrameHostAndroidVizTest, NoCompositorLockOnAttach) {
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame()).Times(0);
  EXPECT_CALL(compositor_, DoGetCompositorLock(_, _)).Times(0);
  frame_host_->AttachToCompositor(&compositor_);
}

// If surface synchronization is off, and we are doing a cross-process
// navigation, then both the primary and fallback surface IDs need to be
// updated together.
TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       TakeFallbackContentFromUpdatesPrimary) {
  EXPECT_FALSE(frame_host_->SurfaceId().is_valid());
  // Submit a compositor frame to ensure we have delegated content.
  SubmitCompositorFrame();

  EXPECT_TRUE(frame_host_->SurfaceId().is_valid());
  std::unique_ptr<DelegatedFrameHostAndroid> other_frame_host =
      std::make_unique<DelegatedFrameHostAndroid>(
          &view_, &host_frame_sink_manager_, &client_, viz::FrameSinkId(2, 2),
          features::IsSurfaceSynchronizationEnabled());

  EXPECT_FALSE(other_frame_host->SurfaceId().is_valid());

  other_frame_host->TakeFallbackContentFrom(frame_host_.get());

  EXPECT_TRUE(other_frame_host->SurfaceId().is_valid());
  EXPECT_EQ(other_frame_host->content_layer_for_testing()->surface_id(),
            other_frame_host->content_layer_for_testing()
                ->oldest_acceptable_fallback());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       CompositorLockDuringFirstFrame) {
  // Attach during the first frame, lock will be taken.
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame()).WillOnce(Return(true));
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->AttachToCompositor(&compositor_);
  EXPECT_TRUE(IsLocked());

  // Lock should be released when we submit a compositor frame.
  SubmitCompositorFrame();
  EXPECT_FALSE(IsLocked());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       CompositorLockDuringLaterFrame) {
  // Attach after the first frame, lock will not be taken.
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame())
      .WillOnce(Return(false));
  EXPECT_CALL(compositor_, DoGetCompositorLock(_, _)).Times(0);
  frame_host_->AttachToCompositor(&compositor_);
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       CompositorLockWithDelegatedContent) {
  // Submit a compositor frame to ensure we have delegated content.
  SubmitCompositorFrame();

  // Even though it's the first frame, we won't take the lock as we already have
  // delegated content.
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame()).WillOnce(Return(true));
  EXPECT_CALL(compositor_, DoGetCompositorLock(_, _)).Times(0);
  frame_host_->AttachToCompositor(&compositor_);
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       CompositorLockReleasedWithDetach) {
  // Attach during the first frame, lock will be taken.
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame()).WillOnce(Return(true));
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->AttachToCompositor(&compositor_);
  EXPECT_TRUE(IsLocked());

  // Lock should be released when we detach.
  frame_host_->DetachFromCompositor();
  EXPECT_FALSE(IsLocked());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest, ResizeLockBasic) {
  // Resize lock is only enabled on O+.
  if (!IsResizeLockEnabled())
    return;

  SetUpValidFrame(gfx::Size(10, 10));

  // Tell the frame host to resize, it should take a lock.
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->PixelSizeWillChange(gfx::Size(50, 50));
  EXPECT_TRUE(IsLocked());

  // Submit a frame of the wrong size, nothing should change.
  SubmitCompositorFrame(gfx::Size(20, 20));
  EXPECT_TRUE(IsLocked());

  // Submit a frame with the right size, the lock should release.
  SubmitCompositorFrame(gfx::Size(50, 50));
  EXPECT_FALSE(IsLocked());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       ResizeLockNotTakenIfNoSizeChange) {
  // Resize lock is only enabled on O+.
  if (!IsResizeLockEnabled())
    return;

  SetUpValidFrame(gfx::Size(10, 10));

  // Tell the frame host to resize to the existing size, nothing should happen.
  frame_host_->PixelSizeWillChange(gfx::Size(10, 10));
  EXPECT_FALSE(IsLocked());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest,
       ResizeLockReleasedWithDetach) {
  // Resize lock is only enabled on O+.
  if (!IsResizeLockEnabled())
    return;

  SetUpValidFrame(gfx::Size(10, 10));

  // Tell the frame host to resize, it should take a lock.
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->PixelSizeWillChange(gfx::Size(50, 50));
  EXPECT_TRUE(IsLocked());

  // Lock should be released when we detach.
  frame_host_->DetachFromCompositor();
  EXPECT_FALSE(IsLocked());
}

TEST_F(DelegatedFrameHostAndroidLegacyNonVizTest, TestBothCompositorLocks) {
  // Resize lock is only enabled on O+.
  if (!IsResizeLockEnabled())
    return;

  // Attach during the first frame, first lock will be taken.
  EXPECT_CALL(compositor_, IsDrawingFirstVisibleFrame()).WillOnce(Return(true));
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->AttachToCompositor(&compositor_);
  EXPECT_TRUE(IsLocked());

  // Tell the frame host to resize, it should take a second lock.
  EXPECT_CALL(compositor_, DoGetCompositorLock(frame_host_.get(), _))
      .WillOnce(Invoke(this, &DelegatedFrameHostAndroidTest::GetLock));
  frame_host_->PixelSizeWillChange(gfx::Size(50, 50));
  EXPECT_TRUE(IsLocked());

  // Submit a compositor frame of the right size, both locks should release.
  SubmitCompositorFrame(gfx::Size(50, 50));
  EXPECT_FALSE(IsLocked());
}

// Make sure frame evictor is notified of the newly embedded surface after
// WasShown.
TEST_F(DelegatedFrameHostAndroidVizTest, EmbedWhileHidden) {
  // Ensure there is currently no frame.
  frame_host_->WasHidden();
  frame_host_->EvictDelegatedFrame();
  EXPECT_FALSE(frame_host_->HasSavedFrame());

  allocator_.GenerateId();
  viz::LocalSurfaceId id =
      allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
  gfx::Size size(100, 100);
  frame_host_->EmbedSurface(id, size, cc::DeadlinePolicy::UseDefaultDeadline());
  EXPECT_FALSE(frame_host_->HasSavedFrame());
  frame_host_->WasShown(id, size);
  EXPECT_TRUE(frame_host_->HasSavedFrame());
}

// Verify that when a source rect or output size is not provided to
// CopyFromCompositingSurface, the corresponding values in CopyOutputRequest
// are also not initialized.
TEST_F(DelegatedFrameHostAndroidSurfaceSynchronizationOnlyTest,
       FullSurfaceCapture) {
  // First embed a surface to make sure we have something to copy from.
  allocator_.GenerateId();
  viz::LocalSurfaceId id =
      allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
  gfx::Size size(100, 100);
  frame_host_->EmbedSurface(id, size, cc::DeadlinePolicy::UseDefaultDeadline());

  // Request readback without source rect or output size specified.
  frame_host_->CopyFromCompositingSurface(gfx::Rect(), gfx::Size(),
                                          base::DoNothing());

  // Make sure the resulting CopyOutputRequest does not have its area or result
  // selection set.
  const std::vector<
      std::pair<viz::LocalSurfaceId, std::unique_ptr<viz::CopyOutputRequest>>>&
      requests = frame_sink_manager_impl_.GetFrameSinkForId(frame_sink_id_)
                     ->copy_output_requests_for_testing();
  ASSERT_EQ(1u, requests.size());
  viz::CopyOutputRequest* request = requests[0].second.get();
  EXPECT_FALSE(request->has_area());
  EXPECT_FALSE(request->has_result_selection());
}

}  // namespace
}  // namespace ui