1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DEVICE_VR_PUBLIC_MOJOM_VR_SERVICE_MOJOM_TRAITS_H_
6 #define DEVICE_VR_PUBLIC_MOJOM_VR_SERVICE_MOJOM_TRAITS_H_
7 
8 #include "device/vr/public/mojom/pose.h"
9 #include "device/vr/public/mojom/rgb_tuple_f32.h"
10 #include "device/vr/public/mojom/rgba_tuple_f16.h"
11 #include "device/vr/public/mojom/vr_service.mojom-shared.h"
12 #include "mojo/public/cpp/bindings/struct_traits.h"
13 #include "ui/gfx/geometry/mojom/geometry_mojom_traits.h"
14 #include "ui/gfx/geometry/point3_f.h"
15 #include "ui/gfx/geometry/quaternion.h"
16 
17 namespace mojo {
18 
19 template <>
20 struct StructTraits<device::mojom::RgbaTupleF16DataView, device::RgbaTupleF16> {
21   static uint16_t red(const device::RgbaTupleF16& rgba) { return rgba.red(); }
22   static uint16_t green(const device::RgbaTupleF16& rgba) {
23     return rgba.green();
24   }
25   static uint16_t blue(const device::RgbaTupleF16& rgba) { return rgba.blue(); }
26   static uint16_t alpha(const device::RgbaTupleF16& rgba) {
27     return rgba.alpha();
28   }
29   static bool Read(device::mojom::RgbaTupleF16DataView data,
30                    device::RgbaTupleF16* out) {
31     out->set_red(data.red());
32     out->set_green(data.green());
33     out->set_blue(data.blue());
34     out->set_alpha(data.alpha());
35     return true;
36   }
37 };
38 
39 template <>
40 struct StructTraits<device::mojom::RgbTupleF32DataView, device::RgbTupleF32> {
41   static float red(const device::RgbTupleF32& rgba) { return rgba.red(); }
42   static float green(const device::RgbTupleF32& rgba) { return rgba.green(); }
43   static float blue(const device::RgbTupleF32& rgba) { return rgba.blue(); }
44   static bool Read(device::mojom::RgbTupleF32DataView data,
45                    device::RgbTupleF32* out) {
46     out->set_red(data.red());
47     out->set_green(data.green());
48     out->set_blue(data.blue());
49     return true;
50   }
51 };
52 
53 template <>
54 class StructTraits<device::mojom::PoseDataView, device::Pose> {
55  public:
56   static const gfx::Point3F& position(const device::Pose& pose) {
57     return pose.position();
58   }
59   static const gfx::Quaternion& orientation(const device::Pose& pose) {
60     return pose.orientation();
61   }
62 
63   static bool Read(device::mojom::PoseDataView pose_data,
64                    device::Pose* out_pose) {
65     gfx::Point3F position;
66     if (!pose_data.ReadPosition(&position)) {
67       return false;
68     }
69 
70     gfx::Quaternion orientation;
71     if (!pose_data.ReadOrientation(&orientation)) {
72       return false;
73     }
74 
75     *out_pose = device::Pose(position, orientation);
76     return true;
77   }
78 };
79 
80 }  // namespace mojo
81 
82 #endif  // DEVICE_VR_PUBLIC_MOJOM_VR_SERVICE_MOJOM_TRAITS_H_
83