Skip to content

Commit

Permalink
Add support for converting more types to and from protobuf.
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
tommynyquist authored and Commit bot committed Nov 20, 2015
1 parent a2d5e73 commit 3228fd5
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cc/cc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions cc/layers/layer_position_constraint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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_)
Expand Down
7 changes: 7 additions & 0 deletions cc/layers/layer_position_constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace cc {

namespace proto {
class LayerPositionConstraint;
}

class CC_EXPORT LayerPositionConstraint {
public:
LayerPositionConstraint();
Expand All @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions cc/layers/layer_position_constraint_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions cc/proto/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
38 changes: 37 additions & 1 deletion cc/proto/gfx_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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());
Expand Down Expand Up @@ -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++) {
Expand All @@ -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
22 changes: 20 additions & 2 deletions cc/proto/gfx_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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_
46 changes: 46 additions & 0 deletions cc/proto/gfx_conversions_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions cc/proto/layer_position_constraint.proto
Original file line number Diff line number Diff line change
@@ -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;
};
15 changes: 15 additions & 0 deletions cc/proto/point3f.proto
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 14 additions & 0 deletions cc/proto/scroll_offset.proto
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 3228fd5

Please sign in to comment.