blob: 7d5a4d4554fe5b36d39cb634c6003c8d7c11c4a4 [file] [log] [blame]
// Copyright 2014 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 "components/viz/service/display/surface_aggregator.h"
#include <stddef.h>
#include <stdint.h>
#include <set>
#include <utility>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "cc/test/render_pass_test_utils.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/quads/draw_quad.h"
#include "components/viz/common/quads/render_pass.h"
#include "components/viz/common/quads/render_pass_draw_quad.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/surface_draw_quad.h"
#include "components/viz/common/quads/texture_draw_quad.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/service/display/display_resource_provider.h"
#include "components/viz/service/display_embedder/server_shared_bitmap_manager.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/surface.h"
#include "components/viz/service/surfaces/surface_manager.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "components/viz/test/fake_compositor_frame_sink_client.h"
#include "components/viz/test/fake_surface_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/rect_conversions.h"
namespace viz {
namespace {
using ::testing::_;
constexpr FrameSinkId kArbitraryRootFrameSinkId(1, 1);
constexpr FrameSinkId kArbitraryFrameSinkId1(2, 2);
constexpr FrameSinkId kArbitraryFrameSinkId2(3, 3);
constexpr FrameSinkId kArbitraryMiddleFrameSinkId(4, 4);
constexpr FrameSinkId kArbitraryReservedFrameSinkId(5, 5);
constexpr FrameSinkId kArbitraryFrameSinkId3(6, 6);
const base::UnguessableToken kArbitraryToken = base::UnguessableToken::Create();
constexpr bool kRootIsRoot = true;
constexpr bool kChildIsRoot = false;
constexpr bool kNeedsSyncPoints = false;
gfx::Size SurfaceSize() {
static gfx::Size size(100, 100);
return size;
}
gfx::Rect NoDamage() {
return gfx::Rect();
}
class MockAggregatedDamageCallback {
public:
MockAggregatedDamageCallback() : weak_ptr_factory_(this) {}
~MockAggregatedDamageCallback() = default;
CompositorFrameSinkSupport::AggregatedDamageCallback GetCallback() {
return base::BindRepeating(
&MockAggregatedDamageCallback::OnAggregatedDamage,
weak_ptr_factory_.GetWeakPtr());
}
MOCK_METHOD4(OnAggregatedDamage,
void(const LocalSurfaceId& local_surface_id,
const gfx::Size& frame_size_in_pixels,
const gfx::Rect& damage_rect,
base::TimeTicks expected_display_time));
private:
base::WeakPtrFactory<MockAggregatedDamageCallback> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MockAggregatedDamageCallback);
};
class DisplayTimeSource {
public:
base::TimeTicks next_display_time() const { return next_display_time_; }
base::TimeTicks GetNextDisplayTimeAndIncrement() {
const base::TimeTicks display_time = next_display_time_;
next_display_time_ += BeginFrameArgs::DefaultInterval();
return display_time;
}
private:
base::TimeTicks next_display_time_ =
base::TimeTicks() + base::TimeDelta::FromSeconds(1);
};
class SurfaceAggregatorTest : public testing::Test, public DisplayTimeSource {
public:
explicit SurfaceAggregatorTest(bool use_damage_rect)
: manager_(&shared_bitmap_manager_),
observer_(false),
support_(std::make_unique<CompositorFrameSinkSupport>(
&fake_client_,
&manager_,
kArbitraryRootFrameSinkId,
kRootIsRoot,
kNeedsSyncPoints)),
aggregator_(manager_.surface_manager(), nullptr, use_damage_rect) {
manager_.surface_manager()->AddObserver(&observer_);
}
SurfaceAggregatorTest() : SurfaceAggregatorTest(false) {}
void TearDown() override {
observer_.Reset();
testing::Test::TearDown();
}
struct Quad {
static Quad SolidColorQuad(SkColor color, const gfx::Rect& rect) {
Quad quad;
quad.material = DrawQuad::SOLID_COLOR;
quad.color = color;
quad.rect = rect;
return quad;
}
// If |fallback_surface_id| is a valid surface Id then this will generate
// two SurfaceDrawQuads.
static Quad SurfaceQuad(const SurfaceRange& surface_range,
SkColor default_background_color,
const gfx::Rect& primary_surface_rect,
bool stretch_content_to_fill_bounds,
bool ignores_input_event) {
Quad quad;
quad.material = DrawQuad::SURFACE_CONTENT;
quad.primary_surface_rect = primary_surface_rect;
quad.surface_range = surface_range;
quad.default_background_color = default_background_color;
quad.stretch_content_to_fill_bounds = stretch_content_to_fill_bounds;
quad.ignores_input_event = ignores_input_event;
return quad;
}
static Quad SurfaceQuad(const SurfaceRange& surface_range,
SkColor default_background_color,
const gfx::Rect& primary_surface_rect,
float opacity,
const gfx::Transform& transform,
bool stretch_content_to_fill_bounds,
bool ignores_input_event) {
Quad quad;
quad.material = DrawQuad::SURFACE_CONTENT;
quad.primary_surface_rect = primary_surface_rect;
quad.opacity = opacity;
quad.to_target_transform = transform;
quad.surface_range = surface_range;
quad.default_background_color = default_background_color;
quad.stretch_content_to_fill_bounds = stretch_content_to_fill_bounds;
quad.ignores_input_event = ignores_input_event;
return quad;
}
static Quad RenderPassQuad(int id) {
Quad quad;
quad.material = DrawQuad::RENDER_PASS;
quad.render_pass_id = id;
return quad;
}
DrawQuad::Material material;
// Set when material==DrawQuad::SURFACE_CONTENT.
SurfaceRange surface_range;
SkColor default_background_color;
bool stretch_content_to_fill_bounds;
bool ignores_input_event;
gfx::Rect primary_surface_rect;
float opacity;
gfx::Transform to_target_transform;
// Set when material==DrawQuad::SOLID_COLOR.
SkColor color;
gfx::Rect rect;
// Set when material==DrawQuad::RENDER_PASS.
RenderPassId render_pass_id;
private:
Quad() : material(DrawQuad::INVALID), opacity(1.f), color(SK_ColorWHITE) {}
};
struct Pass {
Pass(const std::vector<Quad>& quads, int id, const gfx::Size& size)
: quads(quads), id(id), size(size), damage_rect(size) {}
Pass(const std::vector<Quad>& quads, const gfx::Size& size)
: quads(quads), size(size), damage_rect(size) {}
Pass(const std::vector<Quad>& quads,
const gfx::Size& size,
const gfx::Rect& damage_rect)
: quads(quads), size(size), damage_rect(damage_rect) {}
const std::vector<Quad>& quads;
int id = 1;
gfx::Size size;
gfx::Rect damage_rect;
};
// |referenced_surfaces| refers to the SurfaceRanges of all the
// SurfaceDrawQuads added to the provided |pass|.
static void AddQuadInPass(const Quad& desc,
RenderPass* pass,
std::vector<SurfaceRange>* referenced_surfaces) {
switch (desc.material) {
case DrawQuad::SOLID_COLOR:
cc::AddQuad(pass, desc.rect, desc.color);
break;
case DrawQuad::SURFACE_CONTENT:
referenced_surfaces->emplace_back(desc.surface_range);
AddSurfaceQuad(pass, desc.primary_surface_rect, desc.opacity,
desc.to_target_transform, desc.surface_range,
desc.default_background_color,
desc.stretch_content_to_fill_bounds,
desc.ignores_input_event);
break;
case DrawQuad::RENDER_PASS:
AddRenderPassQuad(pass, desc.render_pass_id);
break;
default:
NOTREACHED();
}
}
static void AddPasses(RenderPassList* pass_list,
const std::vector<Pass>& passes,
std::vector<SurfaceRange>* referenced_surfaces) {
gfx::Transform root_transform;
for (auto& pass : passes) {
RenderPass* test_pass = AddRenderPassWithDamage(
pass_list, pass.id, gfx::Rect(pass.size), pass.damage_rect,
root_transform, cc::FilterOperations());
for (size_t j = 0; j < pass.quads.size(); ++j)
AddQuadInPass(pass.quads[j], test_pass, referenced_surfaces);
}
}
static void TestQuadMatchesExpectations(Quad expected_quad,
const DrawQuad* quad) {
switch (expected_quad.material) {
case DrawQuad::SOLID_COLOR: {
ASSERT_EQ(DrawQuad::SOLID_COLOR, quad->material);
const auto* solid_color_quad = SolidColorDrawQuad::MaterialCast(quad);
EXPECT_EQ(expected_quad.color, solid_color_quad->color);
EXPECT_EQ(expected_quad.rect, solid_color_quad->rect);
break;
}
case DrawQuad::RENDER_PASS: {
ASSERT_EQ(DrawQuad::RENDER_PASS, quad->material);
const auto* render_pass_quad = RenderPassDrawQuad::MaterialCast(quad);
EXPECT_EQ(expected_quad.render_pass_id,
render_pass_quad->render_pass_id);
break;
}
default:
NOTREACHED();
break;
}
}
static void TestPassMatchesExpectations(Pass expected_pass,
const RenderPass* pass) {
ASSERT_EQ(expected_pass.quads.size(), pass->quad_list.size());
for (auto iter = pass->quad_list.cbegin(); iter != pass->quad_list.cend();
++iter) {
SCOPED_TRACE(base::StringPrintf("Quad number %" PRIuS, iter.index()));
TestQuadMatchesExpectations(expected_pass.quads[iter.index()], *iter);
}
}
static void TestPassesMatchExpectations(
const std::vector<Pass>& expected_passes,
const RenderPassList* passes) {
ASSERT_EQ(expected_passes.size(), passes->size());
for (size_t i = 0; i < expected_passes.size(); ++i) {
SCOPED_TRACE(base::StringPrintf("Pass number %" PRIuS, i));
RenderPass* pass = (*passes)[i].get();
TestPassMatchesExpectations(expected_passes[i], pass);
}
}
private:
static void AddSurfaceQuad(RenderPass* pass,
const gfx::Rect& primary_surface_rect,
float opacity,
const gfx::Transform& transform,
const SurfaceRange& surface_range,
SkColor default_background_color,
bool stretch_content_to_fill_bounds,
bool ignores_input_event) {
gfx::Transform layer_to_target_transform = transform;
gfx::Rect layer_bounds(primary_surface_rect);
gfx::Rect visible_layer_rect(primary_surface_rect);
gfx::Rect clip_rect(primary_surface_rect);
bool is_clipped = false;
bool are_contents_opaque = false;
SkBlendMode blend_mode = SkBlendMode::kSrcOver;
auto* shared_quad_state = pass->CreateAndAppendSharedQuadState();
shared_quad_state->SetAll(layer_to_target_transform, layer_bounds,
visible_layer_rect, clip_rect, is_clipped,
are_contents_opaque, opacity, blend_mode, 0);
SurfaceDrawQuad* surface_quad =
pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
surface_quad->SetNew(pass->shared_quad_state_list.back(),
primary_surface_rect, primary_surface_rect,
surface_range, default_background_color,
stretch_content_to_fill_bounds, ignores_input_event);
}
static void AddRenderPassQuad(RenderPass* pass, RenderPassId render_pass_id) {
gfx::Rect output_rect = gfx::Rect(0, 0, 5, 5);
auto* shared_state = pass->CreateAndAppendSharedQuadState();
shared_state->SetAll(gfx::Transform(), output_rect, output_rect,
output_rect, false, false, 1, SkBlendMode::kSrcOver,
0);
auto* quad = pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
quad->SetNew(shared_state, output_rect, output_rect, render_pass_id, 0,
gfx::RectF(), gfx::Size(), gfx::Vector2dF(), gfx::PointF(),
gfx::RectF(), false, 1.0f);
}
protected:
ServerSharedBitmapManager shared_bitmap_manager_;
FrameSinkManagerImpl manager_;
FakeSurfaceObserver observer_;
FakeCompositorFrameSinkClient fake_client_;
std::unique_ptr<CompositorFrameSinkSupport> support_;
SurfaceAggregator aggregator_;
};
class SurfaceAggregatorValidSurfaceTest : public SurfaceAggregatorTest {
public:
explicit SurfaceAggregatorValidSurfaceTest(bool use_damage_rect)
: SurfaceAggregatorTest(use_damage_rect),
child_support_(std::make_unique<CompositorFrameSinkSupport>(
nullptr,
&manager_,
kArbitraryReservedFrameSinkId,
kChildIsRoot,
kNeedsSyncPoints)) {
child_support_->set_allow_copy_output_requests_for_testing();
}
SurfaceAggregatorValidSurfaceTest()
: SurfaceAggregatorValidSurfaceTest(false) {}
void SetUp() override {
SurfaceAggregatorTest::SetUp();
allocator_.GenerateId();
root_local_surface_id_ =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
root_surface_ = manager_.surface_manager()->GetSurfaceForId(
SurfaceId(support_->frame_sink_id(), root_local_surface_id_));
}
void TearDown() override { SurfaceAggregatorTest::TearDown(); }
// Verifies that if the |SharedQuadState::quad_layer_rect| can be covered by
// |DrawQuad::Rect| in the SharedQuadState.
void VerifyQuadCoverSQS(CompositorFrame* aggregated_frame) {
const SharedQuadState* shared_quad_state = nullptr;
gfx::Rect draw_quad_coverage;
for (size_t i = 0; i < aggregated_frame->render_pass_list.size(); ++i) {
for (auto quad =
aggregated_frame->render_pass_list[i]->quad_list.cbegin();
quad != aggregated_frame->render_pass_list[i]->quad_list.cend();
++quad) {
if (shared_quad_state != quad->shared_quad_state) {
if (shared_quad_state)
EXPECT_EQ(shared_quad_state->quad_layer_rect, draw_quad_coverage);
shared_quad_state = quad->shared_quad_state;
draw_quad_coverage = quad->rect;
}
draw_quad_coverage.Union(quad->rect);
}
}
}
void AggregateAndVerify(const std::vector<Pass>& expected_passes,
const std::vector<SurfaceId>& expected_surface_ids) {
CompositorFrame aggregated_frame = aggregator_.Aggregate(
SurfaceId(support_->frame_sink_id(), root_local_surface_id_),
GetNextDisplayTimeAndIncrement());
TestPassesMatchExpectations(expected_passes,
&aggregated_frame.render_pass_list);
VerifyQuadCoverSQS(&aggregated_frame);
// Ensure no duplicate pass ids output.
std::set<RenderPassId> used_passes;
for (const auto& pass : aggregated_frame.render_pass_list)
EXPECT_TRUE(used_passes.insert(pass->id).second);
EXPECT_EQ(expected_surface_ids.size(),
aggregator_.previous_contained_surfaces().size());
for (const SurfaceId& surface_id : expected_surface_ids) {
EXPECT_THAT(aggregator_.previous_contained_surfaces(),
testing::Contains(testing::Key(surface_id)));
EXPECT_THAT(
aggregator_.previous_contained_frame_sinks(),
testing::Contains(testing::Pair(surface_id.frame_sink_id(),
surface_id.local_surface_id())));
}
}
void SubmitPassListAsFrame(CompositorFrameSinkSupport* support,
const LocalSurfaceId& local_surface_id,
RenderPassList* pass_list,
std::vector<SurfaceRange> referenced_surfaces,
float device_scale_factor) {
CompositorFrame frame =
CompositorFrameBuilder()
.SetRenderPassList(std::move(*pass_list))
.SetDeviceScaleFactor(device_scale_factor)
.SetReferencedSurfaces(std::move(referenced_surfaces))
.Build();
pass_list->clear();
support->SubmitCompositorFrame(local_surface_id, std::move(frame));
}
RenderPassList GenerateRenderPassList(
const std::vector<Pass>& passes,
std::vector<SurfaceRange>* referenced_surfaces) {
RenderPassList pass_list;
AddPasses(&pass_list, passes, referenced_surfaces);
return pass_list;
}
void SubmitCompositorFrame(CompositorFrameSinkSupport* support,
const std::vector<Pass>& passes,
const LocalSurfaceId& local_surface_id,
float device_scale_factor) {
std::vector<SurfaceRange> referenced_surfaces;
RenderPassList pass_list =
GenerateRenderPassList(passes, &referenced_surfaces);
SubmitPassListAsFrame(support, local_surface_id, &pass_list,
std::move(referenced_surfaces), device_scale_factor);
}
CompositorFrame MakeCompositorFrameFromSurfaceRanges(
const std::vector<SurfaceRange>& ranges) {
std::vector<Quad> quads;
for (const SurfaceRange& range : ranges) {
quads.push_back(Quad::SurfaceQuad(
range, SK_ColorWHITE, gfx::Rect(5, 5), 1.f, gfx::Transform(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false));
}
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
RenderPassList pass_list;
std::vector<SurfaceRange> referenced_surfaces;
AddPasses(&pass_list, passes, &referenced_surfaces);
return CompositorFrameBuilder()
.SetRenderPassList(std::move(pass_list))
.SetDeviceScaleFactor(1.f)
.SetReferencedSurfaces(ranges)
.Build();
}
void QueuePassAsFrame(std::unique_ptr<RenderPass> pass,
const LocalSurfaceId& local_surface_id,
float device_scale_factor,
CompositorFrameSinkSupport* support) {
CompositorFrame child_frame = CompositorFrameBuilder()
.AddRenderPass(std::move(pass))
.SetDeviceScaleFactor(device_scale_factor)
.Build();
support->SubmitCompositorFrame(local_surface_id, std::move(child_frame));
}
protected:
LocalSurfaceId root_local_surface_id_;
Surface* root_surface_;
ParentLocalSurfaceIdAllocator allocator_;
std::unique_ptr<CompositorFrameSinkSupport> child_support_;
ParentLocalSurfaceIdAllocator child_allocator_;
};
// Tests that a very simple frame containing only two solid color quads makes it
// through the aggregator correctly.
TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleFrame) {
std::vector<Quad> quads = {
Quad::SolidColorQuad(SK_ColorRED, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
// Add a callback for when the surface is damaged.
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
// Check that the AggregatedDamageCallback is called with the right arguments.
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()));
AggregateAndVerify(passes, {root_surface_id});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
}
// Test that when surface is translucent and we need the render surface to apply
// the opacity, we would keep the render surface.
TEST_F(SurfaceAggregatorValidSurfaceTest, OpacityCopied) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kRootIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
{
std::vector<Quad> quads = {
Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5), .5f, gfx::Transform(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
CompositorFrame aggregated_frame = aggregator_.Aggregate(
root_surface_id, GetNextDisplayTimeAndIncrement());
auto& render_pass_list = aggregated_frame.render_pass_list;
EXPECT_EQ(2u, render_pass_list.size());
auto& shared_quad_state_list2 = render_pass_list[1]->shared_quad_state_list;
ASSERT_EQ(1u, shared_quad_state_list2.size());
EXPECT_EQ(.5f, shared_quad_state_list2.ElementAt(0)->opacity);
}
// For the case where opacity is close to 1.f, we treat it as opaque, and not
// use a render surface.
{
std::vector<Quad> quads = {Quad::SurfaceQuad(
SurfaceRange(base::nullopt, embedded_surface_id), SK_ColorWHITE,
gfx::Rect(5, 5), .9999f, gfx::Transform(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
CompositorFrame aggregated_frame = aggregator_.Aggregate(
root_surface_id, GetNextDisplayTimeAndIncrement());
auto& render_pass_list = aggregated_frame.render_pass_list;
EXPECT_EQ(1u, render_pass_list.size());
}
}
// Test that when surface is rotated and we need the render surface to apply the
// clip, we would keep the render surface.
TEST_F(SurfaceAggregatorValidSurfaceTest, RotatedClip) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kRootIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
gfx::Transform rotate;
rotate.Rotate(30);
std::vector<Quad> quads = {Quad::SurfaceQuad(
SurfaceRange(base::nullopt, embedded_surface_id), SK_ColorWHITE,
gfx::Rect(5, 5), 1.f, rotate, /*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
auto& render_pass_list = aggregated_frame.render_pass_list;
EXPECT_EQ(2u, render_pass_list.size());
auto& shared_quad_state_list2 =
render_pass_list.back()->shared_quad_state_list;
EXPECT_EQ(rotate, shared_quad_state_list2.front()->quad_to_target_transform);
}
TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSimpleFrame) {
std::vector<Quad> quads[2] = {
{Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorLTGRAY, gfx::Rect(5, 5))},
{Quad::SolidColorQuad(SK_ColorGRAY, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorDKGRAY, gfx::Rect(5, 5))}};
std::vector<Pass> passes = {Pass(quads[0], 1, SurfaceSize()),
Pass(quads[1], 2, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(passes, {root_surface_id});
}
// Ensure that the render pass ID map properly keeps and deletes entries.
TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassDeallocation) {
std::vector<Quad> quads[2] = {
{Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorLTGRAY, gfx::Rect(5, 5))},
{Quad::SolidColorQuad(SK_ColorGRAY, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorDKGRAY, gfx::Rect(5, 5))}};
std::vector<Pass> passes = {Pass(quads[0], 2, SurfaceSize()),
Pass(quads[1], 1, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
SurfaceId surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame;
aggregated_frame =
aggregator_.Aggregate(surface_id, GetNextDisplayTimeAndIncrement());
auto id0 = aggregated_frame.render_pass_list[0]->id;
auto id1 = aggregated_frame.render_pass_list[1]->id;
EXPECT_NE(id1, id0);
// Aggregated RenderPass ids should remain the same between frames.
aggregated_frame =
aggregator_.Aggregate(surface_id, GetNextDisplayTimeAndIncrement());
EXPECT_EQ(id0, aggregated_frame.render_pass_list[0]->id);
EXPECT_EQ(id1, aggregated_frame.render_pass_list[1]->id);
std::vector<Pass> passes2 = {Pass(quads[0], 3, SurfaceSize()),
Pass(quads[1], 1, SurfaceSize())};
SubmitCompositorFrame(support_.get(), passes2, root_local_surface_id_,
device_scale_factor);
// The RenderPass that still exists should keep the same ID.
aggregated_frame =
aggregator_.Aggregate(surface_id, GetNextDisplayTimeAndIncrement());
auto id2 = aggregated_frame.render_pass_list[0]->id;
EXPECT_NE(id2, id1);
EXPECT_NE(id2, id0);
EXPECT_EQ(id1, aggregated_frame.render_pass_list[1]->id);
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
// |id1| didn't exist in the previous frame, so it should be
// mapped to a new ID.
aggregated_frame =
aggregator_.Aggregate(surface_id, GetNextDisplayTimeAndIncrement());
auto id3 = aggregated_frame.render_pass_list[0]->id;
EXPECT_NE(id3, id2);
EXPECT_NE(id3, id1);
EXPECT_NE(id3, id0);
EXPECT_EQ(id1, aggregated_frame.render_pass_list[1]->id);
}
// This tests very simple embedding. root_surface has a frame containing a few
// solid color quads and a surface quad referencing embedded_surface.
// embedded_surface has a frame containing only a solid color quad. The solid
// color quad should be aggregated into the final frame.
TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleSurfaceReference) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kRootIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id, embedded_surface_id});
}
class TestVizClient {
public:
TestVizClient(SurfaceAggregatorValidSurfaceTest* test,
FrameSinkManagerImpl* manager,
const FrameSinkId& frame_sink_id,
const LocalSurfaceId& local_surface_id,
const gfx::Rect& bounds)
: test_(test),
manager_(manager),
frame_sink_id_(frame_sink_id),
local_surface_id_(local_surface_id),
bounds_(bounds) {
constexpr bool is_root = false;
constexpr bool needs_sync_points = false;
support_ = std::make_unique<CompositorFrameSinkSupport>(
nullptr, manager_, frame_sink_id, is_root, needs_sync_points);
}
~TestVizClient() = default;
Surface* GetSurface() const {
return manager_->surface_manager()->GetSurfaceForId(
SurfaceId(frame_sink_id_, local_surface_id_));
}
void SubmitCompositorFrame(SkColor bgcolor) {
using Quad = SurfaceAggregatorValidSurfaceTest::Quad;
using Pass = SurfaceAggregatorValidSurfaceTest::Pass;
std::vector<SurfaceRange> referenced_surfaces;
std::vector<Quad> embedded_quads = {Quad::SolidColorQuad(bgcolor, bounds_)};
for (const auto& embed : embedded_clients_) {
if (embed.second) {
embedded_quads.push_back(Quad::SurfaceQuad(
SurfaceRange(base::nullopt, embed.first->surface_id()),
SK_ColorWHITE, embed.first->bounds(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false));
} else {
referenced_surfaces.emplace_back(
SurfaceRange(base::nullopt, embed.first->surface_id()));
}
}
std::vector<Pass> embedded_passes = {Pass(embedded_quads, bounds_.size())};
constexpr float device_scale_factor = 1.0f;
RenderPassList pass_list =
test_->GenerateRenderPassList(embedded_passes, &referenced_surfaces);
test_->SubmitPassListAsFrame(support_.get(), local_surface_id_, &pass_list,
referenced_surfaces, device_scale_factor);
}
void SetEmbeddedClient(TestVizClient* embedded, bool add_quad) {
embedded_clients_[embedded] = add_quad;
}
CopyOutputRequest* RequestCopyOfOutput() {
auto copy_request = CopyOutputRequest::CreateStubForTesting();
auto* copy_request_ptr = copy_request.get();
support_->RequestCopyOfOutput(local_surface_id_, std::move(copy_request));
return copy_request_ptr;
}
SurfaceId surface_id() const { return {frame_sink_id_, local_surface_id_}; }
const gfx::Rect& bounds() const { return bounds_; }
private:
SurfaceAggregatorValidSurfaceTest* const test_;
FrameSinkManagerImpl* const manager_;
std::unique_ptr<CompositorFrameSinkSupport> support_;
const FrameSinkId frame_sink_id_;
const LocalSurfaceId local_surface_id_;
const gfx::Rect bounds_;
std::map<TestVizClient*, bool> embedded_clients_;
DISALLOW_COPY_AND_ASSIGN(TestVizClient);
};
TEST_F(SurfaceAggregatorValidSurfaceTest, UndrawnSurfaces) {
allocator_.GenerateId();
TestVizClient child(
this, &manager_, kArbitraryFrameSinkId1,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(10, 10));
child.SubmitCompositorFrame(SK_ColorBLUE);
// Parent first submits a CompositorFrame that renfereces |child|, but does
// not provide a DrawQuad that embeds it.
allocator_.GenerateId();
TestVizClient parent(
this, &manager_, kArbitraryFrameSinkId2,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(15, 15));
parent.SetEmbeddedClient(&child, false);
parent.SubmitCompositorFrame(SK_ColorGREEN);
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, parent.surface_id()),
SK_ColorWHITE, parent.bounds(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, parent.bounds()),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id, parent.surface_id(),
child.surface_id()});
// |child| should not be drawn.
EXPECT_TRUE(child.GetSurface()->HasUndrawnActiveFrame());
EXPECT_FALSE(parent.GetSurface()->HasUndrawnActiveFrame());
// Submit another CompositorFrame from |parent|, this time with a DrawQuad for
// |child|.
parent.SetEmbeddedClient(&child, true);
parent.SubmitCompositorFrame(SK_ColorGREEN);
expected_quads = {Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, parent.bounds()),
Quad::SolidColorQuad(SK_ColorBLUE, child.bounds()),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
AggregateAndVerify(
{Pass(expected_quads, SurfaceSize())},
{root_surface_id, parent.surface_id(), child.surface_id()});
EXPECT_FALSE(child.GetSurface()->HasUndrawnActiveFrame());
}
TEST_F(SurfaceAggregatorValidSurfaceTest, UndrawnSurfacesWithCopyRequests) {
allocator_.GenerateId();
TestVizClient child(
this, &manager_, kArbitraryFrameSinkId1,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(10, 10));
child.SubmitCompositorFrame(SK_ColorBLUE);
child.RequestCopyOfOutput();
// Parent first submits a CompositorFrame that renfereces |child|, but does
// not provide a DrawQuad that embeds it.
allocator_.GenerateId();
TestVizClient parent(
this, &manager_, kArbitraryFrameSinkId2,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(15, 15));
parent.SetEmbeddedClient(&child, false);
parent.SubmitCompositorFrame(SK_ColorGREEN);
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, parent.surface_id()),
SK_ColorWHITE, parent.bounds(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, parent.bounds()),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Quad> expected_copy_quads = {
Quad::SolidColorQuad(SK_ColorBLUE, child.bounds())};
std::vector<Pass> expected_passes = {Pass(expected_copy_quads, SurfaceSize()),
Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id, parent.surface_id(),
child.surface_id()});
EXPECT_FALSE(child.GetSurface()->HasUndrawnActiveFrame());
EXPECT_FALSE(parent.GetSurface()->HasUndrawnActiveFrame());
}
TEST_F(SurfaceAggregatorValidSurfaceTest,
SurfacesWithMultipleEmbeddersBothVisibleAndInvisible) {
allocator_.GenerateId();
TestVizClient child(
this, &manager_, kArbitraryFrameSinkId1,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(10, 10));
child.SubmitCompositorFrame(SK_ColorBLUE);
// First parent submits a CompositorFrame that renfereces |child|, but does
// not provide a DrawQuad that embeds it.
allocator_.GenerateId();
TestVizClient first_parent(
this, &manager_, kArbitraryFrameSinkId2,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(15, 15));
first_parent.SetEmbeddedClient(&child, false);
first_parent.SubmitCompositorFrame(SK_ColorGREEN);
// Second parent submits a CompositorFrame referencing |child|, and also
// includes a draw-quad for it.
allocator_.GenerateId();
TestVizClient second_parent(
this, &manager_, kArbitraryMiddleFrameSinkId,
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id(),
gfx::Rect(25, 25));
second_parent.SetEmbeddedClient(&child, true);
second_parent.SubmitCompositorFrame(SK_ColorYELLOW);
// Submit a root CompositorFrame that embeds both parents.
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, first_parent.surface_id()),
SK_ColorCYAN, first_parent.bounds(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, second_parent.surface_id()),
SK_ColorMAGENTA, second_parent.bounds(),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
EXPECT_TRUE(child.GetSurface()->HasUndrawnActiveFrame());
EXPECT_TRUE(first_parent.GetSurface()->HasUndrawnActiveFrame());
EXPECT_TRUE(second_parent.GetSurface()->HasUndrawnActiveFrame());
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, first_parent.bounds()),
Quad::SolidColorQuad(SK_ColorYELLOW, second_parent.bounds()),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(10, 10)),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Quad> expected_copy_quads = {};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes,
{root_surface_id, first_parent.surface_id(),
second_parent.surface_id(), child.surface_id()});
EXPECT_FALSE(child.GetSurface()->HasUndrawnActiveFrame());
EXPECT_FALSE(first_parent.GetSurface()->HasUndrawnActiveFrame());
EXPECT_FALSE(second_parent.GetSurface()->HasUndrawnActiveFrame());
}
// This test verifies that in the absence of a primary Surface,
// SurfaceAggregator will embed a fallback Surface, if available. If the primary
// Surface is available, though, the fallback will not be used.
TEST_F(SurfaceAggregatorValidSurfaceTest, FallbackSurfaceReference) {
auto primary_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
auto fallback_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId2, kChildIsRoot,
kNeedsSyncPoints);
child_allocator_.GenerateId();
LocalSurfaceId fallback_child_local_surface_id =
child_allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId fallback_child_surface_id(fallback_child_support->frame_sink_id(),
fallback_child_local_surface_id);
child_allocator_.GenerateId();
LocalSurfaceId primary_child_local_surface_id =
child_allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId primary_child_surface_id(primary_child_support->frame_sink_id(),
primary_child_local_surface_id);
constexpr gfx::Size fallback_size(10, 10);
std::vector<Quad> fallback_child_quads = {
Quad::SolidColorQuad(SK_ColorRED, gfx::Rect(fallback_size))};
std::vector<Pass> fallback_child_passes = {
Pass(fallback_child_quads, fallback_size)};
// Submit a CompositorFrame to the fallback Surface containing a red
// SolidColorDrawQuad.
constexpr float device_scale_factor_1 = 1.0f;
constexpr float device_scale_factor_2 = 2.0f;
SubmitCompositorFrame(fallback_child_support.get(), fallback_child_passes,
fallback_child_local_surface_id, device_scale_factor_2);
// Try to embed |primary_child_surface_id| and if unavailable, embed
// |fallback_child_surface_id|.
constexpr gfx::Rect surface_quad_rect(12, 15);
std::vector<Quad> root_quads = {Quad::SurfaceQuad(
SurfaceRange(fallback_child_surface_id, primary_child_surface_id),
SK_ColorWHITE, surface_quad_rect,
/*stretch_content_to_fill_bounds=*/false, /*ignores_input_event=*/false)};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
primary_child_support->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
fallback_child_support->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor_1);
// There is no CompositorFrame submitted to |primary_child_surface_id| and
// so |fallback_child_surface_id| will be embedded and we should see a red
// SolidColorDrawQuad. These quads are in physical pixels.
std::vector<Quad> expected_quads1 = {
// Right gutter.
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 0, 7, 15)),
// Bottom guttter.
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(0, 5, 5, 10)),
// Contents of the fallback surface.
Quad::SolidColorQuad(SK_ColorRED, gfx::Rect(fallback_size)),
};
std::vector<Pass> expected_passes1 = {Pass(expected_quads1, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(fallback_child_local_surface_id, fallback_size,
gfx::Rect(fallback_size), next_display_time()))
.Times(1);
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(primary_child_local_surface_id, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()))
.Times(0);
// The whole root surface should be damaged because this is the first
// aggregation.
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()))
.Times(1);
// The primary_surface will not be listed in previously contained surfaces.
AggregateAndVerify(expected_passes1,
{root_surface_id, fallback_child_surface_id});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
// Submit the fallback again to create some damage then aggregate again.
child_allocator_.GenerateId();
fallback_child_local_surface_id =
child_allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SubmitCompositorFrame(fallback_child_support.get(), fallback_child_passes,
fallback_child_local_surface_id, device_scale_factor_2);
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(fallback_child_local_surface_id, _, _, _));
// The damage should be equal to whole size of the primary SurfaceDrawQuad.
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
surface_quad_rect, testing::A<base::TimeTicks>()))
.Times(1);
AggregateAndVerify(
expected_passes1,
{root_surface_id, SurfaceId(fallback_child_support->frame_sink_id(),
fallback_child_local_surface_id)});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
std::vector<Quad> primary_child_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
constexpr gfx::Size primary_surface_size(5, 5);
std::vector<Pass> primary_child_passes = {
Pass(primary_child_quads, primary_surface_size)};
// Submit a CompositorFrame to the primary Surface containing a green
// SolidColorDrawQuad.
SubmitCompositorFrame(primary_child_support.get(), primary_child_passes,
primary_child_local_surface_id, device_scale_factor_2);
// Now that the primary Surface has a CompositorFrame, we expect
// SurfaceAggregator to embed the primary Surface, and drop the fallback
// Surface.
std::vector<Quad> expected_quads2 = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes2 = {Pass(expected_quads2, SurfaceSize())};
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(primary_child_local_surface_id, primary_surface_size,
gfx::Rect(primary_surface_size), next_display_time()))
.Times(1);
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(fallback_child_local_surface_id, fallback_size,
gfx::Rect(fallback_size), next_display_time()))
.Times(0);
// The damage of the root should be equal to the damage of the primary
// surface.
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(primary_surface_size), next_display_time()))
.Times(1);
AggregateAndVerify(expected_passes2,
{root_surface_id, primary_child_surface_id});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
}
// This test verifies that the appropriate transform will be applied to a
// surface embedded by a parent SurfaceDrawQuad marked as
// stretch_content_to_fill_bounds.
TEST_F(SurfaceAggregatorValidSurfaceTest, StretchContentToFillBounds) {
auto primary_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId primary_child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId primary_child_surface_id(primary_child_support->frame_sink_id(),
primary_child_local_surface_id);
{
auto pass = RenderPass::Create();
pass->SetNew(1, gfx::Rect(0, 0, 20, 20), gfx::Rect(), gfx::Transform());
auto* sqs = pass->CreateAndAppendSharedQuadState();
auto* solid_color_quad =
pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
solid_color_quad->SetNew(sqs, gfx::Rect(0, 0, 20, 20),
gfx::Rect(0, 0, 20, 20), SK_ColorRED, false);
CompositorFrame frame =
CompositorFrameBuilder().AddRenderPass(std::move(pass)).Build();
primary_child_support->SubmitCompositorFrame(primary_child_local_surface_id,
std::move(frame));
}
constexpr gfx::Rect surface_quad_rect(10, 5);
std::vector<Quad> root_quads = {Quad::SurfaceQuad(
SurfaceRange(primary_child_surface_id), SK_ColorWHITE, surface_quad_rect,
/*stretch_content_to_fill_bounds=*/true, /*ignores_input_event=*/false)};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
1.0f);
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()));
CompositorFrame frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
EXPECT_EQ(1u, frame.render_pass_list.size());
auto* render_pass = frame.render_pass_list.back().get();
EXPECT_EQ(1u, render_pass->quad_list.size());
auto* output_quad = render_pass->quad_list.back();
EXPECT_EQ(DrawQuad::SOLID_COLOR, output_quad->material);
gfx::RectF output_rect(100.f, 100.f);
// SurfaceAggregator should stretch the SolidColorDrawQuad to fit the bounds
// of the parent's SurfaceDrawQuad.
output_quad->shared_quad_state->quad_to_target_transform.TransformRect(
&output_rect);
EXPECT_EQ(gfx::RectF(50.f, 25.f), output_rect);
}
// This test verifies that the appropriate transform will be applied to a
// surface embedded by a parent SurfaceDrawQuad marked as
// stretch_content_to_fill_bounds when the device_scale_factor is
// greater than 1.
TEST_F(SurfaceAggregatorValidSurfaceTest, StretchContentToFillStretchedBounds) {
auto primary_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId primary_child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId primary_child_surface_id(primary_child_support->frame_sink_id(),
primary_child_local_surface_id);
{
auto pass = RenderPass::Create();
pass->SetNew(1, gfx::Rect(0, 0, 20, 20), gfx::Rect(), gfx::Transform());
auto* sqs = pass->CreateAndAppendSharedQuadState();
auto* solid_color_quad =
pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
solid_color_quad->SetNew(sqs, gfx::Rect(0, 0, 20, 20),
gfx::Rect(0, 0, 20, 20), SK_ColorRED, false);
CompositorFrame frame =
CompositorFrameBuilder().AddRenderPass(std::move(pass)).Build();
primary_child_support->SubmitCompositorFrame(primary_child_local_surface_id,
std::move(frame));
}
constexpr gfx::Rect surface_quad_rect(10, 5);
std::vector<Quad> root_quads = {Quad::SurfaceQuad(
SurfaceRange(primary_child_surface_id), SK_ColorWHITE, surface_quad_rect,
/*stretch_content_to_fill_bounds=*/true, /*ignores_input_event=*/false)};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
2.0f);
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()));
CompositorFrame frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
EXPECT_EQ(1u, frame.render_pass_list.size());
auto* render_pass = frame.render_pass_list.back().get();
EXPECT_EQ(1u, render_pass->quad_list.size());
auto* output_quad = render_pass->quad_list.back();
EXPECT_EQ(DrawQuad::SOLID_COLOR, output_quad->material);
gfx::RectF output_rect(200.f, 200.f);
// SurfaceAggregator should stretch the SolidColorDrawQuad to fit the bounds
// of the parent's SurfaceDrawQuad.
output_quad->shared_quad_state->quad_to_target_transform.TransformRect(
&output_rect);
EXPECT_EQ(gfx::RectF(100.f, 50.f), output_rect);
}
// This test verifies that the appropriate transform will be applied to a
// surface embedded by a parent SurfaceDrawQuad marked as
// stretch_content_to_fill_bounds when the device_scale_factor is
// less than 1.
TEST_F(SurfaceAggregatorValidSurfaceTest, StretchContentToFillSquashedBounds) {
auto primary_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId primary_child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId primary_child_surface_id(primary_child_support->frame_sink_id(),
primary_child_local_surface_id);
{
auto pass = RenderPass::Create();
pass->SetNew(1, gfx::Rect(0, 0, 20, 20), gfx::Rect(), gfx::Transform());
auto* sqs = pass->CreateAndAppendSharedQuadState();
auto* solid_color_quad =
pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
solid_color_quad->SetNew(sqs, gfx::Rect(0, 0, 20, 20),
gfx::Rect(0, 0, 20, 20), SK_ColorRED, false);
CompositorFrame frame =
CompositorFrameBuilder().AddRenderPass(std::move(pass)).Build();
primary_child_support->SubmitCompositorFrame(primary_child_local_surface_id,
std::move(frame));
}
constexpr gfx::Rect surface_quad_rect(10, 5);
std::vector<Quad> root_quads = {Quad::SurfaceQuad(
SurfaceRange(primary_child_surface_id), SK_ColorWHITE, surface_quad_rect,
/*stretch_content_to_fill_bounds=*/true, /*ignores_input_event=*/false)};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
0.5f);
EXPECT_CALL(
aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, SurfaceSize(),
gfx::Rect(SurfaceSize()), next_display_time()));
CompositorFrame frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
EXPECT_EQ(1u, frame.render_pass_list.size());
auto* render_pass = frame.render_pass_list.back().get();
EXPECT_EQ(1u, render_pass->quad_list.size());
auto* output_quad = render_pass->quad_list.back();
EXPECT_EQ(DrawQuad::SOLID_COLOR, output_quad->material);
gfx::RectF output_rect(50.f, 50.f);
// SurfaceAggregator should stretch the SolidColorDrawQuad to fit the bounds
// of the parent's SurfaceDrawQuad.
output_quad->shared_quad_state->quad_to_target_transform.TransformRect(
&output_rect);
EXPECT_EQ(gfx::RectF(25.f, 12.5f), output_rect);
}
// This test verifies that in the presence of both primary Surface and fallback
// Surface, the fallback will not be used.
TEST_F(SurfaceAggregatorValidSurfaceTest, FallbackSurfaceReferenceWithPrimary) {
auto primary_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId primary_child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId primary_child_surface_id(primary_child_support->frame_sink_id(),
primary_child_local_surface_id);
std::vector<Quad> primary_child_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
constexpr gfx::Size primary_size(50, 50);
std::vector<Pass> primary_child_passes = {
Pass(primary_child_quads, primary_size)};
// Submit a CompositorFrame to the primary Surface containing a green
// SolidColorDrawQuad.
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(primary_child_support.get(), primary_child_passes,
primary_child_local_surface_id, device_scale_factor);
auto fallback_child_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId2, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId fallback_child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId fallback_child_surface_id(fallback_child_support->frame_sink_id(),
fallback_child_local_surface_id);
std::vector<Quad> fallback_child_quads = {
Quad::SolidColorQuad(SK_ColorRED, gfx::Rect(5, 5))};
std::vector<Pass> fallback_child_passes = {
Pass(fallback_child_quads, SurfaceSize())};
// Submit a CompositorFrame to the fallback Surface containing a red
// SolidColorDrawQuad.
SubmitCompositorFrame(fallback_child_support.get(), fallback_child_passes,
fallback_child_local_surface_id, device_scale_factor);
// Try to embed |primary_child_surface_id| and if unavailabe, embed
// |fallback_child_surface_id|.
std::vector<Quad> root_quads = {Quad::SurfaceQuad(
SurfaceRange(fallback_child_surface_id, primary_child_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5), /*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
constexpr gfx::Size root_size(75, 75);
std::vector<Pass> root_passes = {Pass(root_quads, root_size, NoDamage())};
MockAggregatedDamageCallback aggregated_damage_callback;
support_->SetAggregatedDamageCallbackForTesting(
aggregated_damage_callback.GetCallback());
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
// The CompositorFrame is submitted to |primary_child_surface_id|, so
// |fallback_child_surface_id| will not be used and we should see a green
// SolidColorDrawQuad.
std::vector<Quad> expected_quads1 = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes1 = {Pass(expected_quads1, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, root_size,
gfx::Rect(root_size), next_display_time()));
// The fallback will not be contained within the aggregated frame.
AggregateAndVerify(expected_passes1,
{root_surface_id, primary_child_surface_id});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
// Submit a new frame to the primary surface to cause some damage.
SubmitCompositorFrame(primary_child_support.get(), primary_child_passes,
primary_child_local_surface_id, device_scale_factor);
// The size of the damage should be equal to the size of the primary surface.
EXPECT_CALL(aggregated_damage_callback,
OnAggregatedDamage(root_local_surface_id_, root_size,
gfx::Rect(primary_size), next_display_time()));
// Generate a new aggregated frame.
AggregateAndVerify(expected_passes1,
{root_surface_id, primary_child_surface_id});
testing::Mock::VerifyAndClearExpectations(&aggregated_damage_callback);
}
TEST_F(SurfaceAggregatorValidSurfaceTest, CopyRequest) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
auto copy_request = CopyOutputRequest::CreateStubForTesting();
auto* copy_request_ptr = copy_request.get();
embedded_support->RequestCopyOfOutput(embedded_local_surface_id,
std::move(copy_request));
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::RenderPassQuad(aggregated_frame.render_pass_list[0]->id),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(embedded_quads, SurfaceSize()),
Pass(expected_quads, SurfaceSize())};
TestPassesMatchExpectations(expected_passes,
&aggregated_frame.render_pass_list);
ASSERT_EQ(2u, aggregated_frame.render_pass_list.size());
ASSERT_EQ(1u, aggregated_frame.render_pass_list[0]->copy_requests.size());
DCHECK_EQ(copy_request_ptr,
aggregated_frame.render_pass_list[0]->copy_requests[0].get());
SurfaceId surface_ids[] = {root_surface_id, embedded_surface_id};
EXPECT_EQ(base::size(surface_ids),
aggregator_.previous_contained_surfaces().size());
for (size_t i = 0; i < base::size(surface_ids); i++) {
EXPECT_TRUE(
aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
aggregator_.previous_contained_surfaces().end());
}
}
// Root surface may contain copy requests.
TEST_F(SurfaceAggregatorValidSurfaceTest, RootCopyRequest) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId2, kChildIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
auto copy_request(CopyOutputRequest::CreateStubForTesting());
auto* copy_request_ptr = copy_request.get();
auto copy_request2(CopyOutputRequest::CreateStubForTesting());
auto* copy_request2_ptr = copy_request2.get();
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Quad> root_quads2 = {
Quad::SolidColorQuad(SK_ColorRED, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, 1, SurfaceSize()),
Pass(root_quads2, 2, SurfaceSize())};
{
CompositorFrame frame = MakeEmptyCompositorFrame();
AddPasses(&frame.render_pass_list, root_passes,
&frame.metadata.referenced_surfaces);
frame.render_pass_list[0]->copy_requests.push_back(std::move(copy_request));
frame.render_pass_list[1]->copy_requests.push_back(
std::move(copy_request2));
support_->SubmitCompositorFrame(root_local_surface_id_, std::move(frame));
}
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize()),
Pass(root_quads2, SurfaceSize())};
TestPassesMatchExpectations(expected_passes,
&aggregated_frame.render_pass_list);
ASSERT_EQ(2u, aggregated_frame.render_pass_list.size());
ASSERT_EQ(1u, aggregated_frame.render_pass_list[0]->copy_requests.size());
DCHECK_EQ(copy_request_ptr,
aggregated_frame.render_pass_list[0]->copy_requests[0].get());
ASSERT_EQ(1u, aggregated_frame.render_pass_list[1]->copy_requests.size());
DCHECK_EQ(copy_request2_ptr,
aggregated_frame.render_pass_list[1]->copy_requests[0].get());
SurfaceId surface_ids[] = {root_surface_id, embedded_surface_id};
EXPECT_EQ(base::size(surface_ids),
aggregator_.previous_contained_surfaces().size());
for (size_t i = 0; i < base::size(surface_ids); i++) {
EXPECT_TRUE(
aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
aggregator_.previous_contained_surfaces().end());
}
// Ensure copy requests have been removed from root surface.
const CompositorFrame& original_frame = manager_.surface_manager()
->GetSurfaceForId(root_surface_id)
->GetActiveFrame();
const auto& original_pass_list = original_frame.render_pass_list;
ASSERT_EQ(2u, original_pass_list.size());
DCHECK(original_pass_list[0]->copy_requests.empty());
DCHECK(original_pass_list[1]->copy_requests.empty());
}
TEST_F(SurfaceAggregatorValidSurfaceTest, UnreferencedSurface) {
auto embedded_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
auto parent_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId2, kRootIsRoot,
kNeedsSyncPoints);
allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(embedded_support->frame_sink_id(),
embedded_local_surface_id);
allocator_.GenerateId();
SurfaceId nonexistent_surface_id(
support_->frame_sink_id(),
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id());
std::vector<Quad> embedded_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))};
std::vector<Pass> embedded_passes = {Pass(embedded_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(embedded_support.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
auto copy_request(CopyOutputRequest::CreateStubForTesting());
auto* copy_request_ptr = copy_request.get();
embedded_support->RequestCopyOfOutput(embedded_local_surface_id,
std::move(copy_request));
allocator_.GenerateId();
LocalSurfaceId parent_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId parent_surface_id(parent_support->frame_sink_id(),
parent_local_surface_id);
std::vector<Quad> parent_quads = {
Quad::SolidColorQuad(SK_ColorGRAY, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorLTGRAY, gfx::Rect(5, 5))};
std::vector<Pass> parent_passes = {Pass(parent_quads, SurfaceSize())};
{
CompositorFrame frame = MakeEmptyCompositorFrame();
AddPasses(&frame.render_pass_list, parent_passes,
&frame.metadata.referenced_surfaces);
frame.metadata.referenced_surfaces.emplace_back(embedded_surface_id);
parent_support->SubmitCompositorFrame(parent_local_surface_id,
std::move(frame));
}
std::vector<Quad> root_quads = {
Quad::SolidColorQuad(SK_ColorWHITE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLACK, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(root_quads, SurfaceSize())};
{
CompositorFrame frame = MakeEmptyCompositorFrame();
AddPasses(&frame.render_pass_list, root_passes,
&frame.metadata.referenced_surfaces);
frame.metadata.referenced_surfaces.emplace_back(parent_surface_id);
// Reference to Surface ID of a Surface that doesn't exist should be
// included in previous_contained_surfaces, but otherwise ignored.
frame.metadata.referenced_surfaces.emplace_back(nonexistent_surface_id);
support_->SubmitCompositorFrame(root_local_surface_id_, std::move(frame));
}
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
// First pass should come from surface that had a copy request but was not
// referenced directly. The second pass comes from the root surface.
// parent_quad should be ignored because it is neither referenced through a
// SurfaceDrawQuad nor has a copy request on it.
std::vector<Pass> expected_passes = {Pass(embedded_quads, SurfaceSize()),
Pass(root_quads, SurfaceSize())};
TestPassesMatchExpectations(expected_passes,
&aggregated_frame.render_pass_list);
ASSERT_EQ(2u, aggregated_frame.render_pass_list.size());
ASSERT_EQ(1u, aggregated_frame.render_pass_list[0]->copy_requests.size());
DCHECK_EQ(copy_request_ptr,
aggregated_frame.render_pass_list[0]->copy_requests[0].get());
SurfaceId surface_ids[] = {
SurfaceId(support_->frame_sink_id(), root_local_surface_id_),
parent_surface_id, embedded_surface_id};
EXPECT_EQ(base::size(surface_ids),
aggregator_.previous_contained_surfaces().size());
for (size_t i = 0; i < base::size(surface_ids); i++) {
EXPECT_TRUE(
aggregator_.previous_contained_surfaces().find(surface_ids[i]) !=
aggregator_.previous_contained_surfaces().end());
}
}
// This tests referencing a surface that has multiple render passes.
TEST_F(SurfaceAggregatorValidSurfaceTest, MultiPassSurfaceReference) {
child_allocator_.GenerateId();
LocalSurfaceId embedded_local_surface_id =
child_allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId embedded_surface_id(child_support_->frame_sink_id(),
embedded_local_surface_id);
int pass_ids[] = {1, 2, 3};
std::vector<Quad> embedded_quads[3] = {
{Quad::SolidColorQuad(1, gfx::Rect(5, 5)),
Quad::SolidColorQuad(2, gfx::Rect(5, 5))},
{Quad::SolidColorQuad(3, gfx::Rect(5, 5)),
Quad::RenderPassQuad(pass_ids[0])},
{Quad::SolidColorQuad(4, gfx::Rect(5, 5)),
Quad::RenderPassQuad(pass_ids[1])}};
std::vector<Pass> embedded_passes = {
Pass(embedded_quads[0], pass_ids[0], SurfaceSize()),
Pass(embedded_quads[1], pass_ids[1], SurfaceSize()),
Pass(embedded_quads[2], pass_ids[2], SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(child_support_.get(), embedded_passes,
embedded_local_surface_id, device_scale_factor);
std::vector<Quad> root_quads[3] = {
{Quad::SolidColorQuad(5, gfx::Rect(5, 5)),
Quad::SolidColorQuad(6, gfx::Rect(5, 5))},
{Quad::SurfaceQuad(SurfaceRange(base::nullopt, embedded_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::RenderPassQuad(pass_ids[0])},
{Quad::SolidColorQuad(7, gfx::Rect(5, 5)),
Quad::RenderPassQuad(pass_ids[1])}};
std::vector<Pass> root_passes = {
Pass(root_quads[0], pass_ids[0], SurfaceSize()),
Pass(root_quads[1], pass_ids[1], SurfaceSize()),
Pass(root_quads[2], pass_ids[2], SurfaceSize())};
SubmitCompositorFrame(support_.get(), root_passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
const auto& aggregated_pass_list = aggregated_frame.render_pass_list;
ASSERT_EQ(5u, aggregated_pass_list.size());
RenderPassId actual_pass_ids[] = {
aggregated_pass_list[0]->id, aggregated_pass_list[1]->id,
aggregated_pass_list[2]->id, aggregated_pass_list[3]->id,
aggregated_pass_list[4]->id};
for (size_t i = 0; i < 5; ++i) {
for (size_t j = 0; j < i; ++j) {
EXPECT_NE(actual_pass_ids[i], actual_pass_ids[j]);
}
}
{
SCOPED_TRACE("First pass");
// The first pass will just be the first pass from the root surfaces quad
// with no render pass quads to remap.
TestPassMatchesExpectations(root_passes[0], aggregated_pass_list[0].get());
}
{
SCOPED_TRACE("Second pass");
// The next two passes will be from the embedded surface since we have to
// draw those passes before they are referenced from the render pass draw
// quad embedded into the root surface's second pass.
// First, there's the first embedded pass which doesn't reference anything
// else.
TestPassMatchesExpectations(embedded_passes[0],
aggregated_pass_list[1].get());
}
{
SCOPED_TRACE("Third pass");
const auto& third_pass_quad_list = aggregated_pass_list[2]->quad_list;
ASSERT_EQ(2u, third_pass_quad_list.size());
TestQuadMatchesExpectations(embedded_quads[1][0],
third_pass_quad_list.ElementAt(0));
// This render pass pass quad will reference the first pass from the
// embedded surface, which is the second pass in the aggregated frame.
ASSERT_EQ(DrawQuad::RENDER_PASS,
third_pass_quad_list.ElementAt(1)->material);
const auto* third_pass_render_pass_draw_quad =
RenderPassDrawQuad::MaterialCast(third_pass_quad_list.ElementAt(1));
EXPECT_EQ(actual_pass_ids[1],
third_pass_render_pass_draw_quad->render_pass_id);
}
{
SCOPED_TRACE("Fourth pass");
// The fourth pass will have aggregated quads from the root surface's second
// pass and the embedded surface's first pass.
const auto& fourth_pass_quad_list = aggregated_pass_list[3]->quad_list;
ASSERT_EQ(3u, fourth_pass_quad_list.size());
// The first quad will be the yellow quad from the embedded surface's last
// pass.
TestQuadMatchesExpectations(embedded_quads[2][0],
fourth_pass_quad_list.ElementAt(0));
// The next quad will be a render pass quad referencing the second pass from
// the embedded surface, which is the third pass in the aggregated frame.
ASSERT_EQ(DrawQuad::RENDER_PASS,
fourth_pass_quad_list.ElementAt(1)->material);
const auto* fourth_pass_first_render_pass_draw_quad =
RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(1));
EXPECT_EQ(actual_pass_ids[2],
fourth_pass_first_render_pass_draw_quad->render_pass_id);
// The last quad will be a render pass quad referencing the first pass from
// the root surface, which is the first pass overall.
ASSERT_EQ(DrawQuad::RENDER_PASS,
fourth_pass_quad_list.ElementAt(2)->material);
const auto* fourth_pass_second_render_pass_draw_quad =
RenderPassDrawQuad::MaterialCast(fourth_pass_quad_list.ElementAt(2));
EXPECT_EQ(actual_pass_ids[0],
fourth_pass_second_render_pass_draw_quad->render_pass_id);
}
{
SCOPED_TRACE("Fifth pass");
const auto& fifth_pass_quad_list = aggregated_pass_list[4]->quad_list;
ASSERT_EQ(2u, fifth_pass_quad_list.size());
TestQuadMatchesExpectations(root_quads[2][0],
fifth_pass_quad_list.ElementAt(0));
// The last quad in the last pass will reference the second pass from the
// root surface, which after aggregating is the fourth pass in the overall
// list.
ASSERT_EQ(DrawQuad::RENDER_PASS,
fifth_pass_quad_list.ElementAt(1)->material);
const auto* fifth_pass_render_pass_draw_quad =
RenderPassDrawQuad::MaterialCast(fifth_pass_quad_list.ElementAt(1));
EXPECT_EQ(actual_pass_ids[3],
fifth_pass_render_pass_draw_quad->render_pass_id);
}
}
// Tests an invalid surface reference in a frame. The surface quad should just
// be dropped.
TEST_F(SurfaceAggregatorValidSurfaceTest, InvalidSurfaceReference) {
std::vector<Quad> quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SurfaceQuad(
SurfaceRange(SurfaceId(
FrameSinkId(),
LocalSurfaceId(0xdeadbeef, 0xdeadbeef, kArbitraryToken))),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id});
}
// Tests a reference to a valid surface with no submitted frame. A
// SolidColorDrawQuad should be placed in lieu of a frame.
TEST_F(SurfaceAggregatorValidSurfaceTest, ValidSurfaceReferenceWithNoFrame) {
allocator_.GenerateId();
LocalSurfaceId empty_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId surface_with_no_frame_id(kArbitraryFrameSinkId1,
empty_local_surface_id);
std::vector<Quad> quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, surface_with_no_frame_id),
SK_ColorYELLOW, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorYELLOW, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id});
}
// Tests a reference to a valid primary surface and a fallback surface
// with no submitted frame. A SolidColorDrawQuad should be placed in lieu of a
// frame.
TEST_F(SurfaceAggregatorValidSurfaceTest, ValidFallbackWithNoFrame) {
allocator_.GenerateId();
const LocalSurfaceId empty_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
const SurfaceId surface_with_no_frame_id(support_->frame_sink_id(),
empty_local_surface_id);
std::vector<Quad> quads = {Quad::SurfaceQuad(
SurfaceRange(surface_with_no_frame_id), SK_ColorYELLOW, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false, /*ignores_input_event=*/false)};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorYELLOW, gfx::Rect(5, 5)),
};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
AggregateAndVerify(expected_passes, {root_surface_id});
}
// Tests a surface quad referencing itself, generating a trivial cycle.
// The quad creating the cycle should be dropped from the final frame.
TEST_F(SurfaceAggregatorValidSurfaceTest, SimpleCyclicalReference) {
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
std::vector<Quad> quads = {
Quad::SurfaceQuad(SurfaceRange(base::nullopt, root_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorYELLOW, gfx::Rect(5, 5))};
std::vector<Pass> passes = {Pass(quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), passes, root_local_surface_id_,
device_scale_factor);
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorYELLOW, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
AggregateAndVerify(expected_passes, {root_surface_id});
}
// Tests a more complex cycle with one intermediate surface.
TEST_F(SurfaceAggregatorValidSurfaceTest, TwoSurfaceCyclicalReference) {
allocator_.GenerateId();
LocalSurfaceId child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId child_surface_id(child_support_->frame_sink_id(),
child_local_surface_id);
std::vector<Quad> parent_quads = {
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, child_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorCYAN, gfx::Rect(5, 5))};
std::vector<Pass> parent_passes = {Pass(parent_quads, SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(support_.get(), parent_passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
std::vector<Quad> child_quads = {
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, root_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false),
Quad::SolidColorQuad(SK_ColorMAGENTA, gfx::Rect(5, 5))};
std::vector<Pass> child_passes = {Pass(child_quads, SurfaceSize())};
SubmitCompositorFrame(child_support_.get(), child_passes,
child_local_surface_id, device_scale_factor);
// The child surface's reference to the root_surface_ will be dropped, so
// we'll end up with:
// SK_ColorBLUE from the parent
// SK_ColorGREEN from the child
// SK_ColorMAGENTA from the child
// SK_ColorCYAN from the parent
std::vector<Quad> expected_quads = {
Quad::SolidColorQuad(SK_ColorBLUE, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorMAGENTA, gfx::Rect(5, 5)),
Quad::SolidColorQuad(SK_ColorCYAN, gfx::Rect(5, 5))};
std::vector<Pass> expected_passes = {Pass(expected_quads, SurfaceSize())};
AggregateAndVerify(expected_passes, {root_surface_id, child_surface_id});
}
// Tests that we map render pass IDs from different surfaces into a unified
// namespace and update RenderPassDrawQuad's id references to match.
TEST_F(SurfaceAggregatorValidSurfaceTest, RenderPassIdMapping) {
allocator_.GenerateId();
LocalSurfaceId child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId child_surface_id(child_support_->frame_sink_id(),
child_local_surface_id);
RenderPassId child_pass_id[] = {1u, 2u};
std::vector<Quad> child_quad[2] = {
{Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))},
{Quad::RenderPassQuad(child_pass_id[0])}};
std::vector<Pass> surface_passes = {
Pass(child_quad[0], child_pass_id[0], SurfaceSize()),
Pass(child_quad[1], child_pass_id[1], SurfaceSize())};
constexpr float device_scale_factor = 1.0f;
SubmitCompositorFrame(child_support_.get(), surface_passes,
child_local_surface_id, device_scale_factor);
// Pass IDs from the parent surface may collide with ones from the child.
RenderPassId parent_pass_id[] = {3u, 2u};
std::vector<Quad> parent_quad[2] = {
{Quad::SurfaceQuad(SurfaceRange(base::nullopt, child_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)},
{Quad::RenderPassQuad(parent_pass_id[0])}};
std::vector<Pass> parent_passes = {
Pass(parent_quad[0], parent_pass_id[0], SurfaceSize()),
Pass(parent_quad[1], parent_pass_id[1], SurfaceSize())};
SubmitCompositorFrame(support_.get(), parent_passes, root_local_surface_id_,
device_scale_factor);
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
const auto& aggregated_pass_list = aggregated_frame.render_pass_list;
ASSERT_EQ(3u, aggregated_pass_list.size());
RenderPassId actual_pass_ids[] = {aggregated_pass_list[0]->id,
aggregated_pass_list[1]->id,
aggregated_pass_list[2]->id};
// Make sure the aggregated frame's pass IDs are all unique.
for (size_t i = 0; i < 3; ++i) {
for (size_t j = 0; j < i; ++j) {
EXPECT_NE(actual_pass_ids[j], actual_pass_ids[i])
<< "pass ids " << i << " and " << j;
}
}
// Make sure the render pass quads reference the remapped pass IDs.
DrawQuad* render_pass_quads[] = {aggregated_pass_list[1]->quad_list.front(),
aggregated_pass_list[2]->quad_list.front()};
ASSERT_EQ(render_pass_quads[0]->material, DrawQuad::RENDER_PASS);
EXPECT_EQ(
actual_pass_ids[0],
RenderPassDrawQuad::MaterialCast(render_pass_quads[0])->render_pass_id);
ASSERT_EQ(render_pass_quads[1]->material, DrawQuad::RENDER_PASS);
EXPECT_EQ(
actual_pass_ids[1],
RenderPassDrawQuad::MaterialCast(render_pass_quads[1])->render_pass_id);
}
void AddSolidColorQuadWithBlendMode(const gfx::Size& size,
RenderPass* pass,
const SkBlendMode blend_mode) {
const gfx::Transform layer_to_target_transform;
const gfx::Rect layer_rect(size);
const gfx::Rect visible_layer_rect(size);
const gfx::Rect clip_rect(size);
bool is_clipped = false;
SkColor color = SK_ColorGREEN;
bool are_contents_opaque = SkColorGetA(color) == 0xFF;
float opacity = 1.f;
bool force_anti_aliasing_off = false;
auto* sqs = pass->CreateAndAppendSharedQuadState();
sqs->SetAll(layer_to_target_transform, layer_rect, visible_layer_rect,
clip_rect, is_clipped, are_contents_opaque, opacity, blend_mode,
0);
auto* color_quad = pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
color_quad->SetNew(pass->shared_quad_state_list.back(), visible_layer_rect,
visible_layer_rect, color, force_anti_aliasing_off);
}
// This tests that we update shared quad state pointers correctly within
// aggregated passes. The shared quad state list on the aggregated pass will
// include the shared quad states from each pass in one list so the quads will
// end up pointed to shared quad state objects at different offsets. This test
// uses the blend_mode value stored on the shared quad state to track the shared
// quad state, but anything saved on the shared quad state would work.
//
// This test has 4 surfaces in the following structure:
// root_surface -> quad with kClear_Mode,
// [child_one_surface],
// quad with kDstOver_Mode,
// [child_two_surface],
// quad with kDstIn_Mode
// child_one_surface -> quad with kSrc_Mode,
// [grandchild_surface],
// quad with kSrcOver_Mode
// child_two_surface -> quad with kSrcIn_Mode
// grandchild_surface -> quad with kDst_Mode
//
// Resulting in the following aggregated pass:
// quad_root_0 - blend_mode kClear_Mode
// quad_child_one_0 - blend_mode kSrc_Mode
// quad_grandchild_0 - blend_mode kDst_Mode
// quad_child_one_1 - blend_mode kSrcOver_Mode
// quad_root_1 - blend_mode kDstOver_Mode
// quad_child_two_0 - blend_mode kSrcIn_Mode
// quad_root_2 - blend_mode kDstIn_Mode
TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateSharedQuadStateProperties) {
const SkBlendMode blend_modes[] = {
SkBlendMode::kClear, // 0
SkBlendMode::kSrc, // 1
SkBlendMode::kDst, // 2
SkBlendMode::kSrcOver, // 3
SkBlendMode::kDstOver, // 4
SkBlendMode::kSrcIn, // 5
SkBlendMode::kDstIn, // 6
};
auto grandchild_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId1, kChildIsRoot,
kNeedsSyncPoints);
auto child_one_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId2, kChildIsRoot,
kNeedsSyncPoints);
auto child_two_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryFrameSinkId3, kChildIsRoot,
kNeedsSyncPoints);
int pass_id = 1;
allocator_.GenerateId();
LocalSurfaceId grandchild_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId grandchild_surface_id(grandchild_support->frame_sink_id(),
grandchild_local_surface_id);
auto grandchild_pass = RenderPass::Create();
constexpr float device_scale_factor = 1.0f;
gfx::Rect output_rect(SurfaceSize());
gfx::Rect damage_rect(SurfaceSize());
gfx::Transform transform_to_root_target;
grandchild_pass->SetNew(pass_id, output_rect, damage_rect,
transform_to_root_target);
AddSolidColorQuadWithBlendMode(SurfaceSize(), grandchild_pass.get(),
blend_modes[2]);
QueuePassAsFrame(std::move(grandchild_pass), grandchild_local_surface_id,
device_scale_factor, grandchild_support.get());
allocator_.GenerateId();
LocalSurfaceId child_one_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId child_one_surface_id(child_one_support->frame_sink_id(),
child_one_local_surface_id);
auto child_one_pass = RenderPass::Create();
child_one_pass->SetNew(pass_id, output_rect, damage_rect,
transform_to_root_target);
AddSolidColorQuadWithBlendMode(SurfaceSize(), child_one_pass.get(),
blend_modes[1]);
auto* grandchild_surface_quad =
child_one_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
grandchild_surface_quad->SetNew(
child_one_pass->shared_quad_state_list.back(), gfx::Rect(SurfaceSize()),
gfx::Rect(SurfaceSize()),
SurfaceRange(base::nullopt, grandchild_surface_id), SK_ColorWHITE,
/*stretch_content_to_fill_bounds=*/false, /*ignores_input_event=*/false);
AddSolidColorQuadWithBlendMode(SurfaceSize(), child_one_pass.get(),
blend_modes[3]);
QueuePassAsFrame(std::move(child_one_pass), child_one_local_surface_id,
device_scale_factor, child_one_support.get());
allocator_.GenerateId();
LocalSurfaceId child_two_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId child_two_surface_id(child_two_support->frame_sink_id(),
child_two_local_surface_id);
auto child_two_pass = RenderPass::Create();
child_two_pass->SetNew(pass_id, output_rect, damage_rect,
transform_to_root_target);
AddSolidColorQuadWithBlendMode(SurfaceSize(), child_two_pass.get(),
blend_modes[5]);
QueuePassAsFrame(std::move(child_two_pass), child_two_local_surface_id,
device_scale_factor, child_two_support.get());
auto root_pass = RenderPass::Create();
root_pass->SetNew(pass_id, output_rect, damage_rect,
transform_to_root_target);
AddSolidColorQuadWithBlendMode(SurfaceSize(), root_pass.get(),
blend_modes[0]);
auto* child_one_surface_quad =
root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
child_one_surface_quad->SetNew(
root_pass->shared_quad_state_list.back(), gfx::Rect(SurfaceSize()),
gfx::Rect(SurfaceSize()),
SurfaceRange(base::nullopt, child_one_surface_id), SK_ColorWHITE,
/*stretch_content_to_fill_bounds=*/false, /*ignores_input_event=*/false);
AddSolidColorQuadWithBlendMode(SurfaceSize(), root_pass.get(),
blend_modes[4]);
auto* child_two_surface_quad =
root_pass->CreateAndAppendDrawQuad<SurfaceDrawQuad>();
child_two_surface_quad->SetNew(
root_pass->shared_quad_state_list.back(), gfx::Rect(SurfaceSize()),
gfx::Rect(SurfaceSize()),
SurfaceRange(base::nullopt, child_two_surface_id), SK_ColorWHITE,
/*stretch_content_to_fill_bounds=*/false, /*ignores_input_event=*/false);
AddSolidColorQuadWithBlendMode(SurfaceSize(), root_pass.get(),
blend_modes[6]);
QueuePassAsFrame(std::move(root_pass), root_local_surface_id_,
device_scale_factor, support_.get());
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
const auto& aggregated_pass_list = aggregated_frame.render_pass_list;
ASSERT_EQ(1u, aggregated_pass_list.size());
const auto& aggregated_quad_list = aggregated_pass_list[0]->quad_list;
ASSERT_EQ(7u, aggregated_quad_list.size());
for (auto iter = aggregated_quad_list.cbegin();
iter != aggregated_quad_list.cend(); ++iter) {
EXPECT_EQ(blend_modes[iter.index()], iter->shared_quad_state->blend_mode)
<< iter.index();
}
}
// This tests that when aggregating a frame with multiple render passes that we
// map the transforms for the root pass but do not modify the transform on child
// passes.
//
// The root surface has one pass with a surface quad transformed by +10 in the y
// direction.
//
// The middle surface has one pass with a surface quad scaled by 2 in the x
// and 3 in the y directions.
//
// The child surface has two passes. The first pass has a quad with a transform
// of +5 in the x direction. The second pass has a reference to the first pass'
// pass id and a transform of +8 in the x direction.
//
// After aggregation, the child surface's root pass quad should have all
// transforms concatenated for a total transform of +23 x, +10 y. The
// contributing render pass' transform in the aggregate frame should not be
// affected.
TEST_F(SurfaceAggregatorValidSurfaceTest, AggregateMultiplePassWithTransform) {
auto middle_support = std::make_unique<CompositorFrameSinkSupport>(
nullptr, &manager_, kArbitraryMiddleFrameSinkId, kChildIsRoot,
kNeedsSyncPoints);
// Innermost child surface.
allocator_.GenerateId();
LocalSurfaceId child_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId child_surface_id(child_support_->frame_sink_id(),
child_local_surface_id);
{
int child_pass_id[] = {1, 2};
std::vector<Quad> child_quads[2] = {
{Quad::SolidColorQuad(SK_ColorGREEN, gfx::Rect(5, 5))},
{Quad::RenderPassQuad(child_pass_id[0])},
};
std::vector<Pass> child_passes = {
Pass(child_quads[0], child_pass_id[0], SurfaceSize()),
Pass(child_quads[1], child_pass_id[1], SurfaceSize())};
CompositorFrame child_frame = MakeEmptyCompositorFrame();
AddPasses(&child_frame.render_pass_list, child_passes,
&child_frame.metadata.referenced_surfaces);
auto* child_nonroot_pass = child_frame.render_pass_list[0].get();
child_nonroot_pass->transform_to_root_target.Translate(8, 0);
auto* child_nonroot_pass_sqs =
child_nonroot_pass->shared_quad_state_list.front();
child_nonroot_pass_sqs->quad_to_target_transform.Translate(5, 0);
auto* child_root_pass = child_frame.render_pass_list[1].get();
auto* child_root_pass_sqs = child_root_pass->shared_quad_state_list.front();
child_root_pass_sqs->quad_to_target_transform.Translate(8, 0);
child_root_pass_sqs->is_clipped = true;
child_root_pass_sqs->clip_rect = gfx::Rect(0, 0, 5, 5);
child_support_->SubmitCompositorFrame(child_local_surface_id,
std::move(child_frame));
}
// Middle child surface.
allocator_.GenerateId();
LocalSurfaceId middle_local_surface_id =
allocator_.GetCurrentLocalSurfaceIdAllocation().local_surface_id();
SurfaceId middle_surface_id(middle_support->frame_sink_id(),
middle_local_surface_id);
{
std::vector<Quad> middle_quads = {Quad::SurfaceQuad(
SurfaceRange(base::nullopt, child_surface_id), SK_ColorWHITE,
gfx::Rect(5, 5), /*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
std::vector<Pass> middle_passes = {
Pass(middle_quads, SurfaceSize()),
};
CompositorFrame middle_frame = MakeEmptyCompositorFrame();
AddPasses(&middle_frame.render_pass_list, middle_passes,
&middle_frame.metadata.referenced_surfaces);
auto* middle_root_pass = middle_frame.render_pass_list[0].get();
DrawQuad* middle_frame_quad = middle_root_pass->quad_list.ElementAt(0);
middle_frame_quad->rect = gfx::Rect(0, 1, 100, 7);
middle_frame_quad->visible_rect = gfx::Rect(0, 1, 100, 7);
auto* middle_root_pass_sqs =
middle_root_pass->shared_quad_state_list.front();
middle_root_pass_sqs->quad_to_target_transform.Scale(2, 3);
middle_support->SubmitCompositorFrame(middle_local_surface_id,
std::move(middle_frame));
}
// Root surface.
std::vector<Quad> secondary_quads = {
Quad::SolidColorQuad(1, gfx::Rect(5, 5)),
Quad::SurfaceQuad(SurfaceRange(base::nullopt, middle_surface_id),
SK_ColorWHITE, gfx::Rect(5, 5),
/*stretch_content_to_fill_bounds=*/false,
/*ignores_input_event=*/false)};
std::vector<Quad> root_quads = {Quad::SolidColorQuad(1, gfx::Rect(5, 5))};
std::vector<Pass> root_passes = {Pass(secondary_quads, SurfaceSize()),
Pass(root_quads, SurfaceSize())};
CompositorFrame root_frame = MakeEmptyCompositorFrame();
AddPasses(&root_frame.render_pass_list, root_passes,
&root_frame.metadata.referenced_surfaces);
root_frame.render_pass_list[0]
->shared_quad_state_list.front()
->quad_to_target_transform.Translate(0, 7);
root_frame.render_pass_list[0]
->shared_quad_state_list.ElementAt(1)
->quad_to_target_transform.Translate(0, 10);
DrawQuad* root_frame_quad =
root_frame.render_pass_list[0]->quad_list.ElementAt(1);
root_frame_quad->rect = gfx::Rect(8, 100);
root_frame_quad->visible_rect = gfx::Rect(8, 100);
root_frame.render_pass_list[0]->transform_to_root_target.Translate(10, 5);
support_->SubmitCompositorFrame(root_local_surface_id_,
std::move(root_frame));
SurfaceId root_surface_id(support_->frame_sink_id(), root_local_surface_id_);
CompositorFrame aggregated_frame =
aggregator_.Aggregate(root_surface_id, GetNextDisplayTimeAndIncrement());
const auto& aggregated_pass_list = aggregated_frame.render_pass_list;
ASSERT_EQ(3u, aggregated_pass_list.size());
ASSERT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
// The first pass should have one shared quad state for the one solid color
// quad.
EXPECT_EQ(1u, aggregated_pass_list[0]->shared_quad_state_list.size());
// The second pass should have just two shared quad states. We'll
// verify the properties through the quads.
EXPECT_EQ(2u, aggregated_pass_list[1]->shared_quad_state_list.size());
EXPECT_EQ(1u, aggregated_pass_list[2]->shared_quad_state_list.size());
auto* aggregated_first_pass_sqs =
aggregated_pass_list[0]->shared_quad_state_list.front();
// The first pass's transform should be unaffected by the embedding and still
// be a translation by +5 in the x direction.
gfx::Transform expected_aggregated_first_pass_sqs_transform;
expected_aggregated_first_pass_sqs_transform.Translate(5, 0);
EXPECT_EQ(expected_aggregated_first_pass_sqs_transform.ToString(),
aggregated_first_pass_sqs->quad_to_target_transform.ToString());
// The first pass's transform to the root target should include the aggregated
// transform, including the transform from the child pass to the root.
gfx::Transform expected_first_pass_transform_to_root_target;
expected_first_pass_transform_to_root_target.Translate(10, 5);
expected_first_pass_transform_to_root_target.Translate(0, 10);
expected_first_pass_transform_to_root_target.Scale(2, 3);
expected_first_pass_transform_to_root_target.Translate(8, 0);
EXPECT_EQ(expected_first_pass_transform_to_root_target.ToString(),
aggregated_pass_list[0]->transform_to_root_target.ToString());
ASSERT_EQ(2u, aggregated_pass_list[1]->quad_list.size());
gfx::Transform expected_root_pass_quad_transforms[2];
// The first quad in the root pass is the solid color quad from the original
// root surface. Its transform should be unaffected by the aggregation and
// still be +7 in the y direction.
expected_root_pass_quad_transforms[0].Translate(0, 7);