summaryrefslogtreecommitdiffstats
path: root/cc/proto/gfx_conversions.cc
blob: 3abc59fcd7cc405319452d94ae7da2ee8e7fec52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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.

#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/vector2d.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/geometry/vector2d.h"
#include "ui/gfx/transform.h"

namespace cc {

void PointToProto(const gfx::Point& point, proto::Point* proto) {
  proto->set_x(point.x());
  proto->set_y(point.y());
}

gfx::Point ProtoToPoint(const proto::Point& proto) {
  return gfx::Point(proto.x(), proto.y());
}

void PointFToProto(const gfx::PointF& point, proto::PointF* proto) {
  proto->set_x(point.x());
  proto->set_y(point.y());
}

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());
  proto->mutable_size()->set_width(rect.width());
  proto->mutable_size()->set_height(rect.height());
}

gfx::Rect ProtoToRect(const proto::Rect& proto) {
  return gfx::Rect(proto.origin().x(), proto.origin().y(), proto.size().width(),
                   proto.size().height());
}

void RectFToProto(const gfx::RectF& rect, proto::RectF* proto) {
  proto->mutable_origin()->set_x(rect.x());
  proto->mutable_origin()->set_y(rect.y());
  proto->mutable_size()->set_width(rect.width());
  proto->mutable_size()->set_height(rect.height());
}

gfx::RectF ProtoToRectF(const proto::RectF& proto) {
  return gfx::RectF(proto.origin().x(), proto.origin().y(),
                    proto.size().width(), proto.size().height());
}

void SizeToProto(const gfx::Size& size, proto::Size* proto) {
  proto->set_width(size.width());
  proto->set_height(size.height());
}

gfx::Size ProtoToSize(const proto::Size& proto) {
  return gfx::Size(proto.width(), proto.height());
}

void SizeFToProto(const gfx::SizeF& size, proto::SizeF* proto) {
  proto->set_width(size.width());
  proto->set_height(size.height());
}

gfx::SizeF ProtoToSizeF(const proto::SizeF& proto) {
  return gfx::SizeF(proto.width(), proto.height());
}

void TransformToProto(const gfx::Transform& transform,
                      proto::Transform* proto) {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      proto->add_matrix(transform.matrix().get(i, j));
    }
  }
}

gfx::Transform ProtoToTransform(const proto::Transform& proto) {
  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++) {
      transform.matrix().set(i, j, proto.matrix(i * 4 + j));
    }
  }
  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());
}

void Vector2dToProto(const gfx::Vector2d& vector, proto::Vector2d* proto) {
  proto->set_x(vector.x());
  proto->set_y(vector.y());
}

gfx::Vector2d ProtoToVector2d(const proto::Vector2d& proto) {
  return gfx::Vector2d(proto.x(), proto.y());
}

}  // namespace cc