File: overlay_proposed_candidate_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 (243 lines) | stat: -rw-r--r-- 10,185 bytes parent folder | download | duplicates (9)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/service/display/overlay_proposed_candidate.h"

#include <tuple>
#include <unordered_map>
#include <vector>

#include "components/viz/client/client_resource_provider.h"
#include "components/viz/common/quads/texture_draw_quad.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/service/display/display_resource_provider_null.h"
#include "components/viz/service/display/overlay_candidate_factory.h"
#include "components/viz/service/display/overlay_processor_strategy.h"
#include "components/viz/test/test_context_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace viz {
namespace {

using RoundedDisplayMasksInfo = TextureDrawQuad::RoundedDisplayMasksInfo;

const auto kTestQuadRect = gfx::Rect(0, 0, 100, 100);

class TestOverlayStrategy : public OverlayProcessorStrategy {
 public:
  TestOverlayStrategy() = default;

  TestOverlayStrategy(const TestOverlayStrategy&) = delete;
  TestOverlayStrategy& operator=(const TestOverlayStrategy&) = delete;

  ~TestOverlayStrategy() override = default;

  void Propose(
      const SkM44& output_color_matrix,
      const OverlayProcessorInterface::FilterOperationsMap& render_pass_filters,
      const OverlayProcessorInterface::FilterOperationsMap&
          render_pass_backdrop_filters,
      const DisplayResourceProvider* resource_provider,
      AggregatedRenderPassList* render_pass_list,
      SurfaceDamageRectList* surface_damage_rect_list,
      const PrimaryPlane* primary_plane,
      std::vector<OverlayProposedCandidate>* candidates,
      std::vector<gfx::Rect>* content_bounds) override {}

  bool Attempt(
      const SkM44& output_color_matrix,
      const OverlayProcessorInterface::FilterOperationsMap& render_pass_filters,
      const OverlayProcessorInterface::FilterOperationsMap&
          render_pass_backdrop_filters,
      const DisplayResourceProvider* resource_provider,
      AggregatedRenderPassList* render_pass_list,
      SurfaceDamageRectList* surface_damage_rect_list,
      const PrimaryPlane* primary_plane,
      OverlayCandidateList* candidates,
      std::vector<gfx::Rect>* content_bounds,
      const OverlayProposedCandidate& proposed_candidate) override {
    return true;
  }

  void CommitCandidate(const OverlayProposedCandidate& proposed_candidate,
                       AggregatedRenderPass* render_pass) override {}
};

// TODO(zoraiznaeem): Move resource creation code into OverlayTestBase class.
class OverlayProposedCandidateTest
    : public testing::Test,
      public ::testing::WithParamInterface<
          std::tuple<RoundedDisplayMasksInfo, gfx::Rect, gfx::Rect>> {
 public:
  OverlayProposedCandidateTest()
      : mask_info_(std::get<0>(GetParam())),
        expected_origin_mask_bounds_(std::get<1>(GetParam())),
        expected_other_mask_bounds_(std::get<2>(GetParam())) {}

  OverlayProposedCandidateTest(const OverlayProposedCandidateTest&) = delete;
  OverlayProposedCandidateTest& operator=(const OverlayProposedCandidateTest&) =
      delete;

  ~OverlayProposedCandidateTest() override = default;

 protected:
  void TearDown() override {
    child_resource_provider_.ReleaseAllExportedResources(true);
  }

  ResourceId CreateResource(bool is_overlay_candidate) {
    scoped_refptr<RasterContextProvider> child_context_provider =
        TestContextProvider::Create();

    child_context_provider->BindToCurrentSequence();

    auto resource = TransferableResource::MakeGpu(
        gpu::Mailbox::Generate(), GL_TEXTURE_2D, gpu::SyncToken(),
        gfx::Size(1, 1), SinglePlaneFormat::kRGBA_8888, is_overlay_candidate);

    ResourceId resource_id =
        child_resource_provider_.ImportResource(resource, base::DoNothing());

    int child_id =
        resource_provider_.CreateChild(base::DoNothing(), SurfaceId());

    // Transfer resource to the parent.
    std::vector<ResourceId> resource_ids_to_transfer;
    resource_ids_to_transfer.push_back(resource_id);
    std::vector<TransferableResource> list;
    child_resource_provider_.PrepareSendToParent(
        resource_ids_to_transfer, &list, child_context_provider.get());
    resource_provider_.ReceiveFromChild(child_id, list);

    // Delete it in the child so it won't be leaked, and will be released once
    // returned from the parent.
    child_resource_provider_.RemoveImportedResource(resource_id);

    // In DisplayResourceProvider's namespace, use the mapped resource id.
    std::unordered_map<ResourceId, ResourceId, ResourceIdHasher> resource_map =
        resource_provider_.GetChildToParentMap(child_id);

    return resource_map[list[0].id];
  }

  void AddQuadWithRoundedDisplayMasks(
      gfx::Rect quad_rect,
      bool is_overlay_candidate,
      const gfx::Transform& quad_to_target_transform,
      const RoundedDisplayMasksInfo& rounded_display_masks_info,
      AggregatedRenderPass* render_pass) {
    SharedQuadState* quad_state = render_pass->CreateAndAppendSharedQuadState();

    quad_state->SetAll(
        /*transform=*/quad_to_target_transform, quad_rect,
        /*visible_layer_rect=*/quad_rect,
        /*filter_info=*/gfx::MaskFilterInfo(),
        /*clip=*/std::nullopt,
        /*are contents opaque=*/true,
        /*opacity_f=*/1.f,
        /*blend=*/SkBlendMode::kSrcOver, /*sorting_context=*/0, /*layer_id=*/0u,
        /*fast_rounded_corner=*/false);

    TextureDrawQuad* texture_quad =
        render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
    texture_quad->SetNew(
        quad_state, quad_rect, quad_rect,
        /*needs_blending=*/true, CreateResource(is_overlay_candidate),
        /*premultiplied=*/true, gfx::PointF(), gfx::PointF(),
        /*background=*/SkColors::kTransparent,
        /*flipped=*/false,
        /*nearest=*/false,
        /*secure_output=*/false, gfx::ProtectedVideoType::kClear);

    texture_quad->rounded_display_masks_info = rounded_display_masks_info;
  }

  ClientResourceProvider child_resource_provider_;
  DisplayResourceProviderNull resource_provider_;
  SurfaceDamageRectList surface_damage_list_;
  SkM44 identity_;
  OverlayProcessorInterface::FilterOperationsMap render_pass_filters_;

  RoundedDisplayMasksInfo mask_info_;
  gfx::Rect expected_origin_mask_bounds_;
  gfx::Rect expected_other_mask_bounds_;
};

TEST_P(OverlayProposedCandidateTest, CorrectRoundedDisplayMaskBounds) {
  AggregatedRenderPass render_pass;
  render_pass.SetNew(AggregatedRenderPassId::FromUnsafeValue(1),
                     gfx::Rect(0, 0, 1, 1), gfx::Rect(), gfx::Transform());

  gfx::Transform identity;
  identity.MakeIdentity();

  AddQuadWithRoundedDisplayMasks(kTestQuadRect,
                                 /*is_overlay_candidate=*/true, identity,
                                 mask_info_, &render_pass);

  OverlayCandidateFactory::OverlayContext context;
  context.supports_rounded_display_masks = true;
  OverlayCandidateFactory factory = OverlayCandidateFactory(
      &render_pass, &resource_provider_, &surface_damage_list_, &identity_,
      gfx::RectF(render_pass.output_rect), &render_pass_filters_, context);

  OverlayCandidate candidate;
  OverlayCandidateFactory::CandidateStatus status =
      factory.FromDrawQuad(*render_pass.quad_list.begin(), candidate);
  ASSERT_EQ(status, OverlayCandidateFactory::CandidateStatus::kSuccess);

  TestOverlayStrategy strategy;
  OverlayProposedCandidate proposed_candidate(render_pass.quad_list.begin(),
                                              candidate, &strategy);

  auto mask_bounds = OverlayProposedCandidate::GetRoundedDisplayMasksBounds(
      proposed_candidate);

  EXPECT_EQ(
      mask_bounds[RoundedDisplayMasksInfo::kOriginRoundedDisplayMaskIndex],
      expected_origin_mask_bounds_);
  EXPECT_EQ(mask_bounds[RoundedDisplayMasksInfo::kOtherRoundedDisplayMaskIndex],
            expected_other_mask_bounds_);
}

INSTANTIATE_TEST_SUITE_P(
    /*no_prefix*/,
    OverlayProposedCandidateTest,
    testing::Values(
        std::make_tuple(
            RoundedDisplayMasksInfo::CreateRoundedDisplayMasksInfo(
                /*origin_rounded_display_mask_radius=*/10,
                /*other_rounded_display_mask_radius=*/15,
                /*is_horizontally_positioned=*/true),
            /*expected_origin_mask_bounds=*/gfx::Rect(0, 0, 10, 10),
            /*expected_other_mask_bounds=*/gfx::Rect(85, 0, 15, 15)),
        std::make_tuple(
            RoundedDisplayMasksInfo::CreateRoundedDisplayMasksInfo(
                /*origin_rounded_display_mask_radius=*/10,
                /*other_rounded_display_mask_radius=*/15,
                /*is_horizontally_positioned=*/false),
            /*expected_origin_mask_bounds=*/gfx::Rect(0, 0, 10, 10),
            /*expected_other_mask_bounds=*/gfx::Rect(0, 85, 15, 15)),
        std::make_tuple(
            RoundedDisplayMasksInfo::CreateRoundedDisplayMasksInfo(
                /*origin_rounded_display_mask_radius=*/0,
                /*other_rounded_display_mask_radius=*/15,
                /*is_horizontally_positioned=*/false),
            /*expected_origin_mask_bounds=*/gfx::Rect(),
            /*expected_other_mask_bounds=*/gfx::Rect(0, 85, 15, 15)),
        std::make_tuple(RoundedDisplayMasksInfo::CreateRoundedDisplayMasksInfo(
                            /*origin_rounded_display_mask_radius=*/10,
                            /*other_rounded_display_mask_radius=*/0,
                            /*is_horizontally_positioned=*/false),
                        /*expected_origin_mask_bounds=*/gfx::Rect(0, 0, 10, 10),
                        /*expected_other_mask_bounds=*/gfx::Rect()),
        std::make_tuple(RoundedDisplayMasksInfo::CreateRoundedDisplayMasksInfo(
                            /*origin_rounded_display_mask_radius=*/0,
                            /*other_rounded_display_mask_radius=*/0,
                            /*is_horizontally_positioned=*/false),
                        /*expected_origin_mask_bounds=*/gfx::Rect(),
                        /*expected_other_mask_bounds=*/gfx::Rect())));

}  // namespace
}  // namespace viz