• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Flags(const Integer & value)63   Flags(const Integer& value) : value_{value} {}
64   Flags(const Flags&) = default;
65   Flags& operator=(const Flags&) = default;
66 
value()67   Integer value() const { return value_; }
Integer()68   operator Integer() const { return value_; }
69 
IsSet(Integer bits)70   bool IsSet(Integer bits) const { return (value_ & bits) == bits; }
IsClear(Integer bits)71   bool IsClear(Integer bits) const { return (value_ & bits) == 0; }
72 
Set(Integer bits)73   void Set(Integer bits) { value_ |= bits; }
Clear(Integer bits)74   void Clear(Integer bits) { value_ &= ~bits; }
75 
76   Integer operator|(Integer bits) const { return value_ | bits; }
77   Integer operator&(Integer bits) const { return value_ & bits; }
78 
79   Flags& operator|=(Integer bits) {
80     value_ |= bits;
81     return *this;
82   }
83   Flags& operator&=(Integer bits) {
84     value_ &= bits;
85     return *this;
86   }
87 
88  private:
89   Integer value_;
90 
91   PDX_SERIALIZABLE_MEMBERS(Flags<Integer>, value_);
92 };
93 
94 // Flags indicating what changed since last update.
95 struct SurfaceUpdateFlags : public Flags<uint32_t> {
96   enum : Type {
97     None = DVR_SURFACE_UPDATE_FLAGS_NONE,
98     NewSurface = DVR_SURFACE_UPDATE_FLAGS_NEW_SURFACE,
99     BuffersChanged = DVR_SURFACE_UPDATE_FLAGS_BUFFERS_CHANGED,
100     VisibilityChanged = DVR_SURFACE_UPDATE_FLAGS_VISIBILITY_CHANGED,
101     AttributesChanged = DVR_SURFACE_UPDATE_FLAGS_ATTRIBUTES_CHANGED,
102   };
103 
SurfaceUpdateFlagsSurfaceUpdateFlags104   SurfaceUpdateFlags() : Base{None} {}
105   using Base::Base;
106 };
107 
108 // Surface attribute key/value types.
109 using SurfaceAttributeKey = int32_t;
110 using SurfaceAttributeValue =
111     pdx::rpc::Variant<int32_t, int64_t, bool, float, std::array<float, 2>,
112                       std::array<float, 3>, std::array<float, 4>,
113                       std::array<float, 8>, std::array<float, 16>>;
114 
115 // Defined surface attribute keys.
116 struct SurfaceAttribute : public Flags<SurfaceAttributeKey> {
117   enum : Type {
118     // Keys in the negative integer space are interpreted by VrFlinger for
119     // direct surfaces.
120     Direct = DVR_SURFACE_ATTRIBUTE_DIRECT,
121     ZOrder = DVR_SURFACE_ATTRIBUTE_Z_ORDER,
122     Visible = DVR_SURFACE_ATTRIBUTE_VISIBLE,
123 
124     // Invalid key. May be used to terminate C style lists in public API code.
125     Invalid = DVR_SURFACE_ATTRIBUTE_INVALID,
126 
127     // Positive keys are interpreted by the compositor only.
128     FirstUserKey = DVR_SURFACE_ATTRIBUTE_FIRST_USER_KEY,
129   };
130 
SurfaceAttributeSurfaceAttribute131   SurfaceAttribute() : Base{Invalid} {}
132   using Base::Base;
133 };
134 
135 // Collection of surface attribute key/value pairs.
136 using SurfaceAttributes = std::map<SurfaceAttributeKey, SurfaceAttributeValue>;
137 
138 struct SurfaceState {
139   int32_t surface_id;
140   int32_t process_id;
141   int32_t user_id;
142 
143   SurfaceAttributes surface_attributes;
144   SurfaceUpdateFlags update_flags;
145   std::vector<int32_t> queue_ids;
146 
147   // Convenience accessors.
GetVisibleSurfaceState148   bool GetVisible() const {
149     bool bool_value = false;
150     GetAttribute(SurfaceAttribute::Visible, &bool_value,
151                  ValidTypes<int32_t, int64_t, bool, float>{});
152     return bool_value;
153   }
154 
GetZOrderSurfaceState155   int GetZOrder() const {
156     int int_value = 0;
157     GetAttribute(SurfaceAttribute::ZOrder, &int_value,
158                  ValidTypes<int32_t, int64_t, float>{});
159     return int_value;
160   }
161 
162  private:
163   template <typename... Types>
164   struct ValidTypes {};
165 
166   template <typename ReturnType, typename... Types>
GetAttributeSurfaceState167   bool GetAttribute(SurfaceAttributeKey key, ReturnType* out_value,
168                     ValidTypes<Types...>) const {
169     auto search = surface_attributes.find(key);
170     if (search != surface_attributes.end())
171       return pdx::rpc::IfAnyOf<Types...>::Get(&search->second, out_value);
172     else
173       return false;
174   }
175 
176   PDX_SERIALIZABLE_MEMBERS(SurfaceState, surface_id, process_id,
177                            surface_attributes, update_flags, queue_ids);
178 };
179 
180 struct SurfaceInfo {
181   int surface_id;
182   bool visible;
183   int z_order;
184 
185  private:
186   PDX_SERIALIZABLE_MEMBERS(SurfaceInfo, surface_id, visible, z_order);
187 };
188 
189 enum class ConfigFileType : uint32_t {
190   kLensMetrics,
191   kDeviceMetrics,
192   kDeviceConfiguration
193 };
194 
195 struct DisplayProtocol {
196   // Service path.
197   static constexpr char kClientPath[] = "system/vr/display/client";
198 
199   // Op codes.
200   enum {
201     kOpGetMetrics = 0,
202     kOpGetConfigurationData,
203     kOpSetupGlobalBuffer,
204     kOpDeleteGlobalBuffer,
205     kOpGetGlobalBuffer,
206     kOpIsVrAppRunning,
207     kOpCreateSurface,
208     kOpGetSurfaceInfo,
209     kOpCreateQueue,
210     kOpSetAttributes,
211   };
212 
213   // Aliases.
214   using LocalChannelHandle = pdx::LocalChannelHandle;
215   using Void = pdx::rpc::Void;
216 
217   // Methods.
218   PDX_REMOTE_METHOD(GetMetrics, kOpGetMetrics, Metrics(Void));
219   PDX_REMOTE_METHOD(GetConfigurationData, kOpGetConfigurationData,
220                     std::string(ConfigFileType config_type));
221   PDX_REMOTE_METHOD(SetupGlobalBuffer, kOpSetupGlobalBuffer,
222                     LocalNativeBufferHandle(DvrGlobalBufferKey key, size_t size,
223                                             uint64_t usage));
224   PDX_REMOTE_METHOD(DeleteGlobalBuffer, kOpDeleteGlobalBuffer,
225                     void(DvrGlobalBufferKey key));
226   PDX_REMOTE_METHOD(GetGlobalBuffer, kOpGetGlobalBuffer,
227                     LocalNativeBufferHandle(DvrGlobalBufferKey key));
228   PDX_REMOTE_METHOD(IsVrAppRunning, kOpIsVrAppRunning, bool(Void));
229   PDX_REMOTE_METHOD(CreateSurface, kOpCreateSurface,
230                     SurfaceInfo(const SurfaceAttributes& attributes));
231   PDX_REMOTE_METHOD(GetSurfaceInfo, kOpGetSurfaceInfo, SurfaceInfo(Void));
232   PDX_REMOTE_METHOD(
233       CreateQueue, kOpCreateQueue,
234       LocalChannelHandle(const ProducerQueueConfig& producer_config));
235   PDX_REMOTE_METHOD(SetAttributes, kOpSetAttributes,
236                     void(const SurfaceAttributes& attributes));
237 };
238 
239 struct DisplayManagerProtocol {
240   // Service path.
241   static constexpr char kClientPath[] = "system/vr/display/manager";
242 
243   // Op codes.
244   enum {
245     kOpGetSurfaceState = 0,
246     kOpGetSurfaceQueue,
247   };
248 
249   // Aliases.
250   using LocalChannelHandle = pdx::LocalChannelHandle;
251   using Void = pdx::rpc::Void;
252 
253   // Methods.
254   PDX_REMOTE_METHOD(GetSurfaceState, kOpGetSurfaceState,
255                     std::vector<SurfaceState>(Void));
256   PDX_REMOTE_METHOD(GetSurfaceQueue, kOpGetSurfaceQueue,
257                     LocalChannelHandle(int surface_id, int queue_id));
258 };
259 
260 struct VSyncSchedInfo {
261   int64_t vsync_period_ns;
262   int64_t timestamp_ns;
263   uint32_t next_vsync_count;
264 
265  private:
266   PDX_SERIALIZABLE_MEMBERS(VSyncSchedInfo, vsync_period_ns, timestamp_ns,
267                            next_vsync_count);
268 };
269 
270 struct VSyncProtocol {
271   // Service path.
272   static constexpr char kClientPath[] = "system/vr/display/vsync";
273 
274   // Op codes.
275   enum {
276     kOpWait = 0,
277     kOpAck,
278     kOpGetLastTimestamp,
279     kOpGetSchedInfo,
280     kOpAcknowledge,
281   };
282 
283   // Aliases.
284   using Void = pdx::rpc::Void;
285   using Timestamp = int64_t;
286 
287   // Methods.
288   PDX_REMOTE_METHOD(Wait, kOpWait, Timestamp(Void));
289   PDX_REMOTE_METHOD(GetLastTimestamp, kOpGetLastTimestamp, Timestamp(Void));
290   PDX_REMOTE_METHOD(GetSchedInfo, kOpGetSchedInfo, VSyncSchedInfo(Void));
291   PDX_REMOTE_METHOD(Acknowledge, kOpAcknowledge, void(Void));
292 };
293 
294 }  // namespace display
295 }  // namespace dvr
296 }  // namespace android
297 
298 #endif  // ANDROID_DVR_DISPLAY_PROTOCOL_H_
299