1 #ifndef ANDROID_DVR_DISPLAY_PROTOCOL_H_
2 #define ANDROID_DVR_DISPLAY_PROTOCOL_H_
3 
4 #include <sys/types.h>
5 
6 #include <array>
7 #include <map>
8 
9 #include <dvr/dvr_display_types.h>
10 
11 #include <dvr/dvr_api.h>
12 #include <pdx/rpc/buffer_wrapper.h>
13 #include <pdx/rpc/remote_method.h>
14 #include <pdx/rpc/serializable.h>
15 #include <pdx/rpc/variant.h>
16 #include <private/dvr/bufferhub_rpc.h>
17 
18 // RPC protocol definitions for DVR display services (VrFlinger).
19 
20 namespace android {
21 namespace dvr {
22 namespace display {
23 
24 // Native display metrics.
25 struct Metrics {
26   // Basic display properties.
27   uint32_t display_width;
28   uint32_t display_height;
29   uint32_t display_x_dpi;
30   uint32_t display_y_dpi;
31   uint32_t vsync_period_ns;
32 
33   // HMD metrics.
34   // TODO(eieio): Determine how these fields should be populated. On phones
35   // these values are determined at runtime by VrCore based on which headset the
36   // phone is in. On dedicated hardware this needs to come from somewhere else.
37   // Perhaps these should be moved to a separate structure that is returned by a
38   // separate runtime call.
39   uint32_t distorted_width;
40   uint32_t distorted_height;
41   uint32_t hmd_ipd_mm;
42   float inter_lens_distance_m;
43   std::array<float, 4> left_fov_lrbt;
44   std::array<float, 4> right_fov_lrbt;
45 
46  private:
47   PDX_SERIALIZABLE_MEMBERS(Metrics, display_width, display_height,
48                            display_x_dpi, display_y_dpi, vsync_period_ns,
49                            distorted_width, distorted_height, hmd_ipd_mm,
50                            inter_lens_distance_m, left_fov_lrbt,
51                            right_fov_lrbt);
52 };
53 
54 // Serializable base type for enum structs. Enum structs are easier to use than
55 // enum classes, especially for bitmasks. This base type provides common
56 // utilities for flags types.
57 template <typename Integer>
58 class Flags {
59  public:
60   using Base = Flags<Integer>;
61   using Type = Integer;
62 
63   // NOLINTNEXTLINE(google-explicit-constructor)
Flags(const Integer & value)64   Flags(const Integer& value) : value_{value} {}
65   Flags(const Flags&) = default;
66   Flags& operator=(const Flags&) = default;
67 
value()68   Integer value() const { return value_; }
69   // NOLINTNEXTLINE(google-explicit-constructor)
Integer()70   operator Integer() const { return value_; }
71 
IsSet(Integer bits)72   bool IsSet(Integer bits) const { return (value_ & bits) == bits; }
IsClear(Integer bits)73   bool IsClear(Integer bits) const { return (value_ & bits) == 0; }
74 
Set(Integer bits)75   void Set(Integer bits) { value_ |= bits; }
Clear(Integer bits)76   void Clear(Integer bits) { value_ &= ~bits; }
77 
78   Integer operator|(Integer bits) const { return value_ | bits; }
79   Integer operator&(Integer bits) const { return value_ & bits; }
80 
81   Flags& operator|=(Integer bits) {
82     value_ |= bits;
83     return *this;
84   }
85   Flags& operator&=(Integer bits) {
86     value_ &= bits;
87     return *this;
88   }
89 
90  private:
91   Integer value_;
92 
93   PDX_SERIALIZABLE_MEMBERS(Flags<Integer>, value_);
94 };
95 
96 // Flags indicating what changed since last update.
97 struct SurfaceUpdateFlags : public Flags<uint32_t> {
98   enum : Type {
99     None = DVR_SURFACE_UPDATE_FLAGS_NONE,
100     NewSurface = DVR_SURFACE_UPDATE_FLAGS_NEW_SURFACE,
101     BuffersChanged = DVR_SURFACE_UPDATE_FLAGS_BUFFERS_CHANGED,
102     VisibilityChanged = DVR_SURFACE_UPDATE_FLAGS_VISIBILITY_CHANGED,
103     AttributesChanged = DVR_SURFACE_UPDATE_FLAGS_ATTRIBUTES_CHANGED,
104   };
105 
SurfaceUpdateFlagsSurfaceUpdateFlags106   SurfaceUpdateFlags() : Base{None} {}
107   using Base::Base;
108 };
109 
110 // Surface attribute key/value types.
111 using SurfaceAttributeKey = int32_t;
112 using SurfaceAttributeValue =
113     pdx::rpc::Variant<int32_t, int64_t, bool, float, std::array<float, 2>,
114                       std::array<float, 3>, std::array<float, 4>,
115                       std::array<float, 8>, std::array<float, 16>>;
116 
117 // Defined surface attribute keys.
118 struct SurfaceAttribute : public Flags<SurfaceAttributeKey> {
119   enum : Type {
120     // Keys in the negative integer space are interpreted by VrFlinger for
121     // direct surfaces.
122     Direct = DVR_SURFACE_ATTRIBUTE_DIRECT,
123     ZOrder = DVR_SURFACE_ATTRIBUTE_Z_ORDER,
124     Visible = DVR_SURFACE_ATTRIBUTE_VISIBLE,
125 
126     // Invalid key. May be used to terminate C style lists in public API code.
127     Invalid = DVR_SURFACE_ATTRIBUTE_INVALID,
128 
129     // Positive keys are interpreted by the compositor only.
130     FirstUserKey = DVR_SURFACE_ATTRIBUTE_FIRST_USER_KEY,
131   };
132 
SurfaceAttributeSurfaceAttribute133   SurfaceAttribute() : Base{Invalid} {}
134   using Base::Base;
135 };
136 
137 // Collection of surface attribute key/value pairs.
138 using SurfaceAttributes = std::map<SurfaceAttributeKey, SurfaceAttributeValue>;
139 
140 struct SurfaceState {
141   int32_t surface_id;
142   int32_t process_id;
143   int32_t user_id;
144 
145   SurfaceAttributes surface_attributes;
146   SurfaceUpdateFlags update_flags;
147   std::vector<int32_t> queue_ids;
148 
149   // Convenience accessors.
GetVisibleSurfaceState150   bool GetVisible() const {
151     bool bool_value = false;
152     GetAttribute(SurfaceAttribute::Visible, &bool_value,
153                  ValidTypes<int32_t, int64_t, bool, float>{});
154     return bool_value;
155   }
156 
GetZOrderSurfaceState157   int GetZOrder() const {
158     int int_value = 0;
159     GetAttribute(SurfaceAttribute::ZOrder, &int_value,
160                  ValidTypes<int32_t, int64_t, float>{});
161     return int_value;
162   }
163 
164  private:
165   template <typename... Types>
166   struct ValidTypes {};
167 
168   template <typename ReturnType, typename... Types>
GetAttributeSurfaceState169   bool GetAttribute(SurfaceAttributeKey key, ReturnType* out_value,
170                     ValidTypes<Types...>) const {
171     auto search = surface_attributes.find(key);
172     if (search != surface_attributes.end())
173       return pdx::rpc::IfAnyOf<Types...>::Get(&search->second, out_value);
174     else
175       return false;
176   }
177 
178   PDX_SERIALIZABLE_MEMBERS(SurfaceState, surface_id, process_id,
179                            surface_attributes, update_flags, queue_ids);
180 };
181 
182 struct SurfaceInfo {
183   int surface_id;
184   bool visible;
185   int z_order;
186 
187  private:
188   PDX_SERIALIZABLE_MEMBERS(SurfaceInfo, surface_id, visible, z_order);
189 };
190 
191 enum class ConfigFileType : uint32_t {
192   kLensMetrics,
193   kDeviceMetrics,
194   kDeviceConfiguration,
195   kDeviceEdid
196 };
197 
198 struct DisplayProtocol {
199   // Service path.
200   static constexpr char kClientPath[] = "system/vr/display/client";
201 
202   // Op codes.
203   enum {
204     kOpGetMetrics = 0,
205     kOpGetConfigurationData,
206     kOpSetupGlobalBuffer,
207     kOpDeleteGlobalBuffer,
208     kOpGetGlobalBuffer,
209     kOpIsVrAppRunning,
210     kOpCreateSurface,
211     kOpGetSurfaceInfo,
212     kOpCreateQueue,
213     kOpSetAttributes,
214     kOpGetDisplayIdentificationPort,
215   };
216 
217   // Aliases.
218   using LocalChannelHandle = pdx::LocalChannelHandle;
219   using Void = pdx::rpc::Void;
220 
221   // Methods.
222   PDX_REMOTE_METHOD(GetMetrics, kOpGetMetrics, Metrics(Void));
223   PDX_REMOTE_METHOD(GetConfigurationData, kOpGetConfigurationData,
224                     std::string(ConfigFileType config_type));
225   PDX_REMOTE_METHOD(GetDisplayIdentificationPort,
226                     kOpGetDisplayIdentificationPort, uint8_t(Void));
227   PDX_REMOTE_METHOD(SetupGlobalBuffer, kOpSetupGlobalBuffer,
228                     LocalNativeBufferHandle(DvrGlobalBufferKey key, size_t size,
229                                             uint64_t usage));
230   PDX_REMOTE_METHOD(DeleteGlobalBuffer, kOpDeleteGlobalBuffer,
231                     void(DvrGlobalBufferKey key));
232   PDX_REMOTE_METHOD(GetGlobalBuffer, kOpGetGlobalBuffer,
233                     LocalNativeBufferHandle(DvrGlobalBufferKey key));
234   PDX_REMOTE_METHOD(IsVrAppRunning, kOpIsVrAppRunning, bool(Void));
235   PDX_REMOTE_METHOD(CreateSurface, kOpCreateSurface,
236                     SurfaceInfo(const SurfaceAttributes& attributes));
237   PDX_REMOTE_METHOD(GetSurfaceInfo, kOpGetSurfaceInfo, SurfaceInfo(Void));
238   PDX_REMOTE_METHOD(
239       CreateQueue, kOpCreateQueue,
240       LocalChannelHandle(const ProducerQueueConfig& producer_config));
241   PDX_REMOTE_METHOD(SetAttributes, kOpSetAttributes,
242                     void(const SurfaceAttributes& attributes));
243 };
244 
245 struct DisplayManagerProtocol {
246   // Service path.
247   static constexpr char kClientPath[] = "system/vr/display/manager";
248 
249   // Op codes.
250   enum {
251     kOpGetSurfaceState = 0,
252     kOpGetSurfaceQueue,
253   };
254 
255   // Aliases.
256   using LocalChannelHandle = pdx::LocalChannelHandle;
257   using Void = pdx::rpc::Void;
258 
259   // Methods.
260   PDX_REMOTE_METHOD(GetSurfaceState, kOpGetSurfaceState,
261                     std::vector<SurfaceState>(Void));
262   PDX_REMOTE_METHOD(GetSurfaceQueue, kOpGetSurfaceQueue,
263                     LocalChannelHandle(int surface_id, int queue_id));
264 };
265 
266 struct VSyncSchedInfo {
267   int64_t vsync_period_ns;
268   int64_t timestamp_ns;
269   uint32_t next_vsync_count;
270 
271  private:
272   PDX_SERIALIZABLE_MEMBERS(VSyncSchedInfo, vsync_period_ns, timestamp_ns,
273                            next_vsync_count);
274 };
275 
276 struct VSyncProtocol {
277   // Service path.
278   static constexpr char kClientPath[] = "system/vr/display/vsync";
279 
280   // Op codes.
281   enum {
282     kOpWait = 0,
283     kOpAck,
284     kOpGetLastTimestamp,
285     kOpGetSchedInfo,
286     kOpAcknowledge,
287   };
288 
289   // Aliases.
290   using Void = pdx::rpc::Void;
291   using Timestamp = int64_t;
292 
293   // Methods.
294   PDX_REMOTE_METHOD(Wait, kOpWait, Timestamp(Void));
295   PDX_REMOTE_METHOD(GetLastTimestamp, kOpGetLastTimestamp, Timestamp(Void));
296   PDX_REMOTE_METHOD(GetSchedInfo, kOpGetSchedInfo, VSyncSchedInfo(Void));
297   PDX_REMOTE_METHOD(Acknowledge, kOpAcknowledge, void(Void));
298 };
299 
300 }  // namespace display
301 }  // namespace dvr
302 }  // namespace android
303 
304 #endif  // ANDROID_DVR_DISPLAY_PROTOCOL_H_
305