From 3228fd5a28eb5190f7302f5fb89075479d20009f Mon Sep 17 00:00:00 2001 From: nyquist Date: Fri, 20 Nov 2015 11:38:41 -0800 Subject: [PATCH] Add support for converting more types to and from protobuf. For the (de)serialization of property trees and layers, we need to be able to serialize more data types and this CL adds conversions and unit tests for the types Point3F, Vector2dF, ScrollOffset and LayerPositionConstraint. This is done in a separate CL to keep the logic-changing CLs clean and focused. This CL also changes the behavior of the conversions of gfx::Transform to ensure that it supports sending empty protobufs across the channel. BUG=538710 CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel Review URL: https://codereview.chromium.org/1452323004 Cr-Commit-Position: refs/heads/master@{#360881} --- cc/cc.gyp | 4 ++ cc/layers/layer_position_constraint.cc | 15 ++++++ cc/layers/layer_position_constraint.h | 7 +++ .../layer_position_constraint_unittest.cc | 27 +++++++++++ cc/proto/BUILD.gn | 4 ++ cc/proto/gfx_conversions.cc | 38 ++++++++++++++- cc/proto/gfx_conversions.h | 22 ++++++++- cc/proto/gfx_conversions_unittest.cc | 46 +++++++++++++++++++ cc/proto/layer_position_constraint.proto | 15 ++++++ cc/proto/point3f.proto | 15 ++++++ cc/proto/scroll_offset.proto | 14 ++++++ cc/proto/vector2df.proto | 14 ++++++ 12 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 cc/proto/layer_position_constraint.proto create mode 100644 cc/proto/point3f.proto create mode 100644 cc/proto/scroll_offset.proto create mode 100644 cc/proto/vector2df.proto diff --git a/cc/cc.gyp b/cc/cc.gyp index 709bfc1eda326d..cf128889c5a7ac 100644 --- a/cc/cc.gyp +++ b/cc/cc.gyp @@ -588,16 +588,20 @@ 'proto/compositor_message.proto', 'proto/display_item.proto', 'proto/layer.proto', + 'proto/layer_position_constraint.proto', 'proto/point.proto', + 'proto/point3f.proto', 'proto/pointf.proto', 'proto/rect.proto', 'proto/rectf.proto', + 'proto/scroll_offset.proto', 'proto/size.proto', 'proto/sizef.proto', 'proto/skregion.proto', 'proto/skrrect.proto', 'proto/skxfermode.proto', 'proto/transform.proto', + 'proto/vector2df.proto', ], 'defines': [ 'CC_PROTO_IMPLEMENTATION=1', diff --git a/cc/layers/layer_position_constraint.cc b/cc/layers/layer_position_constraint.cc index 183b6b5b53e90a..5d8824e24213b6 100644 --- a/cc/layers/layer_position_constraint.cc +++ b/cc/layers/layer_position_constraint.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "cc/layers/layer_position_constraint.h" +#include "cc/proto/layer_position_constraint.pb.h" namespace cc { @@ -12,6 +13,20 @@ LayerPositionConstraint::LayerPositionConstraint() is_fixed_to_bottom_edge_(false) { } +void LayerPositionConstraint::ToProtobuf( + proto::LayerPositionConstraint* proto) const { + proto->set_is_fixed_position(is_fixed_position_); + proto->set_is_fixed_to_right_edge(is_fixed_to_right_edge_); + proto->set_is_fixed_to_bottom_edge(is_fixed_to_bottom_edge_); +} + +void LayerPositionConstraint::FromProtobuf( + const proto::LayerPositionConstraint& proto) { + is_fixed_position_ = proto.is_fixed_position(); + is_fixed_to_right_edge_ = proto.is_fixed_to_right_edge(); + is_fixed_to_bottom_edge_ = proto.is_fixed_to_bottom_edge(); +} + bool LayerPositionConstraint::operator==( const LayerPositionConstraint& other) const { if (!is_fixed_position_ && !other.is_fixed_position_) diff --git a/cc/layers/layer_position_constraint.h b/cc/layers/layer_position_constraint.h index 9a5751377369e6..1ee4bc72775b40 100644 --- a/cc/layers/layer_position_constraint.h +++ b/cc/layers/layer_position_constraint.h @@ -9,6 +9,10 @@ namespace cc { +namespace proto { +class LayerPositionConstraint; +} + class CC_EXPORT LayerPositionConstraint { public: LayerPositionConstraint(); @@ -24,6 +28,9 @@ class CC_EXPORT LayerPositionConstraint { } bool is_fixed_to_bottom_edge() const { return is_fixed_to_bottom_edge_; } + void ToProtobuf(proto::LayerPositionConstraint* proto) const; + void FromProtobuf(const proto::LayerPositionConstraint& proto); + bool operator==(const LayerPositionConstraint&) const; bool operator!=(const LayerPositionConstraint&) const; diff --git a/cc/layers/layer_position_constraint_unittest.cc b/cc/layers/layer_position_constraint_unittest.cc index 5d61c01ada4d15..c137203249b003 100644 --- a/cc/layers/layer_position_constraint_unittest.cc +++ b/cc/layers/layer_position_constraint_unittest.cc @@ -9,6 +9,7 @@ #include "cc/layers/layer.h" #include "cc/layers/layer_impl.h" #include "cc/layers/layer_settings.h" +#include "cc/proto/layer_position_constraint.pb.h" #include "cc/test/fake_layer_tree_host.h" #include "cc/test/fake_proxy.h" #include "cc/test/geometry_test_utils.h" @@ -1131,5 +1132,31 @@ TEST_F(LayerPositionConstraintTest, fixed_child_impl->draw_transform()); } +void VerifySerializeAndDeserializeProto(bool is_fixed_position, + bool is_fixed_to_right_edge, + bool is_fixed_to_bottom_edge) { + LayerPositionConstraint constraint; + constraint.set_is_fixed_position(is_fixed_position); + constraint.set_is_fixed_to_right_edge(is_fixed_to_right_edge); + constraint.set_is_fixed_to_bottom_edge(is_fixed_to_bottom_edge); + proto::LayerPositionConstraint proto; + constraint.ToProtobuf(&proto); + + LayerPositionConstraint constraint2; + constraint2.FromProtobuf(proto); + EXPECT_EQ(constraint, constraint2); +} + +TEST(LayerPositionConstraintSerializationTest, SerializeAndDeserializeProto) { + VerifySerializeAndDeserializeProto(true, true, true); + VerifySerializeAndDeserializeProto(true, true, false); + VerifySerializeAndDeserializeProto(true, false, true); + VerifySerializeAndDeserializeProto(true, false, false); + VerifySerializeAndDeserializeProto(false, true, true); + VerifySerializeAndDeserializeProto(false, true, false); + VerifySerializeAndDeserializeProto(false, false, true); + VerifySerializeAndDeserializeProto(false, false, false); +} + } // namespace } // namespace cc diff --git a/cc/proto/BUILD.gn b/cc/proto/BUILD.gn index 2465cfa592b3df..33e3e58a22492b 100644 --- a/cc/proto/BUILD.gn +++ b/cc/proto/BUILD.gn @@ -32,16 +32,20 @@ proto_library("proto_internal") { "compositor_message.proto", "display_item.proto", "layer.proto", + "layer_position_constraint.proto", "point.proto", + "point3f.proto", "pointf.proto", "rect.proto", "rectf.proto", + "scroll_offset.proto", "size.proto", "sizef.proto", "skregion.proto", "skrrect.proto", "skxfermode.proto", "transform.proto", + "vector2df.proto", ] deps = [ diff --git a/cc/proto/gfx_conversions.cc b/cc/proto/gfx_conversions.cc index cf85fbf249b822..8dac00cd7e08ea 100644 --- a/cc/proto/gfx_conversions.cc +++ b/cc/proto/gfx_conversions.cc @@ -5,16 +5,21 @@ #include "cc/proto/gfx_conversions.h" #include "cc/proto/point.pb.h" +#include "cc/proto/point3f.pb.h" #include "cc/proto/pointf.pb.h" #include "cc/proto/rect.pb.h" #include "cc/proto/rectf.pb.h" +#include "cc/proto/scroll_offset.pb.h" #include "cc/proto/size.pb.h" #include "cc/proto/sizef.pb.h" #include "cc/proto/transform.pb.h" +#include "cc/proto/vector2df.pb.h" #include "ui/gfx/geometry/point.h" +#include "ui/gfx/geometry/point3_f.h" #include "ui/gfx/geometry/point_f.h" #include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect_f.h" +#include "ui/gfx/geometry/scroll_offset.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size_f.h" #include "ui/gfx/transform.h" @@ -39,6 +44,16 @@ gfx::PointF ProtoToPointF(const proto::PointF& proto) { return gfx::PointF(proto.x(), proto.y()); } +void Point3FToProto(const gfx::Point3F& point, proto::Point3F* proto) { + proto->set_x(point.x()); + proto->set_y(point.y()); + proto->set_z(point.z()); +} + +gfx::Point3F ProtoToPoint3F(const proto::Point3F& proto) { + return gfx::Point3F(proto.x(), proto.y(), proto.z()); +} + void RectToProto(const gfx::Rect& rect, proto::Rect* proto) { proto->mutable_origin()->set_x(rect.x()); proto->mutable_origin()->set_y(rect.y()); @@ -91,7 +106,9 @@ void TransformToProto(const gfx::Transform& transform, } gfx::Transform ProtoToTransform(const proto::Transform& proto) { - gfx::Transform transform; + if (proto.matrix_size() == 0) + return gfx::Transform(); + gfx::Transform transform(gfx::Transform::kSkipInitialization); DCHECK_EQ(16, proto.matrix_size()); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { @@ -101,4 +118,23 @@ gfx::Transform ProtoToTransform(const proto::Transform& proto) { return transform; } +void Vector2dFToProto(const gfx::Vector2dF& vector, proto::Vector2dF* proto) { + proto->set_x(vector.x()); + proto->set_y(vector.y()); +} + +gfx::Vector2dF ProtoToVector2dF(const proto::Vector2dF& proto) { + return gfx::Vector2dF(proto.x(), proto.y()); +} + +void ScrollOffsetToProto(const gfx::ScrollOffset& scroll_offset, + proto::ScrollOffset* proto) { + proto->set_x(scroll_offset.x()); + proto->set_y(scroll_offset.y()); +} + +gfx::ScrollOffset ProtoToScrollOffset(const proto::ScrollOffset& proto) { + return gfx::ScrollOffset(proto.x(), proto.y()); +} + } // namespace cc diff --git a/cc/proto/gfx_conversions.h b/cc/proto/gfx_conversions.h index 5b5c31551869f8..b5caa3a3f1f7fb 100644 --- a/cc/proto/gfx_conversions.h +++ b/cc/proto/gfx_conversions.h @@ -9,31 +9,40 @@ namespace gfx { class Point; +class Point3F; class PointF; class Rect; class RectF; +class ScrollOffset; class Size; class SizeF; class Transform; -} +class Vector2dF; +} // namespace gfx namespace cc { namespace proto { class Point; +class Point3F; class PointF; class Rect; class RectF; +class ScrollOffset; class Size; class SizeF; class Transform; -} +class Vector2dF; +} // namespace proto // TODO(dtrainor): Move these to a class and make them static // (crbug.com/548432). CC_EXPORT void PointToProto(const gfx::Point& point, proto::Point* proto); CC_EXPORT gfx::Point ProtoToPoint(const proto::Point& proto); +CC_EXPORT void Point3FToProto(const gfx::Point3F& point, proto::Point3F* proto); +CC_EXPORT gfx::Point3F ProtoToPoint3F(const proto::Point3F& proto); + CC_EXPORT void PointFToProto(const gfx::PointF& point, proto::PointF* proto); CC_EXPORT gfx::PointF ProtoToPointF(const proto::PointF& proto); @@ -53,6 +62,15 @@ CC_EXPORT void TransformToProto(const gfx::Transform& transform, proto::Transform* proto); CC_EXPORT gfx::Transform ProtoToTransform(const proto::Transform& proto); +CC_EXPORT void Vector2dFToProto(const gfx::Vector2dF& vector, + proto::Vector2dF* proto); +CC_EXPORT gfx::Vector2dF ProtoToVector2dF(const proto::Vector2dF& proto); + +CC_EXPORT void ScrollOffsetToProto(const gfx::ScrollOffset& scroll_offset, + proto::ScrollOffset* proto); +CC_EXPORT gfx::ScrollOffset ProtoToScrollOffset( + const proto::ScrollOffset& proto); + } // namespace cc #endif // CC_PROTO_GFX_CONVERSIONS_H_ diff --git a/cc/proto/gfx_conversions_unittest.cc b/cc/proto/gfx_conversions_unittest.cc index 6ffdb96d6e8f11..6affc1ce36891f 100644 --- a/cc/proto/gfx_conversions_unittest.cc +++ b/cc/proto/gfx_conversions_unittest.cc @@ -5,19 +5,25 @@ #include "cc/proto/gfx_conversions.h" #include "cc/proto/point.pb.h" +#include "cc/proto/point3f.pb.h" #include "cc/proto/pointf.pb.h" #include "cc/proto/rect.pb.h" #include "cc/proto/rectf.pb.h" +#include "cc/proto/scroll_offset.pb.h" #include "cc/proto/size.pb.h" #include "cc/proto/sizef.pb.h" #include "cc/proto/transform.pb.h" +#include "cc/proto/vector2df.pb.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/gfx/geometry/point.h" +#include "ui/gfx/geometry/point3_f.h" #include "ui/gfx/geometry/point_f.h" #include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect_f.h" +#include "ui/gfx/geometry/scroll_offset.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size_f.h" +#include "ui/gfx/geometry/vector2d_f.h" #include "ui/gfx/transform.h" namespace cc { @@ -87,6 +93,20 @@ TEST(GfxProtoConversionsTest, SerializeDeserializePointF) { EXPECT_EQ(point, ProtoToPointF(proto)); } +TEST(GfxProtoConversionsTest, SerializeDeserializePoint3F) { + const gfx::Point3F point(5.1f, 10.2f, 13.4f); + + // Test PointFToProto + proto::Point3F proto; + Point3FToProto(point, &proto); + EXPECT_EQ(point.x(), proto.x()); + EXPECT_EQ(point.y(), proto.y()); + EXPECT_EQ(point.z(), proto.z()); + + // Test ProtoToPoint3F + EXPECT_EQ(point, ProtoToPoint3F(proto)); +} + TEST(GfxProtoConversionsTest, SerializeDeserializeSize) { const gfx::Size size(5, 10); @@ -173,5 +193,31 @@ TEST(GfxProtoConversionsTest, SerializeDeserializeTransform) { EXPECT_EQ(transform, ProtoToTransform(proto)); } +TEST(GfxProtoConversionsTest, SerializeDeserializeVector2dF) { + const gfx::Vector2dF vector(5.1f, 10.2f); + + // Test Vector2dFToProto + proto::Vector2dF proto; + Vector2dFToProto(vector, &proto); + EXPECT_EQ(vector.x(), proto.x()); + EXPECT_EQ(vector.y(), proto.y()); + + // Test ProtoToVector2dF + EXPECT_EQ(vector, ProtoToVector2dF(proto)); +} + +TEST(GfxProtoConversionsTest, SerializeDeserializeScrollOffset) { + const gfx::ScrollOffset scroll_offset(5.1f, 10.2f); + + // Test ScrollOffsetToProto + proto::ScrollOffset proto; + ScrollOffsetToProto(scroll_offset, &proto); + EXPECT_EQ(scroll_offset.x(), proto.x()); + EXPECT_EQ(scroll_offset.y(), proto.y()); + + // Test ProtoToScrollOffset + EXPECT_EQ(scroll_offset, ProtoToScrollOffset(proto)); +} + } // namespace } // namespace cc diff --git a/cc/proto/layer_position_constraint.proto b/cc/proto/layer_position_constraint.proto new file mode 100644 index 00000000000000..f84ebf08526a91 --- /dev/null +++ b/cc/proto/layer_position_constraint.proto @@ -0,0 +1,15 @@ +// Copyright 2015 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. + +syntax = "proto2"; + +option optimize_for = LITE_RUNTIME; + +package cc.proto; + +message LayerPositionConstraint { + optional bool is_fixed_position = 1; + optional bool is_fixed_to_right_edge = 2; + optional bool is_fixed_to_bottom_edge = 3; +}; diff --git a/cc/proto/point3f.proto b/cc/proto/point3f.proto new file mode 100644 index 00000000000000..89b0235d61ead5 --- /dev/null +++ b/cc/proto/point3f.proto @@ -0,0 +1,15 @@ +// Copyright 2015 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. + +syntax = "proto2"; + +option optimize_for = LITE_RUNTIME; + +package cc.proto; + +message Point3F { + optional float x = 1; + optional float y = 2; + optional float z = 3; +} diff --git a/cc/proto/scroll_offset.proto b/cc/proto/scroll_offset.proto new file mode 100644 index 00000000000000..b89b1817af04b5 --- /dev/null +++ b/cc/proto/scroll_offset.proto @@ -0,0 +1,14 @@ +// Copyright 2015 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. + +syntax = "proto2"; + +option optimize_for = LITE_RUNTIME; + +package cc.proto; + +message ScrollOffset { + optional double x = 1; + optional double y = 2; +} diff --git a/cc/proto/vector2df.proto b/cc/proto/vector2df.proto new file mode 100644 index 00000000000000..1651b074856fb2 --- /dev/null +++ b/cc/proto/vector2df.proto @@ -0,0 +1,14 @@ +// Copyright 2015 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. + +syntax = "proto2"; + +option optimize_for = LITE_RUNTIME; + +package cc.proto; + +message Vector2dF { + optional float x = 1; + optional float y = 2; +}