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